// #include "arduino_secrets.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// Add your MQTT Broker IP address, example:
// const char* mqtt_server = "172.16.25.57";
// const char* mqtt_server = "192.168.100.14";
//const char* mqtt_server = "test.mosquitto.org";
// const char* mqtt_server = "broker.emqx.io";
const char* mqtt_server = "mqtt.eclipseprojects.io";
#define DHTPIN 32 // Digital pin connected to the DHT sensor
// Uncomment the type of sensor in use:
// #define DHTTYPE DHT11 // DHT 11
// #define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int connT=0;
float temperature = 0;
float humidity = 0;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// const char* ssid = "HUTABARAT";
// const char* password = "1234barat";
// LED Pin
const int ledPin1 = 14;
const int ledPin2 = 12;
const int ledPin3 = 13;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(2000);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
dht.begin();
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
Serial.println("reconnect");
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 15000) {
lastMsg = now;
float t = dht.readTemperature();Serial.println(t);
float h = dht.readHumidity();Serial.println(h);
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
// Convert the value to a char array
char tempString[8];
dtostrf(t, 1, 2, tempString);
// Serial.print("Temperature: ");Serial.println(tempString);
client.publish("esp32/sensor01/temperature", tempString);
// sDatang(tempString);//delay(500);
humidity = analogRead(35);
// Convert the value to a char array
char humString[8];
dtostrf(h, 1, 2, humString);
// Serial.print("Humidity: ");Serial.println(humString);
client.publish("esp32/sensor01/humidity", humString);
// sDatang(humString);//delay(500);
}
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");delay(500);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
connT++;
if(connT>=10){
ESP.restart();
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
if (String(topic) == "esp32/sensor01/output1") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin1, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin1, LOW);
}
}
if (String(topic) == "esp32/sensor01/output2") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin2, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin2, LOW);
}
}
if (String(topic) == "esp32/sensor01/output3") {
Serial.print("Changing output to ");
if(messageTemp == "on"){
Serial.println("on");
digitalWrite(ledPin3, HIGH);
}
else if(messageTemp == "off"){
Serial.println("off");
digitalWrite(ledPin3, LOW);
}
}
}
void reconnect() {
// Loop until we're reconnected
Serial.println("Reconnect MQTT");
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("sensor02")) {
Serial.println("connected");
// Subscribe
client.subscribe("esp32/sensor01/output1");
client.subscribe("esp32/sensor01/output2");
client.subscribe("esp32/sensor01/output3");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}