2016-03-03 23:19:56 +01:00
|
|
|
/*
|
|
|
|
* mqttclient.cpp
|
|
|
|
*
|
|
|
|
* Created on: 03.03.2016
|
|
|
|
* Author: wn
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mqttclient.h"
|
|
|
|
|
2016-07-12 12:36:50 +02:00
|
|
|
#include <avr/wdt.h>
|
2016-03-03 23:19:56 +01:00
|
|
|
#include <Streaming.h>
|
2016-03-06 15:18:46 +01:00
|
|
|
#include <WiFi.h>
|
2016-10-29 23:45:24 +02:00
|
|
|
#include <WiFiUdp.h>
|
2016-03-03 23:19:56 +01:00
|
|
|
#include <Metro.h>
|
|
|
|
#include <PubSubClient.h>
|
2016-03-04 14:30:30 +01:00
|
|
|
#include "hmi.h"
|
2016-03-03 23:19:56 +01:00
|
|
|
|
|
|
|
|
2016-03-06 15:20:08 +01:00
|
|
|
|
2016-03-04 14:30:30 +01:00
|
|
|
extern Hmi hmi;
|
|
|
|
|
2016-07-10 18:38:53 +02:00
|
|
|
static const char MESSAGE_TOPIC[] = "IoT/Monitor/Message";
|
|
|
|
static const char ALARM_TOPIC[] = "IoT/Monitor/Alarm";
|
2016-07-11 21:51:33 +02:00
|
|
|
static const char SECONDS_TOPIC[] = "IoT/SecondsSinceMidnight";
|
2016-07-12 12:36:50 +02:00
|
|
|
static const char WATCHDOG_TOPIC[] = "IoT/Watchdog";
|
2016-07-11 21:51:33 +02:00
|
|
|
|
2016-03-04 14:30:30 +01:00
|
|
|
|
2016-03-03 23:19:56 +01:00
|
|
|
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length);
|
|
|
|
|
2016-03-06 15:18:46 +01:00
|
|
|
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);
|
2016-03-03 23:19:56 +01:00
|
|
|
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;
|
2016-03-04 14:30:30 +01:00
|
|
|
|
|
|
|
if (!(strcmp(topic, MESSAGE_TOPIC) && strcmp(topic, ALARM_TOPIC))) {
|
2016-03-06 15:18:46 +01:00
|
|
|
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);
|
2016-03-04 14:30:30 +01:00
|
|
|
}
|
2016-03-06 15:18:46 +01:00
|
|
|
}
|
2016-07-11 21:51:33 +02:00
|
|
|
} else if (!strcmp(topic, SECONDS_TOPIC)) {
|
|
|
|
uint32_t seconds = atol(buffer);
|
|
|
|
hmi.setSeconds(seconds);
|
2016-07-12 12:36:50 +02:00
|
|
|
} else if (!strcmp(topic, WATCHDOG_TOPIC)) {
|
|
|
|
wdt_reset();
|
2016-03-04 14:30:30 +01:00
|
|
|
} else {
|
|
|
|
Serial << "Strange, unknown topic received" << endl;
|
|
|
|
}
|
2016-03-03 23:19:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:18:46 +01:00
|
|
|
void printWifiStatus() {
|
2016-03-06 15:45:33 +01:00
|
|
|
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;
|
2016-03-06 15:18:46 +01:00
|
|
|
}
|
2016-03-03 23:19:56 +01:00
|
|
|
|
|
|
|
void MqttClientNS::begin() {
|
2016-03-06 15:18:46 +01:00
|
|
|
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) {
|
2016-03-06 15:45:33 +01:00
|
|
|
Serial << "WiFi shield not present" << endl;
|
|
|
|
*(hmi.tft()) << "WiFi shield not present" << endl;
|
2016-03-06 15:18:46 +01:00
|
|
|
while(true);
|
|
|
|
}
|
2016-03-03 23:19:56 +01:00
|
|
|
|
2016-03-06 15:18:46 +01:00
|
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
while ( status != WL_CONNECTED) {
|
2016-03-06 15:45:33 +01:00
|
|
|
Serial << "Attempting to connect to SSID: " << ssid << endl;
|
|
|
|
*(hmi.tft()) << "Attempting to connect to SSID: " << ssid << endl;
|
2016-03-06 15:18:46 +01:00
|
|
|
status = WiFi.begin(ssid, pass);
|
2016-03-03 23:19:56 +01:00
|
|
|
|
2016-03-06 15:18:46 +01:00
|
|
|
// wait 10 seconds for connection:
|
|
|
|
delay(10000);
|
2016-03-03 23:19:56 +01:00
|
|
|
}
|
2016-03-06 15:45:33 +01:00
|
|
|
Serial << "Connected." << endl;
|
|
|
|
*(hmi.tft()) << "Connected." << endl;
|
2016-03-06 15:18:46 +01:00
|
|
|
|
2016-03-06 15:45:33 +01:00
|
|
|
printWifiStatus();
|
2016-03-06 15:18:46 +01:00
|
|
|
|
|
|
|
disconnectState = 3;
|
|
|
|
disconnectTime = millis();
|
2016-03-03 23:19:56 +01:00
|
|
|
}
|
2016-03-06 15:18:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MqttClientNS::exec() {
|
2016-07-10 18:38:53 +02:00
|
|
|
// if (minute.check() == 1) {
|
|
|
|
// byte r = Ethernet.maintain();
|
|
|
|
// Serial << "Ethernet.maintain: " << r << endl;
|
|
|
|
// if ((r == DHCP_CHECK_REBIND_FAIL) || (r == DHCP_CHECK_RENEW_FAIL)) {
|
|
|
|
// }
|
|
|
|
// }
|
2016-03-03 23:19:56 +01:00
|
|
|
|
|
|
|
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")) {
|
2016-03-04 14:30:30 +01:00
|
|
|
mqttClient.subscribe(MESSAGE_TOPIC);
|
|
|
|
mqttClient.subscribe(ALARM_TOPIC);
|
2016-07-11 21:51:33 +02:00
|
|
|
mqttClient.subscribe(SECONDS_TOPIC);
|
2016-07-12 12:36:50 +02:00
|
|
|
mqttClient.subscribe(WATCHDOG_TOPIC);
|
2016-03-03 23:19:56 +01:00
|
|
|
disconnectTime = millis();
|
2016-07-10 18:38:53 +02:00
|
|
|
mqttClient.publish("IoT/Monitor/Started", "monitor started");
|
2016-03-03 23:19:56 +01:00
|
|
|
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("}}");
|
2016-07-12 12:36:50 +02:00
|
|
|
mqttClient.publish("IoT/Heartbeat/Monitor", (char*)msg.c_str());
|
2016-03-03 23:19:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|