#include <DHTesp.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define DHT_PIN 25
#define photoresistor 26
#define MOSFET 14
#define RELAY_servo 12
#define SOIL_MOISTURE 32
#define WATER_LEVEL 33
#define SERVO_PIN 13
DHTesp dhtSensor;
Servo myServo;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//***Set server***
const char* mqttServer = "broker.hivemq.com";
int port = 1883;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
unsigned long lastMsg = 0;
void wifiConnect() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connected!");
}
void mqttConnect() {
while (!mqttClient.connected()) {
Serial.println("Attemping MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("connected");
//***Subscribe all topic you need***
mqttClient.subscribe("/ThinkIOT/led");
mqttClient.subscribe("/ThinkIOT/servo");
mqttClient.subscribe("/ThinkIOT/relay");
}
else {
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
//MQTT Receiver
void callback(char* topic, byte* message, unsigned int length) {
Serial.println(topic);
String strMsg;
for (int i = 0; i < length; i++) {
strMsg += (char)message[i];
}
Serial.println(strMsg);
char status[10];
int value;
sscanf(strMsg.c_str(), "{\"status\":\"%[^\"]\",\"value\":%d}", status, &value);
Serial.println(status);
Serial.println(value);
if (strcmp(status, "LED_ON") == 0)
analogWrite(MOSFET, map(value,0,1000,0,1023));
else if (strcmp(status, "LED_OFF") == 0)
analogWrite(MOSFET, 0);
if (strcmp(status, "Relay_ON") == 0)
digitalWrite(RELAY_servo,HIGH);
else if (strcmp(status, "Relay_OFF") == 0)
digitalWrite(RELAY_servo,LOW);
if (strcmp(status, "Servo_ON") == 0)
myServo.write(map(value,0,2000,0,180));
else if (strcmp(status, "Servo_OFF") == 0)
myServo.write(90);
//***Code here to process the received package***
}
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
wifiConnect();
mqttClient.setServer(mqttServer, port);
mqttClient.setCallback(callback);
mqttClient.setKeepAlive( 90 );
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//PINMODE
pinMode(photoresistor, INPUT);
pinMode(MOSFET, OUTPUT);
pinMode(RELAY_servo, OUTPUT);
pinMode(SOIL_MOISTURE, INPUT);
pinMode(WATER_LEVEL, INPUT);
//Servo
myServo.attach(SERVO_PIN);
}
void loop() {
if (!mqttClient.connected()) {
mqttConnect();
}
mqttClient.loop();
//***Publish data to MQTT Server***
unsigned long now = millis();
if (now - lastMsg > 2000) {
lastMsg=now;
TempAndHumidity data = dhtSensor.getTempAndHumidity();
String temp = String(data.temperature, 2);
mqttClient.publish("/ThinkIOT/temp", temp.c_str());
String hum = String(data.humidity, 1);
mqttClient.publish("/ThinkIOT/hum", hum.c_str());
int luminosity= map(analogRead(photoresistor),4063,32,0,1000);
String lum = String(luminosity);
mqttClient.publish("/ThinkIOT/luminosity", lum.c_str());
int soil_moisture=map(analogRead(SOIL_MOISTURE),0,1023,0,100);
String soil = String(soil_moisture);
mqttClient.publish("/ThinkIOT/soil_moisture", soil.c_str());
int tank_volume=50;
int water_level=map(analogRead(WATER_LEVEL),480,710,0,40)*tank_volume;
String water = String(water_level);
mqttClient.publish("/ThinkIOT/water_level", water.c_str());
delay(5000);
}
}