json output via udp

This commit is contained in:
hg
2015-05-03 20:53:49 +02:00
parent 40e18d4172
commit bbe3e8f9cd
7 changed files with 251 additions and 46 deletions

View File

@ -1,10 +1,13 @@
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUDP.h>
#include "Metro.h"
#include "Mudbus.h"
// #include "Mudbus.h"
#include "LiquidCrystal.h"
#include "ModbusApp.h"
#include "PString.h"
#include "Streaming.h"
@ -12,8 +15,8 @@
LiquidCrystal lcd = LiquidCrystal(A0, A1, A2, A3, A4, A5);
Mudbus Mb;
// Mudbus Mb;
WiFiUDP udpSock;
//char ssid[] = "Kinderland"; // your network SSID (name)
@ -23,7 +26,7 @@ char pass[] = "UNVmpwbr6heQnMQ7ykXT";
Metro tick = Metro(10000);
Metro second = Metro(1000);
uint32_t *uptime;
uint32_t uptime;
void printWifiStatus() {
// print the SSID of the network you're attached to:
@ -46,7 +49,7 @@ void printWifiStatus() {
}
void setup() {
Serial.begin(9600);
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
@ -93,31 +96,28 @@ void setup() {
lcd.home();
lcd.clear();
uptime = (uint32_t*) &(Mb.R[121]); // uptime
*uptime = 0;
// uptime = (uint32_t*) &(Mb.R[121]); // uptime
uptime = 0;
Mb.R[123] = 0xdead; // magic
Mb.R[124] = 0xbeef;
// Mb.R[123] = 0xdead; // magic
// Mb.R[124] = 0xbeef;
modbusAppBegin(&Mb);
// modbusAppBegin(&Mb);
modbusAppBegin();
}
void updateDisplay() {
static uint32_t lastUptime = 0;
static long lastRssi = 0;
long rssi = WiFi.RSSI();
if ((*uptime != lastUptime) ||
(rssi != lastRssi)) {
lastUptime = *uptime;
lastRssi = rssi;
if (uptime != lastUptime) {
lastUptime = uptime;
lcd.home(); lcd.clear();
uint32_t days = *uptime / 86400;
uint32_t rem = *uptime % 86400;
uint32_t days = uptime / 86400;
uint32_t rem = uptime % 86400;
uint32_t hours = rem / 3600;
rem = rem % 3600;
uint32_t minutes = rem / 60;
@ -126,7 +126,10 @@ void updateDisplay() {
lcd.print(days); lcd.print("d "); lcd.print(hours); lcd.print(":");
lcd.print(minutes); lcd.print(":"); lcd.print(seconds); lcd.print(" ");
lcd.print(rssi);
long rssi = WiFi.RSSI();
lcd.print(rssi); lcd.print(" ");
uint8_t wifiStatus = WiFi.status();
lcd.print(wifiStatus);
lcd.setCursor(0, 1);
lcd.print(getVoltage()); lcd.print("V");
@ -145,10 +148,59 @@ void updateDisplay() {
void loop() {
modbusAppExec();
Mb.Run();
// Mb.Run();
if (second.check() == 1) {
(*uptime)++;
uptime++;
char strbuf[256];
memset(strbuf, sizeof(strbuf), 0);
PString buf = PString(strbuf, sizeof(strbuf));
udpSock.beginPacket("172.16.2.16", 9999);
buf << "{ \"metadata\": { \"table\": \"WiFiPowerMeter\" }, \"data\": {";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"voltage\": " << getVoltage() << ", ";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"current\": " << getCurrent() << ", ";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"frequency\": " << getFrequency() << ", ";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"power\": " << getPower() << ", ";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"energy\": " << getEnergy() << ", ";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "\"uptime\": " << uptime;
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "}";
udpSock.write(strbuf, buf.length());
buf.begin();
buf << "}" << endl;
udpSock.write(strbuf, buf.length());
udpSock.endPacket();
}
if (tick.check() == 1) {
Serial.println("tick");
}
updateDisplay();