json output via udp
This commit is contained in:
parent
40e18d4172
commit
bbe3e8f9cd
@ -6,12 +6,10 @@
|
||||
*/
|
||||
|
||||
#include "SimpleModbusMaster.h"
|
||||
#include "Mudbus.h"
|
||||
|
||||
// #include "Mudbus.h"
|
||||
|
||||
#include "ModbusApp.h"
|
||||
|
||||
|
||||
enum
|
||||
{
|
||||
VOLTAGE,
|
||||
@ -24,7 +22,8 @@ enum
|
||||
|
||||
Packet packets[TOTAL_NO_OF_PACKETS];
|
||||
|
||||
Mudbus *modbusSlave;
|
||||
|
||||
// Mudbus *modbusSlave;
|
||||
|
||||
uint16_t voltage[2];
|
||||
uint16_t frequency[2];
|
||||
@ -55,9 +54,10 @@ float getEnergy() { return convF(energy); }
|
||||
float getNewEnergy() { return convF(energy) - startEnergy; }
|
||||
|
||||
|
||||
void modbusAppBegin(Mudbus *mb) {
|
||||
// void modbusAppBegin(Mudbus *mb) {
|
||||
void modbusAppBegin() {
|
||||
pinMode(ZEROING_PIN, INPUT_PULLUP);
|
||||
modbusSlave = mb;
|
||||
// modbusSlave = mb;
|
||||
|
||||
|
||||
modbus_construct(&packets[VOLTAGE], 1, READ_HOLDING_REGISTERS, 0x2000, 2, voltage);
|
||||
@ -79,15 +79,16 @@ void modbusAppExec() {
|
||||
startEnergy = nEnergy;
|
||||
}
|
||||
|
||||
modbusSlave->R[0] = voltage[0];
|
||||
modbusSlave->R[1] = voltage[1];
|
||||
modbusSlave->R[2] = current[0];
|
||||
modbusSlave->R[3] = current[1];
|
||||
modbusSlave->R[4] = frequency[0];
|
||||
modbusSlave->R[5] = frequency[1];
|
||||
modbusSlave->R[6] = power[0];
|
||||
modbusSlave->R[7] = power[1];
|
||||
modbusSlave->R[8] = energy[0];
|
||||
modbusSlave->R[9] = energy[1];
|
||||
|
||||
// modbusSlave->R[0] = voltage[0];
|
||||
// modbusSlave->R[1] = voltage[1];
|
||||
// modbusSlave->R[2] = current[0];
|
||||
// modbusSlave->R[3] = current[1];
|
||||
// modbusSlave->R[4] = frequency[0];
|
||||
// modbusSlave->R[5] = frequency[1];
|
||||
// modbusSlave->R[6] = power[0];
|
||||
// modbusSlave->R[7] = power[1];
|
||||
// modbusSlave->R[8] = energy[0];
|
||||
// modbusSlave->R[9] = energy[1];
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@
|
||||
#define MODBUSAPP_H_
|
||||
|
||||
#include "SimpleModbusMaster.h"
|
||||
#include "Mudbus.h"
|
||||
// #include "Mudbus.h"
|
||||
|
||||
|
||||
|
||||
@ -22,7 +22,8 @@ const uint8_t MODBUS_TX_ENABLE_PIN = 2;
|
||||
const uint8_t ZEROING_PIN = 3;
|
||||
|
||||
|
||||
void modbusAppBegin(Mudbus *mb);
|
||||
// void modbusAppBegin(Mudbus *mb);
|
||||
void modbusAppBegin();
|
||||
void modbusAppExec();
|
||||
|
||||
float getVoltage();
|
||||
|
2
Mudbus.h
2
Mudbus.h
@ -29,7 +29,7 @@
|
||||
#ifndef Mudbus_h
|
||||
#define Mudbus_h
|
||||
|
||||
#define MbDebug
|
||||
// #define MbDebug
|
||||
|
||||
#define MB_N_R 125 //Max 16 bit registers for Modbus is 125
|
||||
#define MB_N_C 128 //Max coils for Modbus is 2000 - dont need that many so here is a multiple of 8
|
||||
|
60
PString.cpp
Normal file
60
PString.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
PString.cpp - Lightweight printable string class
|
||||
Copyright (c) 2009-2012 Mikal Hart. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "PString.h"
|
||||
|
||||
void PString::begin()
|
||||
{
|
||||
_cur = _buf;
|
||||
if (_size > 0)
|
||||
_buf[0] = '\0';
|
||||
}
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
size_t PString::write(uint8_t b)
|
||||
#else
|
||||
void PString::write(uint8_t b)
|
||||
#endif
|
||||
{
|
||||
if (_cur + 1 < _buf + _size)
|
||||
{
|
||||
*_cur++ = (char)b;
|
||||
*_cur = '\0';
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
int PString::format(char *str, ...)
|
||||
{
|
||||
va_list argptr;
|
||||
va_start(argptr, str);
|
||||
int ret = vsnprintf(_cur, _size - (_cur - _buf), str, argptr);
|
||||
if (_size)
|
||||
while (*_cur)
|
||||
++_cur;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
87
PString.h
Normal file
87
PString.h
Normal file
@ -0,0 +1,87 @@
|
||||
/*
|
||||
PString.h - Lightweight printable string class
|
||||
Copyright (c) 2009-2012 Mikal Hart. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef PString_h
|
||||
#define PString_h
|
||||
|
||||
#include "Print.h"
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PSTRING_LIBRARY_VERSION 3
|
||||
|
||||
class PString : public Print
|
||||
{
|
||||
private:
|
||||
char *_buf, *_cur;
|
||||
size_t _size;
|
||||
public:
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
virtual size_t write(uint8_t);
|
||||
#else
|
||||
virtual void write(uint8_t);
|
||||
#endif
|
||||
|
||||
public:
|
||||
|
||||
// Basic constructor requires a preallocated buffer
|
||||
PString(char *buf, size_t size) : _buf(buf), _size(size)
|
||||
{ begin(); }
|
||||
|
||||
// templated constructors allow inline renderings of this type: PString(buf, size, myfloat[, modifier]);
|
||||
template<class T> PString(char *buf, size_t size, T arg) : _buf(buf), _size(size)
|
||||
{ begin(); print(arg); }
|
||||
|
||||
template<class T> PString(char *buf, size_t size, T arg, int modifier) : _buf(buf), _size(size)
|
||||
{ begin(); print(arg, modifier); }
|
||||
|
||||
// returns the length of the current string, not counting the 0 terminator
|
||||
inline const size_t length()
|
||||
{ return _cur - _buf; }
|
||||
|
||||
// returns the capacity of the string
|
||||
inline const size_t capacity()
|
||||
{ return _size; }
|
||||
|
||||
// gives access to the internal string
|
||||
inline operator const char *()
|
||||
{ return _buf; }
|
||||
|
||||
// compare to another string
|
||||
bool operator==(const char *str)
|
||||
{ return _size > 0 && !strcmp(_buf, str); }
|
||||
|
||||
// call this to re-use an existing string
|
||||
void begin();
|
||||
|
||||
// This function allows assignment to an arbitrary scalar value like str = myfloat;
|
||||
template<class T> inline PString &operator =(T arg)
|
||||
{ begin(); print(arg); return *this; }
|
||||
|
||||
// Concatenation str += myfloat;
|
||||
template<class T> inline PString &operator +=(T arg)
|
||||
{ print(arg); return *this; }
|
||||
|
||||
// Safe access to sprintf-like formatting, e.g. str.format("Hi, my name is %s and I'm %d years old", name, age);
|
||||
int format(char *str, ...);
|
||||
};
|
||||
|
||||
#endif
|
@ -48,14 +48,18 @@ void modbus_update()
|
||||
switch (state)
|
||||
{
|
||||
case IDLE:
|
||||
idle();
|
||||
break;
|
||||
//Serial.println("mu 1a");
|
||||
idle();
|
||||
//Serial.println("mu 1b");
|
||||
break;
|
||||
case WAITING_FOR_REPLY:
|
||||
waiting_for_reply();
|
||||
break;
|
||||
//Serial.println("mu 2a");
|
||||
waiting_for_reply();
|
||||
//Serial.println("mu 2b");
|
||||
break;
|
||||
case WAITING_FOR_TURNAROUND:
|
||||
waiting_for_turnaround();
|
||||
break;
|
||||
waiting_for_turnaround();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user