#include <WiFi.h> //Import two new library
#include <PubSubClient.h>
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>
#define WIFI_SSID "Wokwi-GUEST" //WIFI SSID NAME
#define WIFI_PW "" //WIFI PASSWORD
#define MQTT_BROKER "broker.mqtt-dashboard.com" //MQTT BROKER IP
#define MQTT_PORT 1883 //MQTT BROKER PORT
const int DHT_PIN = 15;
int pos = 0;
int OnOffAutoInt = 0;
boolean LCDblinkWiFi=true;
boolean LCDblinkMqtt=true;
WiFiClient espClient; // ESP8266 Wifi Client
PubSubClient client(espClient); // MQTT Client using the Wifi-Client
DHTesp dhtSensor;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
Servo servo;
//https://maxpromer.github.io/LCD-Character-Creator/
byte customChar0[] = {
0x00,
0x00,
0x00,
0x00,
0x00,
0x1F,
0x0E,
0x04
};
byte customChar1[] = {
0x00,
0x00,
0x00,
0x1F,
0x00,
0x0E,
0x00,
0x04
};
byte customChar2[] = {
0x05,
0x06,
0x07,
0x00,
0x00,
0x1C,
0x0C,
0x14
};
byte customChar3[] = {
0x01,
0x0A,
0x0C,
0x0E,
0x0E,
0x06,
0x0A,
0x10
};
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
servo.attach(2);
LCD.init();
LCD.backlight();
LCD.createChar(0, customChar0);
LCD.createChar(1, customChar1);
LCD.createChar(2, customChar2);
LCD.createChar(3, customChar3);
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin(WIFI_SSID, WIFI_PW); // Start Wi-Fi connection
while (WiFi.status() != WL_CONNECTED) { // Waiting for connection success
delay(250);
Serial.println(".");
LCD.print(".");
}
Serial.println("Hello from ESP8266");
Serial.print("Connected to hotspot: ");
Serial.println(WIFI_SSID);
LCD.clear();
LCD.setCursor(15, 1);
LCD.write(0);
LCD.print(WiFi.localIP());
Serial.println("-------------------------");
client.setServer(MQTT_BROKER, MQTT_PORT); //Start MQTT connection
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESPGM", "GrigoriyMokrenko", "GrigoriyMokrenko9" )) { //Sign in to the broker
Serial.println("Connected to broker");
client.subscribe("OnOffAuto");
LCD.setCursor(15, 0);
LCD.write(2);
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 2) + "%");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Tem:");
LCD.print(String(data.temperature, 2));
LCD.setCursor(0, 1);
LCD.print("Hum:");
LCD.print(String(data.humidity, 2));
blink();
delay(1000);
switch (OnOffAutoInt) {
case 0:
pos=0;
break;
case 1:
pos=90;
break;
case 2:
pos=180;
break;
case 3:
if ((data.temperature>14)&&(data.humidity>40)){
pos = map(data.humidity, 40, 100, 0, 180);
} else {
pos=0;
}
break;
}
Serial.print("<: ");
Serial.println(pos);
servo.write(pos);
client.loop();
client.publish("GM-T", String(data.temperature, 2).c_str());
client.publish("GM-H", String(data.humidity, 2).c_str());
client.publish("GM-pos", String(pos).c_str());
}
void blink(){
if (WiFi.status() == WL_CONNECTED) { // Waiting for connection success
if (LCDblinkWiFi){
LCD.setCursor(15, 1);
LCD.write(0);
} else {
LCD.setCursor(15, 1);
LCD.write(1);
}
LCDblinkWiFi=!LCDblinkWiFi;
} else {
LCD.setCursor(15, 1);
LCD.print(".");
}
if (client.connected()) { // Waiting for connection success
if (LCDblinkMqtt){
LCD.setCursor(15, 0);
LCD.write(2);
} else {
LCD.setCursor(15, 0);
LCD.write(3);
}
LCDblinkMqtt=!LCDblinkMqtt;
} else {
LCD.setCursor(15, 0);
LCD.print(".");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String ValueString;
Serial.print("Message arrived in topic: "); // Print out the topic
Serial.println(topic);
Serial.print("Message:"); //Print Received message
for (int i = 0; i < length; i++) {
ValueString=ValueString+(char)payload[i];
}
OnOffAutoInt=ValueString.toInt();
Serial.println(OnOffAutoInt);
// Serial.println("-----------------------");
delay(3000);
}