Switched to WiFi

This commit is contained in:
Wolfgang Hottgenroth 2016-03-06 15:18:46 +01:00
parent bb816775b6
commit 4ac4ca4281
4 changed files with 95 additions and 49 deletions

View File

@ -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="&quot;${workspace_loc:/MqttMonitor/arduino/core}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/arduino/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/Ethernet}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/Ethernet/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/SPI}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Adafruit-GFX}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Metro}&quot;"/>
@ -34,6 +32,8 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Streaming}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/TFT_ILI9341}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Touch-Screen}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/WiFi}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/WiFi/src}&quot;"/>
</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="&quot;${workspace_loc:/MqttMonitor/arduino/core}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/arduino/variant}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/Ethernet}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/Ethernet/src}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/SPI}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Adafruit-GFX}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Metro}&quot;"/>
@ -50,6 +48,8 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Streaming}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/TFT_ILI9341}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Touch-Screen}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/WiFi}&quot;"/>
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/MqttMonitor/Libraries/WiFi/src}&quot;"/>
</option>
<inputType id="it.baeyens.arduino.compiler.c.sketch.input.575652065" name="C Source Files" superClass="it.baeyens.arduino.compiler.c.sketch.input"/>
</tool>

View File

@ -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>

View File

@ -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

View File

@ -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(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
headerStr = strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
bodyStr = strsep(&paramPtr, " ");
}
char *paramPtr = buffer;
char *slotStr = 0;
char *headerStr = 0;
char *bodyStr = 0;
if ((paramPtr != 0) && (*paramPtr != 0)){
slotStr = strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
headerStr = strsep(&paramPtr, " ");
}
if ((paramPtr != 0) && (*paramPtr != 0)){
bodyStr = strsep(&paramPtr, " ");
}
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;