From 72ec3a80fbedc1ba24cd278a4da50893964522f4 Mon Sep 17 00:00:00 2001 From: Domagoj Zecevic Date: Sun, 5 Feb 2023 00:10:47 +0100 Subject: [PATCH] working mqtt --- arduino_mqtt.ino | 108 ------------------- arduino_mqtt/.theia/launch.json | 8 ++ arduino_mqtt/arduino_mqtt.ino | 186 ++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+), 108 deletions(-) delete mode 100644 arduino_mqtt.ino create mode 100644 arduino_mqtt/.theia/launch.json create mode 100644 arduino_mqtt/arduino_mqtt.ino diff --git a/arduino_mqtt.ino b/arduino_mqtt.ino deleted file mode 100644 index f770dd4..0000000 --- a/arduino_mqtt.ino +++ /dev/null @@ -1,108 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#define OLED_RESET 0 // GPIO0 -Adafruit_SSD1306 display(OLED_RESET); - -#if (SSD1306_LCDHEIGHT != 48) -#error("Height incorrect, please fix Adafruit_SSD1306.h!"); -#endif - -const char* ssid = "IoT-Zecevic"; -const char* password = "IoTSifra123456!"; -const char* mqttServer = "192.168.1.11"; -const int mqttPort = 1883; -const char* mqttUser = "yourMQTTuser"; -const char* mqttPassword = "yourMQTTpassword"; - -WiFiClient espClient; -PubSubClient client(espClient); - -void callback(char* topic, byte* payload, unsigned int length) { - - Serial.print("Message arrived in topic: "); - Serial.println(topic); - - Serial.print("Message:"); - display.clearDisplay(); - display.setTextSize(1); - display.setTextColor(WHITE); - display.setCursor(0,0); - display.println("Power: "); - for (int i = 0; i < length; i++) { - Serial.print((char)payload[i]); - display.setTextSize(2); - display.print((char)payload[i]); - } - - display.display(); - Serial.println(); - Serial.println("-----------------------"); - -} - -void setup() { - - Serial.begin(115200); - - // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48) - // init done - - // Clear the buffer. - display.clearDisplay(); - - WiFi.begin(ssid, password); - - while (WiFi.status() != WL_CONNECTED) { - delay(500); - Serial.println("Connecting to WiFi.."); - } - Serial.println("Connected to WiFi"); - display.setTextSize(1); - display.setTextColor(WHITE); - display.setCursor(0,0); - display.println("Connected to the WiFi network!"); - display.display(); - - client.setServer(mqttServer, mqttPort); - client.setCallback(callback); - - while (!client.connected()) { - Serial.println("Connecting to MQTT..."); - - if (client.connect("ESP32Client", mqttUser, mqttPassword )) { - - Serial.println("connected"); - Serial.println("Connected to mqtt!"); - display.setTextSize(1); - display.setTextColor(WHITE); - display.println("Connected to mqtt!"); - display.println("No data yet"); - display.display(); - - } else { - - Serial.print("failed with state "); - Serial.print(client.state()); - delay(2000); - - } - } - //solar power - client.subscribe("solar/114182126368/0/power"); - - /*client.subscribe("tele/tasmota_server_C32969/SENSOR"); - client.subscribe("tele/tasmota_LR-C23E87/SENSOR"); - client.subscribe("tele/tasmota_OfficeNET_17A00F/SENSOR"); - client.subscribe("tele/tasmota_OfficePC1856E2/SENSOR");*/ - -} - -void loop() { - client.loop(); -} \ No newline at end of file diff --git a/arduino_mqtt/.theia/launch.json b/arduino_mqtt/.theia/launch.json new file mode 100644 index 0000000..7e4253b --- /dev/null +++ b/arduino_mqtt/.theia/launch.json @@ -0,0 +1,8 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + "version": "0.2.0", + "configurations": [ + + ] +} diff --git a/arduino_mqtt/arduino_mqtt.ino b/arduino_mqtt/arduino_mqtt.ino new file mode 100644 index 0000000..c0b635f --- /dev/null +++ b/arduino_mqtt/arduino_mqtt.ino @@ -0,0 +1,186 @@ +#include +#include +#include +#include +#include +#include +#include + +// Display Definition +#define OLED_RESET 0 // GPIO0 +Adafruit_SSD1306 display(OLED_RESET); +#if (SSD1306_LCDHEIGHT != 48) +#error("Height incorrect, please fix Adafruit_SSD1306.h!"); +#endif + +// DHT Definitnion +#define DHTPIN 2 +#define DHTTYPE DHT11 +DHT dht(DHTPIN, DHTTYPE); + +//Start web server +WiFiServer server(80); + +//Wifi Data +const char* ssid = "IoT-Zecevic"; +const char* password = "IoTSifra123456!"; +//MQTT data +const char* mqttServer = "192.168.1.11"; +const int mqttPort = 1883; +const char* mqttUser = "yourMQTTuser"; +const char* mqttPassword = "yourMQTTpassword"; + +//Diplay and DHT update data +long previousMillis = 0; +long interval = 2000; //ms + +//Global Vars +String global_power; +float humidity; +float temperature; + +//Start WiFi client +WiFiClient espClient; +PubSubClient MQTTclient(espClient); + +void callback(char* topic, byte* payload, unsigned int length) { + + Serial.print("Message arrived in topic: "); + Serial.println(topic); + + Serial.print("Message:"); + global_power = ""; + for (int i = 0; i < length; i++) { + Serial.print((char)payload[i]); + global_power += char(payload[i]); + } +} + +void showDisplay() { + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("Power: "); + display.setTextSize(2); + display.print(global_power); + display.println(); + display.setTextSize(1); + display.println("Temp: "); + display.setTextSize(2); + display.println(temperature); + display.display(); +} + +// prepare a web page to be send to a client (web browser) +String prepareHtmlPage() +{ + String htmlPage; + htmlPage.reserve(1024); // prevent ram fragmentation + htmlPage = F("HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n" // the connection will be closed after completion of the response + "Refresh: 5\r\n" // refresh the page automatically every 5 sec + "\r\n" + "" + "" + "

