Switched to WiFi
This commit is contained in:
parent
bb816775b6
commit
4ac4ca4281
@ -25,8 +25,6 @@
|
||||
<option id="it.baeyens.arduino.compiler.cpp.sketch.option.incpath.660768886" name="Include Paths (-I)" superClass="it.baeyens.arduino.compiler.cpp.sketch.option.incpath" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/arduino/core}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/arduino/variant}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/Ethernet}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/Ethernet/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/SPI}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Adafruit-GFX}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Metro}""/>
|
||||
@ -34,6 +32,8 @@
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Streaming}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/TFT_ILI9341}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Touch-Screen}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/WiFi/src}""/>
|
||||
</option>
|
||||
<inputType id="it.baeyens.arduino.compiler.cpp.sketch.input.505756577" name="CPP source files" superClass="it.baeyens.arduino.compiler.cpp.sketch.input"/>
|
||||
</tool>
|
||||
@ -41,8 +41,6 @@
|
||||
<option id="it.baeyens.arduino.compiler.c.sketch.option.incpath.2126801261" name="Include Paths (-I)" superClass="it.baeyens.arduino.compiler.c.sketch.option.incpath" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/arduino/core}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/arduino/variant}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/Ethernet}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/Ethernet/src}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/SPI}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Adafruit-GFX}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Metro}""/>
|
||||
@ -50,6 +48,8 @@
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Streaming}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/TFT_ILI9341}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Touch-Screen}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Libraries/WiFi/src}""/>
|
||||
</option>
|
||||
<inputType id="it.baeyens.arduino.compiler.c.sketch.input.575652065" name="C Source Files" superClass="it.baeyens.arduino.compiler.c.sketch.input"/>
|
||||
</tool>
|
||||
|
10
.project
10
.project
@ -26,16 +26,16 @@
|
||||
<nature>it.baeyens.arduinonature</nature>
|
||||
</natures>
|
||||
<linkedResources>
|
||||
<link>
|
||||
<name>Libraries/Ethernet</name>
|
||||
<type>2</type>
|
||||
<locationURI>ArduinoLibPath/Ethernet</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>Libraries/SPI</name>
|
||||
<type>2</type>
|
||||
<locationURI>ArduinoHardwareLibPath/SPI</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>Libraries/WiFi</name>
|
||||
<type>2</type>
|
||||
<locationURI>ArduinoLibPath/WiFi</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>arduino/core</name>
|
||||
<type>2</type>
|
||||
|
@ -154,10 +154,10 @@ environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.DTS/value=0
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/value=1457087444
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/value=1457277262
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/value=1457083844
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/value=1457273662
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/value=3600
|
||||
@ -370,7 +370,7 @@ environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.BUILD_VARIANT/value=mega
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/value=/dev/tty.usbmodem33
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/value=/dev/tty.usbmodem36
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/value=Default
|
||||
|
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user