Switched to WiFi
This commit is contained in:
120
mqttclient.cpp
120
mqttclient.cpp
@ -8,7 +8,8 @@
|
||||
#include "mqttclient.h"
|
||||
|
||||
#include <Streaming.h>
|
||||
#include <Ethernet.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUDP.h>
|
||||
#include <Metro.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "hmi.h"
|
||||
@ -22,10 +23,15 @@ static const char ALARM_TOPIC[] = "IoT/Alarm/Monitor";
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
static uint8_t MAC[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x08 };
|
||||
const static char BROKER[] = "192.168.75.1";
|
||||
EthernetClient ethernetClient;
|
||||
PubSubClient mqttClient = PubSubClient(BROKER, 1883, callback, ethernetClient);
|
||||
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);
|
||||
@ -44,54 +50,94 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
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, " ");
|
||||
}
|
||||
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)) {
|
||||
if ((slotStr != 0) && (*slotStr != 0) &&
|
||||
(headerStr != 0) && (*headerStr != 0) &&
|
||||
(bodyStr != 0) && (*bodyStr != 0)) {
|
||||
|
||||
uint8_t slot = atoi(slotStr);
|
||||
uint8_t slot = atoi(slotStr);
|
||||
|
||||
if (! strcmp(topic, ALARM_TOPIC)) {
|
||||
Serial << "Alarm" << endl;
|
||||
} else if (! strcmp(topic, MESSAGE_TOPIC)) {
|
||||
hmi.updateMessage(slot, headerStr, bodyStr);
|
||||
if (! strcmp(topic, ALARM_TOPIC)) {
|
||||
Serial << "Alarm" << endl;
|
||||
} else if (! strcmp(topic, MESSAGE_TOPIC)) {
|
||||
hmi.updateMessage(slot, headerStr, bodyStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Serial << "Strange, unknown topic received" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printWifiStatus() {
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial.print("signal strength (RSSI):");
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
|
||||
void MqttClientNS::begin() {
|
||||
Ethernet.begin(MAC);
|
||||
Serial << "Got IP address: " << Ethernet.localIP() << endl;
|
||||
pinMode(WIFI_ENABLE_PIN, INPUT_PULLUP);
|
||||
delay(500);
|
||||
bool wifiEnabled = (digitalRead(WIFI_ENABLE_PIN) != 0);
|
||||
|
||||
disconnectState = 3;
|
||||
disconnectTime = millis();
|
||||
// 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.println("WiFi shield not present");
|
||||
while(true);
|
||||
}
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial.print("Attempting to connect to SSID: ");
|
||||
Serial.println(ssid);
|
||||
status = WiFi.begin(ssid, pass);
|
||||
|
||||
// wait 10 seconds for connection:
|
||||
delay(10000);
|
||||
}
|
||||
Serial.println("Connected.");
|
||||
|
||||
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 (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;
|
||||
|
Reference in New Issue
Block a user