#include <WiFi.h> //Import two new library
#include <PubSubClient.h>
#include "RTClib.h"
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "ThingSpeak.h"
#include "DHTesp.h"
boolean chek=true;
unsigned long myChannelMumber = 1925564;
unsigned long timerDelay = 30000;
const char * myReadAPIKey = "G9CLUUN6J6W8CZW9";
const char * myWriteAPIKey = "J3US9NL341SHURAL";
int photoresistor_string;
#define WIFI_SSID "Wokwi-GUEST" //WIFI SSID NAME
#define WIFI_PW "" //WIFI PASSWORD
#define MQTT_BROKER "broker.hivemq.com" //MQTT BROKER IP
#define MQTT_PORT 1883 //MQTT BROKER PORT
#define TFT_DC 2
#define TFT_CS 15
const int led = 32;
const int pir = 5;
const int LDR_PIN= 34 ;
const int DHT_PIN = 13;
int photoresistor1=0, lux=0,stop_led=5000,lastTime=30000,photoresistor=0,i=0, intOnOFF=0, start=0,vrema=0;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
RTC_DS1307 rtc;
DHTesp dhtSensor;
WiFiClient espClient; // ESP8266 Wifi Client
PubSubClient client(espClient); // MQTT Client using the Wifi-Client
void setup() {
Serial.begin(115200);
pinMode(led, OUTPUT);
pinMode(pir, INPUT);
pinMode(LDR_PIN, INPUT);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
tft.begin();
WiFi.begin(WIFI_SSID, WIFI_PW); // Start Wi-Fi connection
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
while (WiFi.status() != WL_CONNECTED) { // Waiting for connection success
delay(250);
Serial.println(".");
}
Serial.println("Hello from ESP32");
Serial.print("Connected to hotspot: ");
Serial.println(WIFI_SSID);
Serial.print("IP address is: ");
Serial.println(WiFi.localIP());
Serial.println("-------------------------");
client.setSocketTimeout(60);
client.setKeepAlive(5);
client.setServer(MQTT_BROKER, MQTT_PORT); //Start MQTT connection
client.setCallback(callback);
reconnect();
client.subscribe("0start1");
client.subscribe("led01");
client.subscribe("pat_photoresistor");
client.publish("0start1", String(intOnOFF).c_str());
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
DateTime now = rtc.now();
start=digitalRead(pir);
photoresistor=analogRead(LDR_PIN);
client.publish("patt", String(data.temperature, 2).c_str());
client.publish("path", String(data.humidity, 2).c_str());
tft.setTextSize(2);
tft.setCursor(1, 15);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(1, 15);
tft.setTextColor(ILI9341_GREEN);
tft.print(now.year(), DEC);
tft.print('/');
tft.print(now.month(), DEC);
tft.print('/');
tft.print(now.day(), DEC);
tft.print(" (");
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.print(") ");
tft.print(now.hour(), DEC);
tft.print(':');
tft.print(now.minute(), DEC);
tft.print(':');
tft.print(now.second(), DEC);
tft.println();
tft.setTextColor(ILI9341_GREEN);
tft.print("move:");
tft.println(start);
client.publish("pat_move01", String(start).c_str());
// if((millis() - lastTime) > timerDelay){
// if(chek){
// ThingSpeak.writeField(myChannelMumber,1,data.temperature,myWriteAPIKey);
// }else{
// ThingSpeak.writeField(myChannelMumber,2,data.humidity,myWriteAPIKey);
// }
// chek=!chek;
// lastTime=millis();
// Serial.println("WRITE");
// }
lux=map(photoresistor,4063,32,1,100000);
delay(1000);
switch (intOnOFF) {
case 0:
client.publish("led_on_and_off", "0");
tft.println("Off");
start=0;
vrema=0;
digitalWrite(led, 0);
break;
case 1:
client.publish("led_on_and_off", "0");
tft.println("Auto");
if (((photoresistor>1718)||(photoresistor1>1718))&&(intOnOFF==1)&&(vrema==0)&&(start==1)){
vrema=1;
Serial.println("Старт");
}
if (((millis() - lastTime) < stop_led)&&(lastTime!=0)&&(vrema==1)){
digitalWrite(led,1);
client.publish("led_on_and_off", "1");
}
else
{
vrema=0;
digitalWrite(led, 0);
client.publish("led_on_and_off", "0");
}
delay(200);
Serial.println("<: " + String(start));
Serial.println("<: " + String(vrema));
Serial.println("<: " + String(intOnOFF));
break;
case 2:
tft.println("On diod");
digitalWrite(led,1);
client.publish("led_on_and_off", "1");
break;
}
if (!client.connected()) {
reconnect();
}
client.loop();
tft.println("Temp: " + String(data.temperature, 2) + "C");
tft.println("Humidity: " + String(data.humidity, 2) + "%");
delay(1000);
tft.print("photoresistor:");
tft.println(photoresistor);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: "); // Print out the topic
Serial.println(topic);
String stop_led_string;
String photoresistor_string;
if(String(topic) == "pat_photoresistor") {// проверяем из нужного ли нам топика пришли данные
for(int b=0;b<length;b++){
photoresistor_string=photoresistor_string+(char)payload[b];
}
Serial.print("photoresistor:");
Serial.println(photoresistor);
Serial.println(photoresistor_string);
photoresistor1=photoresistor_string.toInt();
}
if(String(topic) == "0start1") {// проверяем из нужного ли нам топика пришли данные
String strPayload = String((char)payload[0]); // преобразуем полученные данные в тип
switch (int i=strPayload.toInt()) {
case 0:
Serial.println("Off");
intOnOFF=0;
break;
case 1:
Serial.println("Auto");
intOnOFF=1;
break;
case 2:
Serial.println("On idiod");
intOnOFF=2;
break;
}
}
if (String(topic) == "led01"){
for (int z = 0; z < length; z++) {
stop_led_string=stop_led_string+(char)payload[z];
}
Serial.print("stop_led_string");
Serial.println(stop_led);
stop_led=stop_led_string.toInt()*1000;
}
Serial.println("-----------------------");
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP22", "pat_den1ad", "karusel213" )) { //Sign in to the broker
Serial.println("Connected to broker");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}