Skip to content

Commit

Permalink
add /raw.dat output for better debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
aviborg committed Oct 30, 2023
1 parent d2f2227 commit afca4bb
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 263 deletions.
130 changes: 72 additions & 58 deletions lib/HanReader/HanReader.cpp
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
#include "HanReader.h"

#ifdef ARDUINO

#include <LittleFS.h>
#include <ESP8266WiFi.h>

HanReader::HanReader(Stream *hanPort)
{
han = hanPort;
bytesRead = 0;
}

String HanReader::parseData()
{
String dataJsonStr;
jsonData.clear();
jsonData["elapsedtime"] = millis();
jsonData["rssi"] = WiFi.RSSI();
jsonData["mac"] = WiFi.macAddress();
jsonData["localip"] = WiFi.localIP().toString();
jsonData["ssid"] = WiFi.SSID();
jsonData["hostname"] = WiFi.getHostname();
if (bytesRead > 0)
{
reader.setJson(&jsonData);
if (!reader.ParseData(buffer, bytesRead))
{
File dataFile;
dataFile = LittleFS.open("/log.txt", "w");
dataFile.printf("--- Start %lu ---", millis());
for (uint32_t n = 0; n < bytesRead; ++n)
{
if ((n % 16) == 0)
dataFile.println();
dataFile.printf("%#04x, ", buffer[n]);
}
dataFile.println("\n--- End ---");
dataFile.close();
}
}
serializeJson(jsonData, dataJsonStr);
bytesRead = 0;
return dataJsonStr;
}

bool HanReader::read()
{
if (bytesRead >= DLMS_READER_BUFFER_SIZE)
return false;
bool dataReceived = han->available() > 0;
while ((han->available() > 0) && (bytesRead < DLMS_READER_BUFFER_SIZE))
{
buffer[bytesRead++] = han->read();
}
return dataReceived;
}

#include "HanReader.h"

#ifdef ARDUINO

#include <LittleFS.h>
#include <ESP8266WiFi.h>

HanReader::HanReader(Stream *hanPort)
{
han = hanPort;
bytesRead = 0;
}

String HanReader::getHex(){
String str("");
if(bytesRead > 0){
for (uint32_t n = 0; n < bytesRead; ++n){
str += "0x";
if(buffer[n] < 16) str += "0";
str += String(buffer[n], HEX);
if(n + 1 < bytesRead) str += ", ";
if((n % 16) == 15) str += '\n';
}
}
return str;
}

String HanReader::parseData()
{
String dataJsonStr;
jsonData.clear();
jsonData["elapsedtime"] = millis();
jsonData["rssi"] = WiFi.RSSI();
jsonData["mac"] = WiFi.macAddress();
jsonData["localip"] = WiFi.localIP().toString();
jsonData["ssid"] = WiFi.SSID();
jsonData["hostname"] = WiFi.getHostname();
if (bytesRead > 0)
{
reader.setJson(&jsonData);
if (!reader.ParseData(buffer, bytesRead))
{
File dataFile;
dataFile = LittleFS.open("/log.txt", "w");
dataFile.printf("--- Start %lu ---", millis());
for (uint32_t n = 0; n < bytesRead; ++n)
{
if ((n % 16) == 0)
dataFile.println();
dataFile.printf("%#04x, ", buffer[n]);
}
dataFile.println("\n--- End ---");
dataFile.close();
}
}
serializeJson(jsonData, dataJsonStr);
bytesRead = 0;
return dataJsonStr;
}

bool HanReader::read()
{
if (bytesRead >= DLMS_READER_BUFFER_SIZE)
return false;
bool dataReceived = han->available() > 0;
while ((han->available() > 0) && (bytesRead < DLMS_READER_BUFFER_SIZE))
{
buffer[bytesRead++] = han->read();
}
return dataReceived;
}

#endif //ARDUINO
53 changes: 27 additions & 26 deletions lib/HanReader/HanReader.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
#ifndef _HANREADER_h
#define _HANREADER_h

#ifdef ARDUINO
#include <Arduino.h>
#include "DlmsReader.h"


class HanReader
{
public:
HanReader(Stream *hanPort);
String parseData();
bool read();
private:
Stream *han;

uint8_t buffer[DLMS_READER_BUFFER_SIZE] = {0};
StaticJsonDocument<DLMS_READER_BUFFER_SIZE*4> jsonData;
uint32_t bytesRead = 0;
DlmsReader reader;

};

