-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-dht11-wifi.ino
76 lines (64 loc) · 2.34 KB
/
esp32-dht11-wifi.ino
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
#inclide <ArduinoJson.h> // add ArduinoJson library v5.9.0
#include <Adafruit_Sensor.h> // add Adafruit_Sensor library
#include <PubSubClient.h> // add PubSubClient library
#include <DHT.h> // add DHT library
#define DHTPIN 4 // define pin which is connected to DHT sensor
#define DHTTYPE DHT11 // define type of sensor, if you're another sensor change it
const char* ssid = "ssid"; // your wifi ssid
const char* password = "password"; // your wifi password
const char* mqttServer = "192.168.1.100"; // mqtt server address
const int mqttPort = 1883; // mqtt port
const char* mqttUser = "mqtt_username"; // your mqtt user
const char* mqttPassword = "mqtt_password"; // your mqtt password
float t; // variable for temperature
float h; // variable for humidity
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
void setup()
{
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
readPZEM();
StaticJsonBuffer <300> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder["device"] = "ESP32";
JSONencoder["sensor type"] = "DHT11";
JSONencoder["temperature"] = t;
JSONencoder["humidity"] = h;
char JSONmessageBuffer[100];
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println("Sending message to MQTT topic..");
Serial.println(JSONmessageBuffer);
if (client.publish("esp/dht11", JSONmessageBuffer) == true) {
Serial.println("Success sending message");
} else {
Serial.println("Error sending message");
}
delay(2000);
client.loop();
}
// function to read temperature and humidity
void readData(){
t = dht.readTemperature();
h = dht.readHumidity();
}