This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
83 lines (72 loc) · 2.24 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ArduinoJson.h"
#include "EspMQTTClient.h"
#include "DHTesp.h"
#include <cmath>
const int DHT_PIN = 15;
DHTesp dhtSensor;
// MQTT Configuracoes
EspMQTTClient client{
"Wokwi-GUEST", //SSID do WiFi
"", // Senha do wifi
"mqtt.tago.io", // Endereço do servidor
"Just Me", // Usuario
"7c997302-d93c-4308-9dbc-7cf48969f4d1", // Token do device
"esp", // Nome do device
1883 // Porta de comunicação
};
void setup() {
Serial.begin(9600);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
Serial.println("Conectando WiFi");
while (!client.isWifiConnected()) {
Serial.print('.'); client.loop(); delay(1000);
}
Serial.println("WiFi Conectado");
Serial.println("Conectando com Servidor MQTT");
while (!client.isMqttConnected()) {
Serial.print('.'); client.loop(); delay(1000);
}
Serial.println("MQTT Conectado");
}
// Callback da EspMQTTClient
void onConnectionEstablished()
{}
char bufferJson[100];
int PAS = 0;
int PAD = 0;
float temperatura = 0;
void loop() {
PAS = map(analogRead(32),0,4095,70,370);
PAD = map(analogRead(35),0,4095,40,200);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
temperatura = round(data.temperature);
//JSON PAS
StaticJsonDocument<300> documentoJson;
documentoJson["variable"] = "PAS";
documentoJson["value"] = PAS;
documentoJson["unit"] = "mmHg";
serializeJson(documentoJson, bufferJson);
Serial.println(bufferJson);
//Envio de dados
client.publish("topicoTDSPI", bufferJson);
//JSON PAD
StaticJsonDocument<300> documentoJson2;
documentoJson2["variable"] = "PAD";
documentoJson2["value"] = PAD;
documentoJson2["unit"] = "mmHg";
serializeJson(documentoJson2, bufferJson);
Serial.println(bufferJson);
//Envio de dados
client.publish("topicoTDSPI_2", bufferJson);
//JSON TEMPERATURA
StaticJsonDocument<300> documentoJson3;
documentoJson3["variable"] = "Temperatura";
documentoJson3["value"] = temperatura;
documentoJson3["unit"] = "celsius";
serializeJson(documentoJson3, bufferJson);
Serial.println(bufferJson);
//Envio de dados
client.publish("topicoTDSPI_3", bufferJson);
delay(5000); //Tempo de espera para envio
client.loop();
}