#include #include #include "SPI.h" #include "Adafruit_GFX.h" #include "Adafruit_ILI9341.h" // Use hardware SPI (on ESP D4 and D8 as above) Adafruit_ILI9341 tft = Adafruit_ILI9341(5, 2); // If using the breakout, change pins as desired //Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _mosi, _sclk, _rst); // Pentru uPesy_esp32_wroom_devkit // SS = 5; // MOSI = 23; // MISO = 19; // SCK = 18; // DC = 2 // RST -> ESP32 RST // 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; // 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"; // EMQX broker endpoint const char *mqtt_topic = "mass/esp8266"; // 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) 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); yield(); tft.fillScreen(ILI9341_RED); yield(); tft.fillScreen(ILI9341_GREEN); yield(); tft.fillScreen(ILI9341_BLUE); /* tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); tft.setTextColor(ILI9341_WHITE); tft.setTextSize(1); tft.println("Hello World!"); tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2); tft.println(1234.56); tft.setTextColor(ILI9341_RED); tft.setTextSize(3); tft.println(0xDEADBEEF, HEX); tft.println(); tft.setTextColor(ILI9341_GREEN); tft.setTextSize(5); tft.println("Groop"); tft.setTextSize(2); tft.println("I implore thee,"); tft.setTextSize(1); tft.println("my foonting turlingdromes."); tft.println("And hooptiously drangle me"); tft.println("with crinkly bindlewurdles,"); tft.println("Or I will rend thee"); tft.println("in the gobberwarts"); tft.println("with my blurglecruncheon,"); tft.println("see if I don't!"); */ 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(); } 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); // 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.print("Message:"); String message; for (int i = 0; i < length; i++) { message += (char) payload[i]; // Convert *byte to string } // Read Temperature if "temp" message is received if (message == "temp") { int temperature = analogRead(A0); mqtt_client.publish(mqtt_topic, String(temperature).c_str(), true); Serial.print("Temperature is:"); Serial.println(temperature); } // LED Control the LED based on the message received if (message == "ledon" && !led_State) { digitalWrite(LED, HIGH); // Turn on the LED led_State = true; Serial.println("LED is turned on"); } if (message == "ledoff" && led_State) { digitalWrite(LED, LOW); // Turn off the LED led_State = false; Serial.println("LED is turned off"); } // R1 Control the Relay based on the message received if (message == "onr1" && !rel1_State) { digitalWrite(REL1, HIGH); // Turn on the LED rel1_State = true; Serial.println("Rel1 is turned on"); } if (message == "offr1" && rel1_State) { digitalWrite(REL1, LOW); // Turn off the LED rel1_State = false; Serial.println("Rel1 is turned off"); } // R2 Control the Relay based on the message received if (message == "onr2" && !rel2_State) { digitalWrite(REL2, HIGH); // Turn on the LED rel2_State = true; Serial.println("Rel2 is turned on"); } if (message == "offr2" && rel2_State ) { digitalWrite(REL2, LOW); // Turn off the LED rel2_State = false; Serial.println("Rel2 is turned off"); } // R3 Control the Relay based on the message received if (message == "onr3" && !rel3_State) { digitalWrite(REL3, HIGH); // Turn on the LED rel3_State = true; Serial.println("Rel3 is turned on"); } if (message == "offr3" && rel3_State) { digitalWrite(REL3, LOW); // Turn off the LED rel3_State = false; Serial.println("Rel3 is turned off"); } // R4 Control the Relay based on the message received if (message == "onr4" && !rel4_State) { digitalWrite(REL4, HIGH); // Turn on the LED rel4_State = true; Serial.println("Rel4 is turned on"); } if (message == "offr4" && rel4_State) { digitalWrite(REL4, LOW); // Turn off the LED rel4_State = false; Serial.println("Rel4 is turned off"); } Serial.println(); Serial.println("-----------------------"); } void loop() { if (!mqtt_client.connected()) { connectToMQTTBroker(); } mqtt_client.loop(); }