Ethernet stuff

This commit is contained in:
Wolfgang Hottgenroth
2016-10-30 00:35:25 +02:00
parent 7ec78fbb6d
commit e7f3a81173
21 changed files with 3198 additions and 58 deletions

View File

@ -9,8 +9,7 @@
#include <avr/wdt.h>
#include <Streaming.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Ethernet.h>
#include <Metro.h>
#include <PubSubClient.h>
#include "hmi.h"
@ -32,11 +31,11 @@ char ssid[] = "MessWLAN";
char pass[] = "UNVmpwbr6heQnMQ7ykXT";
const uint8_t WIFI_ENABLE_PIN = 3;
// static uint8_t MAC[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x08 };
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);
EthernetClient client;
PubSubClient mqttClient = PubSubClient(BROKER, 1883, callback, client);
uint8_t disconnectState = 0;
uint32_t disconnectTime = 0;
Metro minute = Metro(60000);
@ -92,57 +91,60 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
}
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 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);
// 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();
}
Ethernet.begin(MAC);
Serial << "Got IP address: " << Ethernet.localIP() << endl;
*(hmi.tft()) << Ethernet.localIP() << endl;
disconnectState = 3;
disconnectTime = millis();
// 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() {