/* * mqttclient.cpp * * Created on: 03.03.2016 * Author: wn */ #include "mqttclient.h" #include #include #include #include #include #include #include "hmi.h" extern Hmi hmi; static const char MESSAGE_TOPIC[] = "IoT/Monitor/Message"; static const char ALARM_TOPIC[] = "IoT/Monitor/Alarm"; static const char SECONDS_TOPIC[] = "IoT/SecondsSinceMidnight"; static const char WATCHDOG_TOPIC[] = "IoT/Watchdog"; void callback(char* topic, byte* payload, unsigned int length); char ssid[] = "MessWLAN"; char pass[] = "UNVmpwbr6heQnMQ7ykXT"; const uint8_t WIFI_ENABLE_PIN = 3; // static uint8_t MAC[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x08 }; // const static char BROKER[] = "192.168.75.1"; const static char BROKER[] = "mqttbroker"; WiFiClient wifiClient; PubSubClient mqttClient = PubSubClient(BROKER, 1883, callback, wifiClient); uint8_t disconnectState = 0; uint32_t disconnectTime = 0; Metro minute = Metro(60000); Metro second = Metro(1000); uint32_t uptime; void callback(char* topic, byte* payload, unsigned int length) { const uint8_t BUFSIZE = 128; if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL Serial << "Received message too long, ignore it" << endl; } else { char buffer[BUFSIZE]; memcpy(buffer, payload, length); *(buffer + length) = 0; Serial << "Received message: " << length << ", " << String(topic) << ", " << String(buffer) << endl; if (!(strcmp(topic, MESSAGE_TOPIC) && strcmp(topic, ALARM_TOPIC))) { char *paramPtr = buffer; char *slotStr = 0; char *headerStr = 0; char *bodyStr = 0; if ((paramPtr != 0) && (*paramPtr != 0)){ slotStr = strsep(¶mPtr, " "); } if ((paramPtr != 0) && (*paramPtr != 0)){ headerStr = strsep(¶mPtr, " "); } if ((paramPtr != 0) && (*paramPtr != 0)){ bodyStr = strsep(¶mPtr, " "); } if ((slotStr != 0) && (*slotStr != 0) && (headerStr != 0) && (*headerStr != 0) && (bodyStr != 0) && (*bodyStr != 0)) { uint8_t slot = atoi(slotStr); if (! strcmp(topic, ALARM_TOPIC)) { Serial << "Alarm" << endl; } else if (! strcmp(topic, MESSAGE_TOPIC)) { hmi.updateMessage(slot, headerStr, bodyStr); } } } else if (!strcmp(topic, SECONDS_TOPIC)) { uint32_t seconds = atol(buffer); hmi.setSeconds(seconds); } else if (!strcmp(topic, WATCHDOG_TOPIC)) { wdt_reset(); } else { Serial << "Strange, unknown topic received" << endl; } } } void printWifiStatus() { char buffer[16]; // print the SSID of the network you're attached to: Serial << "SSID: " << WiFi.SSID() << endl; *(hmi.tft()) << "SSID: " << WiFi.SSID() << endl; // print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial << "IP Address: " << ip << endl; *(hmi.tft()) << "IP Address: " << ip << endl; // print the received signal strength: long rssi = WiFi.RSSI(); Serial << "signal strength (RSSI):" << rssi << " dBm" << endl; *(hmi.tft()) << "signal strength (RSSI):" << rssi << " dBm" << endl; } void MqttClientNS::begin() { pinMode(WIFI_ENABLE_PIN, INPUT_PULLUP); delay(500); bool wifiEnabled = (digitalRead(WIFI_ENABLE_PIN) != 0); // Ethernet.begin(MAC); // Serial << "Got IP address: " << Ethernet.localIP() << endl; if (wifiEnabled) { // check for the presence of the shield: if (WiFi.status() == WL_NO_SHIELD) { Serial << "WiFi shield not present" << endl; *(hmi.tft()) << "WiFi shield not present" << endl; while(true); } int status = WL_IDLE_STATUS; while ( status != WL_CONNECTED) { Serial << "Attempting to connect to SSID: " << ssid << endl; *(hmi.tft()) << "Attempting to connect to SSID: " << ssid << endl; status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } Serial << "Connected." << endl; *(hmi.tft()) << "Connected." << endl; printWifiStatus(); disconnectState = 3; disconnectTime = millis(); } } void MqttClientNS::exec() { // if (minute.check() == 1) { // byte r = Ethernet.maintain(); // Serial << "Ethernet.maintain: " << r << endl; // if ((r == DHCP_CHECK_REBIND_FAIL) || (r == DHCP_CHECK_RENEW_FAIL)) { // } // } if ((disconnectState == 0) && (! mqttClient.loop())) { disconnectState = 1; } switch (disconnectState) { case 0: // Serial.println("discState 0"); // everything fine break; case 1: Serial.println("discState 1"); mqttClient.disconnect(); disconnectTime = millis(); disconnectState = 2; break; case 2: Serial.println("discState 3"); if (disconnectTime + 2000 < millis()) { disconnectState = 3; } break; case 3: Serial.println("discState 3"); if (mqttClient.connect("Monitor")) { mqttClient.subscribe(MESSAGE_TOPIC); mqttClient.subscribe(ALARM_TOPIC); mqttClient.subscribe(SECONDS_TOPIC); mqttClient.subscribe(WATCHDOG_TOPIC); disconnectTime = millis(); mqttClient.publish("IoT/Monitor/Started", "monitor started"); disconnectState = 0; } else { disconnectState = 1; } break; default: disconnectState = 0; break; } if (second.check() == 1) { uptime++; Serial.println("mqtt tick"); if (disconnectState == 0) { String msg = String("{ \"metadata\": { \"device\": \"Monitor\" }, \"data\": { \"uptime\": ") + uptime + String("}}"); mqttClient.publish("IoT/Heartbeat/Monitor", (char*)msg.c_str()); } } }