#include #include #include "Barometer.h" #include #include "SPI.h" #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" // Pentru uPesy_esp32_wroom_devkit //BMP180 //SDA = 21 //SCL = 22 // TFT pins // SS = 5; // MOSI = 23; // MISO = 19; // SCK = 18; // DC = 2 // RST -> 13 // 3.3V // Goes to TFT LED // 5v // Goes to TFT Vcc // Gnd // Goes to TFT Gnd #ifdef LED #undef LED #endif #define LED 23 // GPIO 5 (D1) for LED bool led_State = false; #define REL1 32 // GPIO 5 (D1) for LED bool rel1_State = false; #define REL2 33 // GPIO 5 (D1) for LED bool rel2_State = false; #define REL3 25 // GPIO 5 (D1) for LED bool rel3_State = false; #define REL4 26 // GPIO 5 (D1) for LED bool rel4_State = false; float temperature, pressure, atm, altitude; unsigned long ts, perioada=1000; // WiFi settings const char *ssid = "Acasuca_2.4G"; // Replace with your WiFi name const char *password = "kNY2ph3mmrnT6zk4"; // Replace with your WiFi password // MQTT Broker settings const char *mqtt_broker = "masserv.utcluj.ro"; // masserv broker endpoint const char *mqtt_topic = "mass/esp32"; // MQTT topic const char *mqtt_topic_led = "mass/esp32/led"; // MQTT topic const char *mqtt_topic_t = "mass/esp32/temp"; // MQTT topic const char *mqtt_topic_p = "mass/esp32/pres"; // MQTT topic const char *mqtt_topic_a = "mass/esp32/atm"; // MQTT topic const char *mqtt_topic_r1 = "mass/esp32/rel1"; // MQTT topic const char *mqtt_topic_r2 = "mass/esp32/rel2"; // MQTT topic const char *mqtt_topic_r3 = "mass/esp32/rel3"; // MQTT topic const char *mqtt_topic_r4 = "mass/esp32/rel4"; // MQTT topic const char *mqtt_username = "iot_client"; // MQTT username for authentication const char *mqtt_password = "ASA123"; // MQTT password for authentication const int mqtt_port = 1883; // MQTT port (TCP) Barometer myBarometer; // Use hardware SPI (on ESP D4 and D8 as above) Adafruit_ILI9341 tft = Adafruit_ILI9341(5, 2, 13); // If using the breakout, change pins as desired //Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _mosi, _sclk, _rst); WiFiClient espClient; PubSubClient mqtt_client(espClient); void connectToWiFi(); void connectToMQTTBroker(); void mqttCallback(char *topic, byte *payload, unsigned int length); void setup() { Serial.begin(115200); Serial.println("Start tft.begin"); tft.begin(); // Serial.println("Start tft test"); //Test display tft.fillScreen(ILI9341_BLACK); delay(1000); /* tft.fillScreen(ILI9341_RED); delay(2000); tft.fillScreen(ILI9341_GREEN); delay(2000); tft.fillScreen(ILI9341_BLUE); delay(2000); Serial.println("Stop tft test"); */ pinMode(LED, OUTPUT); digitalWrite(LED, LOW); pinMode(REL1, OUTPUT); digitalWrite(REL1, LOW); pinMode(REL2, OUTPUT); digitalWrite(REL2, LOW); pinMode(REL3, OUTPUT); digitalWrite(REL3, LOW); pinMode(REL4, OUTPUT); digitalWrite(REL4, LOW); connectToWiFi(); mqtt_client.setServer(mqtt_broker, mqtt_port); mqtt_client.setCallback(mqttCallback); connectToMQTTBroker(); //myBarometer.init(); }//end setup void connectToWiFi() { WiFi.begin(ssid, password); Serial.print("Connecting to WiFi"); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to the WiFi network"); } void connectToMQTTBroker() { while (!mqtt_client.connected()) { String client_id = "esp32-client-" + String(WiFi.macAddress()); Serial.printf("Connecting to MQTT Broker as %s.....\n", client_id.c_str()) ; if (mqtt_client.connect(client_id.c_str(), mqtt_username, mqtt_password)) { Serial.println("Connected to MQTT broker"); mqtt_client.subscribe(mqtt_topic); mqtt_client.subscribe(mqtt_topic_t); mqtt_client.subscribe(mqtt_topic_p); mqtt_client.subscribe(mqtt_topic_a); mqtt_client.subscribe(mqtt_topic_led); mqtt_client.subscribe(mqtt_topic_r1); mqtt_client.subscribe(mqtt_topic_r2); mqtt_client.subscribe(mqtt_topic_r3); mqtt_client.subscribe(mqtt_topic_r4); // Publish message upon successful connection mqtt_client.publish(mqtt_topic, "Hi mass I'm ESP32 4 Channels Relay ^^"); } else { Serial.print("Failed to connect to MQTT broker, rc="); Serial.print(mqtt_client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void mqttCallback(char *topic, byte *payload, unsigned int length) { Serial.print("Message received on topic: "); Serial.println(topic); // Serial.println("Message:"); String message; for (int i = 0; i < length; i++) { message += (char) payload[i]; // Convert *byte to string } Serial.print("Message:"); Serial.println(message); if(strcmp(topic, mqtt_topic_t)==0){ // Read Temperature if "temp" message is received if (message == "temp") { mqtt_client.publish(mqtt_topic_t, String(temperature).c_str(), true); Serial.print("Temperature is:"); Serial.println(temperature); } //end topic temp } else if(strcmp(topic, mqtt_topic_p)==0){ if (message == "pa") { mqtt_client.publish(mqtt_topic_p, String(pressure).c_str(), true); Serial.print("Pressure in Pa is:"); Serial.println(pressure); } //end topic pres } else if(strcmp(topic, mqtt_topic_a)==0){ if (message == "atm") { mqtt_client.publish(mqtt_topic_a, String(atm).c_str(), true); Serial.print("Pressure in atm is:"); Serial.println(atm); } //end topic atm } else if(strcmp(topic, mqtt_topic_led)==0){ // LED Control the LED based on the message received if (message == "ledon" && !led_State) { digitalWrite(LED, HIGH); // Turn on the LED led_State = true; mqtt_client.publish(mqtt_topic_led, String("ledon").c_str(), false); Serial.println("LED is turned on"); } if (message == "ledoff" && led_State) { digitalWrite(LED, LOW); // Turn off the LED led_State = false; mqtt_client.publish(mqtt_topic_led, String("ledoff").c_str(), false); Serial.println("LED is turned off"); } //end topic LED } else if(strcmp(topic, mqtt_topic_r1)==0){ // R1 Control the Relay based on the message received if (message == "r1on" && !rel1_State) { digitalWrite(REL1, HIGH); // Turn on the LED rel1_State = true; mqtt_client.publish(mqtt_topic_r1, String("on").c_str(), false); Serial.println("Rel1 is turned on"); } if (message == "r1off" && rel1_State) { digitalWrite(REL1, LOW); // Turn off the LED rel1_State = false; mqtt_client.publish(mqtt_topic_r1, String("off").c_str(), false); Serial.println("Rel1 is turned off"); } //end topic r1 } else if(strcmp(topic, mqtt_topic_r2)==0){ // R2 Control the Relay based on the message received if (message == "r2on" && !rel2_State) { digitalWrite(REL2, HIGH); // Turn on the LED rel2_State = true; mqtt_client.publish(mqtt_topic_r2, String("on").c_str(), false); Serial.println("Rel2 is turned on"); } if (message == "r2off" && rel2_State ) { digitalWrite(REL2, LOW); // Turn off the LED rel2_State = false; mqtt_client.publish(mqtt_topic_r2, String("off").c_str(), false); Serial.println("Rel2 is turned off"); } //end topic r2 } else if(strcmp(topic, mqtt_topic_r3)==0){ // R3 Control the Relay based on the message received if (message == "r3on" && !rel3_State) { digitalWrite(REL3, HIGH); // Turn on the LED rel3_State = true; mqtt_client.publish(mqtt_topic_r3, String("on").c_str(), false); Serial.println("Rel3 is turned on"); } if (message == "r3off" && rel3_State) { digitalWrite(REL3, LOW); // Turn off the LED rel3_State = false; mqtt_client.publish(mqtt_topic_r3, String("off").c_str(), false); Serial.println("Rel3 is turned off"); } //end topic r3 } else if(strcmp(topic, mqtt_topic_r4)==0){ // R4 Control the Relay based on the message received if (message == "r4on" && !rel4_State) { digitalWrite(REL4, HIGH); // Turn on the LED rel4_State = true; mqtt_client.publish(mqtt_topic_r4, String("on").c_str(), false); Serial.println("Rel4 is turned on"); } if (message == "r4off" && rel4_State) { digitalWrite(REL4, LOW); // Turn off the LED rel4_State = false; mqtt_client.publish(mqtt_topic_r4, String("off").c_str(), false); Serial.println("Rel4 is turned off"); } } //end topic r4 // Serial.println(); Serial.println("-----------------------"); } void citireTempPres(){ //Get the temperature, bmp085ReadUT MUST be called first temperature = myBarometer.bmp085GetTemperature(myBarometer.bmp085ReadUT()); pressure = myBarometer.bmp085GetPressure(myBarometer.bmp085ReadUP());//Get the temperature altitude = myBarometer.calcAltitude(pressure); //Uncompensated caculation - in Meters atm = pressure / 101325; Serial.print("Temperature: "); Serial.print(temperature, 2); //display 2 decimal places Serial.println("deg C"); Serial.print("Pressure: "); Serial.print(pressure, 0); //whole number only. Serial.println(" Pa"); Serial.print("Ralated Atmosphere: "); Serial.println(atm, 4); //display 4 decimal places Serial.print("Altitude: "); Serial.print(altitude, 2); //display 2 decimal places Serial.println(" m"); Serial.println(); }//end citireTempPres void loop() { if((millis() - ts) >= perioada) { ts = millis(); //citireTempPres(); } if (!mqtt_client.connected()) { connectToMQTTBroker(); } mqtt_client.loop(); }//end loop