#endif //ARDUINO

#ifndef _HANREADER_h
#define _HANREADER_h

#ifdef ARDUINO
#include <Arduino.h>
#include "DlmsReader.h"


class HanReader
{
public:
HanReader(Stream *hanPort);
String parseData();
String getHex();
bool read();
private:
Stream *han;

uint8_t buffer[DLMS_READER_BUFFER_SIZE] = {0};
StaticJsonDocument<DLMS_READER_BUFFER_SIZE*4> jsonData;
uint32_t bytesRead = 0;
DlmsReader reader;

};

#endif //ARDUINO

#endif
165 changes: 83 additions & 82 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include "HanReader.h"
#include "web/AmsWebServer.h"
#include "hw/chipSetup.h"

AmsWebServer webServer;
HanReader hanReader(&Serial);

void setup() {
Serial.begin(115200);

pinMode(TRIGGER_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, LOW);

// Mount filesystem
if (!LittleFS.begin()) {
LittleFS.end();
delay(2000);
ESP.reset();
}

// Setup wifi and webserver
wifiSetup();
webServer.setup();
ArduinoOTA.begin();
webServer.setDataJson(hanReader.parseData());

// Flush serial buffer
while(Serial.available()>0) Serial.read();
}

void loop()
{
static uint32_t lastUpdate = 0xFFFFFFFF; // Test wraparound at startup instead of waiting 50 days
static uint16_t scheduleState = 0;
static bool dataReceived = false;
unsigned long now = millis();
// Reading serial data should be uninterrupted
// When a serial read is detected other stuff is delayed 13 ms
// using a timeout not divisible by 1000 (even) and a prime number
// reduce the risk of having the server working while data is received
if (hanReader.read()) {
lastUpdate = now;
dataReceived = true;
}
else {
if (now - lastUpdate > 13) {
lastUpdate = now;
if (dataReceived) {
digitalWrite(LED_BUILTIN, LOW); // Lit up LED
webServer.setDataJson(hanReader.parseData());
dataReceived = false;
}
else {
digitalWrite(LED_BUILTIN, HIGH); // Turn off LED
switch (scheduleState++) {
case 0:
resetChipOnTrigger();
break;
case 1:
MDNS.update();
break;
case 2:
webServer.loop();
break;
case 3:
ArduinoOTA.handle();
break;
default:
yield();
scheduleState = 0;
break;
}
}
}
}
#include <Arduino.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <LittleFS.h>
#include <ArduinoJson.h>
#include <ArduinoOTA.h>
#include "HanReader.h"
#include "web/AmsWebServer.h"
#include "hw/chipSetup.h"

AmsWebServer webServer;
HanReader hanReader(&Serial);

void setup() {
Serial.begin(115200);

pinMode(TRIGGER_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, LOW);

// Mount filesystem
if (!LittleFS.begin()) {
LittleFS.end();
delay(2000);
ESP.reset();
}

// Setup wifi and webserver
wifiSetup();
webServer.setup();
ArduinoOTA.begin();
webServer.setDataJson(hanReader.parseData());

// Flush serial buffer
while(Serial.available()>0) Serial.read();
}

void loop()
{
static uint32_t lastUpdate = 0xFFFFFFFF; // Test wraparound at startup instead of waiting 50 days
static uint16_t scheduleState = 0;
static bool dataReceived = false;
unsigned long now = millis();
// Reading serial data should be uninterrupted
// When a serial read is detected other stuff is delayed 13 ms
// using a timeout not divisible by 1000 (even) and a prime number
// reduce the risk of having the server working while data is received
if (hanReader.read()) {
lastUpdate = now;
dataReceived = true;
}
else {
if (now - lastUpdate > 13) {
lastUpdate = now;
if (dataReceived) {
digitalWrite(LED_BUILTIN, LOW); // Lit up LED
webServer.setRawData(hanReader.getHex());
webServer.setDataJson(hanReader.parseData());
dataReceived = false;
}
else {
digitalWrite(LED_BUILTIN, HIGH); // Turn off LED
switch (scheduleState++) {
case 0:
resetChipOnTrigger();
break;
case 1:
MDNS.update();
break;
case 2:
webServer.loop();
break;
case 3:
ArduinoOTA.handle();
break;
default:
yield();
scheduleState = 0;
break;
}
}
}
}
}
Loading

0 comments on commit afca4bb

Please sign in to comment.