Power Producing: "); + htmlPage += global_power; + htmlPage += F("

" + "

Temperature: "); + htmlPage += temperature; + htmlPage += F("

" + "

Humidity: "); + htmlPage += humidity; + htmlPage += "

"; + htmlPage += F("" + "\r\n"); + return htmlPage; +} + +void setup() { + + Serial.begin(115200); + dht.begin(); + + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); + + display.clearDisplay(); + + WiFi.begin(ssid, password); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.println("Connecting to WiFi.."); + } + Serial.println("Connected to WiFi"); + + MQTTclient.setServer(mqttServer, mqttPort); + MQTTclient.setCallback(callback); + + while (!MQTTclient.connected()) { + Serial.println("Connecting to MQTT..."); + + if (MQTTclient.connect("ESP32Client", mqttUser, mqttPassword )) { + Serial.println("Connected to mqtt!"); + } else { + Serial.print("failed with state "); + Serial.print(MQTTclient.state()); + delay(2000); + } + } + //Get info from Solar + MQTTclient.subscribe("solar/114182126368/0/power"); + + server.begin(); + Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str()); + + //show 5 sec IP on display + display.clearDisplay(); + display.setTextSize(1); + display.setTextColor(WHITE); + display.setCursor(0,0); + display.println("IP addr: "); + display.println(WiFi.localIP().toString().c_str()); + display.display(); + delay(5000); +} + +void loop() { + MQTTclient.loop(); + WiFiClient serverClient = server.available(); + if (serverClient) + { + while (serverClient.connected()) + { + // read line by line what the client (web browser) is requesting + if (serverClient.available()) + { + String line = serverClient.readStringUntil('\r'); + if (line.length() == 1 && line[0] == '\n') + { + serverClient.println(prepareHtmlPage()); + break; + } + } + } + + while (serverClient.available()) { + serverClient.read(); + } + // close the connection: + serverClient.stop(); + } + String MQTTmessage; + char out[255]; + unsigned long currentMillis = millis(); + if(currentMillis - previousMillis > interval) { + previousMillis = currentMillis; + humidity = dht.readHumidity(); + temperature = dht.readTemperature(); + showDisplay(); + + MQTTclient.publish("my/office/Temperature", String(temperature).c_str()); + MQTTclient.publish("my/office/Humidity", String(humidity).c_str()); + } +} \ No newline at end of file