2015-05-06 19:08:30 +02:00
|
|
|
#include "WiModbusGateway.h"
|
|
|
|
|
2015-05-02 00:14:40 +02:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <WiFi.h>
|
2015-05-03 20:53:49 +02:00
|
|
|
#include <WiFiUDP.h>
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
#include "Metro.h"
|
2015-05-03 20:53:49 +02:00
|
|
|
// #include "Mudbus.h"
|
2015-05-02 00:14:40 +02:00
|
|
|
#include "LiquidCrystal.h"
|
|
|
|
#include "ModbusApp.h"
|
2015-05-03 20:53:49 +02:00
|
|
|
#include "PString.h"
|
|
|
|
#include "Streaming.h"
|
2015-05-06 19:08:30 +02:00
|
|
|
#include "PubSubClient.h"
|
2015-05-13 22:47:57 +02:00
|
|
|
#include <avr/wdt.h>
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
|
2015-05-18 22:07:33 +02:00
|
|
|
const uint8_t WIFI_ENABLE_PIN = 3;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
LiquidCrystal lcd = LiquidCrystal(A0, A1, A2, A3, A4, A5);
|
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
// Mudbus Mb;
|
2015-05-06 19:08:30 +02:00
|
|
|
// WiFiUDP udpSock;
|
|
|
|
WiFiClient wifiClient;
|
2015-05-06 19:21:33 +02:00
|
|
|
//byte server[] = { 192, 168, 87, 100 };
|
2015-05-13 22:47:57 +02:00
|
|
|
//byte server[] = { 172, 16, 2, 16 };
|
|
|
|
char server[] = "mqttbroker";
|
2015-05-06 19:08:30 +02:00
|
|
|
PubSubClient client(server, 1883, callback, wifiClient);
|
2015-05-07 08:56:47 +02:00
|
|
|
uint8_t disconnectState = 0;
|
|
|
|
uint32_t disconnectTime = 0;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
|
2015-05-06 19:21:33 +02:00
|
|
|
// char ssid[] = "Kinderland"; // your network SSID (name)
|
|
|
|
// char pass[] = "test1234"; // your network password
|
|
|
|
char ssid[] = "MessWLAN";
|
|
|
|
char pass[] = "UNVmpwbr6heQnMQ7ykXT";
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-13 22:47:57 +02:00
|
|
|
Metro minute = Metro(60000);
|
2015-05-02 00:14:40 +02:00
|
|
|
Metro second = Metro(1000);
|
2015-05-03 20:53:49 +02:00
|
|
|
uint32_t uptime;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
static bool wifiEnabled = false;
|
|
|
|
|
|
|
|
|
2015-05-06 19:08:30 +02:00
|
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
// handle message arrived
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-02 00:14:40 +02:00
|
|
|
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);
|
|
|
|
lcd.setCursor(0, 3);
|
|
|
|
lcd.print(ip); lcd.print(" ");
|
|
|
|
|
|
|
|
// print the received signal strength:
|
|
|
|
long rssi = WiFi.RSSI();
|
|
|
|
Serial.print("signal strength (RSSI):");
|
|
|
|
Serial.print(rssi);
|
|
|
|
Serial.println(" dBm");
|
|
|
|
lcd.print(rssi);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
2015-05-18 20:20:59 +02:00
|
|
|
pinMode(WIFI_ENABLE_PIN, INPUT_PULLUP);
|
2015-05-18 22:07:33 +02:00
|
|
|
delay(500);
|
|
|
|
wifiEnabled = (digitalRead(WIFI_ENABLE_PIN) != 0);
|
2015-05-18 20:20:59 +02:00
|
|
|
|
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
Serial.begin(57600);
|
2015-05-02 00:14:40 +02:00
|
|
|
while (!Serial) {
|
|
|
|
; // wait for serial port to connect. Needed for Leonardo only
|
|
|
|
}
|
|
|
|
|
|
|
|
lcd.begin(20, 4);
|
|
|
|
|
|
|
|
Serial.println("Starting ... ");
|
|
|
|
lcd.clear();
|
|
|
|
lcd.home();
|
|
|
|
lcd.print("Starting ...");
|
|
|
|
|
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
if (wifiEnabled) {
|
|
|
|
// check for the presence of the shield:
|
|
|
|
if (WiFi.status() == WL_NO_SHIELD) {
|
|
|
|
Serial.println("WiFi shield not present");
|
|
|
|
lcd.setCursor(0, 1);
|
|
|
|
lcd.print("WiFi shield not present");
|
|
|
|
// don't continue:
|
|
|
|
while(true);
|
|
|
|
}
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
int status = WL_IDLE_STATUS;
|
|
|
|
while ( status != WL_CONNECTED) {
|
|
|
|
Serial.print("Attempting to connect to SSID: ");
|
|
|
|
Serial.println(ssid);
|
|
|
|
lcd.setCursor(0, 1);
|
|
|
|
lcd.print("Trying: ");
|
|
|
|
lcd.print(ssid);
|
|
|
|
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
|
|
|
|
status = WiFi.begin(ssid, pass);
|
|
|
|
|
|
|
|
// wait 10 seconds for connection:
|
|
|
|
delay(10000);
|
|
|
|
}
|
|
|
|
Serial.println("Connected.");
|
|
|
|
lcd.setCursor(0, 2);
|
|
|
|
lcd.print("Connected.");
|
|
|
|
|
|
|
|
printWifiStatus();
|
|
|
|
|
|
|
|
// if (! client.connect("WiFiPowerMeter")) {
|
|
|
|
// Serial.println("MQTT broker not found");
|
|
|
|
// lcd.home();
|
|
|
|
// lcd.clear();
|
|
|
|
// lcd.print("MQTT broker not found");
|
|
|
|
// while (true);
|
|
|
|
// } else {
|
|
|
|
// lcd.print(" *");
|
|
|
|
// }
|
2015-05-02 00:14:40 +02:00
|
|
|
}
|
2015-05-06 19:08:30 +02:00
|
|
|
|
|
|
|
delay(1000);
|
2015-05-02 00:14:40 +02:00
|
|
|
lcd.home();
|
|
|
|
lcd.clear();
|
|
|
|
|
2015-05-07 08:56:47 +02:00
|
|
|
disconnectState = 3;
|
|
|
|
disconnectTime = millis();
|
|
|
|
|
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
// uptime = (uint32_t*) &(Mb.R[121]); // uptime
|
|
|
|
uptime = 0;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
// Mb.R[123] = 0xdead; // magic
|
|
|
|
// Mb.R[124] = 0xbeef;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-13 22:47:57 +02:00
|
|
|
wdt_enable(WDTO_8S);
|
|
|
|
|
2015-05-02 00:14:40 +02:00
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
// modbusAppBegin(&Mb);
|
|
|
|
modbusAppBegin();
|
2015-05-02 00:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void updateDisplay() {
|
|
|
|
static uint32_t lastUptime = 0;
|
2015-05-07 08:56:47 +02:00
|
|
|
static uint8_t lastDisconnectState = 0;
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
|
2015-05-07 08:56:47 +02:00
|
|
|
if ((disconnectState != lastDisconnectState) || (uptime != lastUptime)) {
|
2015-05-03 20:53:49 +02:00
|
|
|
lastUptime = uptime;
|
2015-05-07 08:56:47 +02:00
|
|
|
lastDisconnectState = disconnectState;
|
2015-05-02 00:14:40 +02:00
|
|
|
lcd.home(); lcd.clear();
|
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
uint32_t days = uptime / 86400;
|
|
|
|
uint32_t rem = uptime % 86400;
|
2015-05-02 00:14:40 +02:00
|
|
|
uint32_t hours = rem / 3600;
|
|
|
|
rem = rem % 3600;
|
|
|
|
uint32_t minutes = rem / 60;
|
|
|
|
uint32_t seconds = rem % 60;
|
|
|
|
lcd.setCursor(0,0);
|
2015-05-07 08:56:47 +02:00
|
|
|
lcd.print(days); lcd.print("/"); lcd.print(hours); lcd.print(":");
|
2015-05-02 00:14:40 +02:00
|
|
|
lcd.print(minutes); lcd.print(":"); lcd.print(seconds); lcd.print(" ");
|
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
if (wifiEnabled) {
|
|
|
|
long rssi = WiFi.RSSI();
|
|
|
|
lcd.print(rssi); lcd.print(" ");
|
|
|
|
uint8_t wifiStatus = WiFi.status();
|
|
|
|
lcd.print(wifiStatus);
|
|
|
|
lcd.print(disconnectState);
|
2015-05-18 20:20:59 +02:00
|
|
|
} else {
|
|
|
|
lcd.print("no wifi");
|
2015-05-18 12:36:47 +02:00
|
|
|
}
|
2015-05-02 00:14:40 +02:00
|
|
|
|
|
|
|
lcd.setCursor(0, 1);
|
|
|
|
lcd.print(getVoltage()); lcd.print("V");
|
|
|
|
lcd.setCursor(10, 1);
|
|
|
|
lcd.print(getCurrent()); lcd.print("A");
|
|
|
|
lcd.setCursor(0, 2);
|
2015-05-18 12:36:47 +02:00
|
|
|
lcd.print(getCosPhi()); lcd.print("");
|
2015-05-02 00:14:40 +02:00
|
|
|
lcd.setCursor(10, 2);
|
|
|
|
lcd.print(getPower()); lcd.print("W");
|
|
|
|
lcd.setCursor(0, 3);
|
2015-05-07 08:56:47 +02:00
|
|
|
lcd.print(getEnergy()); lcd.print(" "); lcd.print(getNewEnergy()); lcd.print(" ");
|
2015-05-02 00:14:40 +02:00
|
|
|
lcd.print("kWh");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2015-05-13 22:47:57 +02:00
|
|
|
wdt_reset();
|
|
|
|
|
2015-05-07 08:56:47 +02:00
|
|
|
updateDisplay();
|
|
|
|
|
2015-05-02 00:14:40 +02:00
|
|
|
modbusAppExec();
|
2015-05-03 20:53:49 +02:00
|
|
|
// Mb.Run();
|
2015-05-07 08:56:47 +02:00
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
if (wifiEnabled) {
|
|
|
|
if ((disconnectState == 0) && (! client.loop())) {
|
|
|
|
disconnectState = 1;
|
2015-05-07 08:56:47 +02:00
|
|
|
}
|
2015-05-18 12:36:47 +02:00
|
|
|
|
|
|
|
switch (disconnectState) {
|
|
|
|
case 0:
|
|
|
|
// Serial.println("discState 0");
|
|
|
|
// everything fine
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Serial.println("discState 1");
|
|
|
|
client.disconnect();
|
2015-05-07 08:56:47 +02:00
|
|
|
disconnectTime = millis();
|
2015-05-18 12:36:47 +02:00
|
|
|
disconnectState = 2;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
Serial.println("discState 3");
|
|
|
|
if (disconnectTime + 2000 < millis()) {
|
|
|
|
disconnectState = 3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
Serial.println("discState 3");
|
|
|
|
if (client.connect("WiFiPowerMeter")) {
|
|
|
|
disconnectTime = millis();
|
|
|
|
disconnectState = 0;
|
|
|
|
} else {
|
|
|
|
disconnectState = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2015-05-07 08:56:47 +02:00
|
|
|
disconnectState = 0;
|
2015-05-18 12:36:47 +02:00
|
|
|
break;
|
2015-05-07 08:56:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
if (minute.check() == 1) {
|
|
|
|
char strbuf[256];
|
|
|
|
memset(strbuf, sizeof(strbuf), 0);
|
|
|
|
PString buf = PString(strbuf, sizeof(strbuf));
|
|
|
|
buf << "{ \"metadata\": { \"device\": \"WiFiPowerMeter\" }, " <<
|
|
|
|
"\"data\": {" <<
|
|
|
|
"\"voltage\": " << getVoltage() << ", " <<
|
|
|
|
"\"current\": " << getCurrent() << ", " <<
|
|
|
|
"\"cosphi\": " << getCosPhi() << ", " <<
|
|
|
|
"\"power\": " << getPower() << ", " <<
|
|
|
|
"\"energy\": " << getEnergy() << ", " <<
|
|
|
|
"\"newEnergy\": " << getNewEnergy() << ", " <<
|
|
|
|
"\"uptime\": " << uptime <<
|
|
|
|
"}" <<
|
|
|
|
"}" << endl;
|
|
|
|
if (disconnectState == 0) {
|
|
|
|
client.publish("IoT/WiFiPowerMeter/Measurement", strbuf);
|
|
|
|
}
|
2015-05-07 08:56:47 +02:00
|
|
|
}
|
2015-05-03 20:53:49 +02:00
|
|
|
}
|
|
|
|
|
2015-05-13 22:47:57 +02:00
|
|
|
if (second.check() == 1) {
|
|
|
|
uptime++;
|
|
|
|
|
2015-05-03 20:53:49 +02:00
|
|
|
Serial.println("tick");
|
2015-05-13 22:47:57 +02:00
|
|
|
|
2015-05-18 12:36:47 +02:00
|
|
|
if (wifiEnabled) {
|
|
|
|
String msg = String("{ \"metadata\": { \"device\": \"WiFiPowerMeter\" }, \"data\": { \"uptime\": ") + uptime + String("}}");
|
|
|
|
if (disconnectState == 0) {
|
|
|
|
client.publish("IoT/WiFiPowerMeter/Heartbeat", (char*)msg.c_str());
|
|
|
|
}
|
2015-05-13 22:47:57 +02:00
|
|
|
}
|
2015-05-02 00:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|