Skip to content

Instantly share code, notes, and snippets.

@natm
Created November 20, 2016 18:56
Show Gist options
  • Save natm/d5213363a98fcd8e9473acaab4b39cfc to your computer and use it in GitHub Desktop.
Save natm/d5213363a98fcd8e9473acaab4b39cfc to your computer and use it in GitHub Desktop.
#include <SoftwareSerial.h>
#define PUSHINTERVAL 10
SoftwareSerial mySerial = SoftwareSerial(4, 14);
byte readByte = 0xFF;
byte pinState = 0;
char startPattern[] = "<ch1><watts>";
char endPattern[] = "<";
int state = 0;
int pos = 0;
int power = 0;
int powerLast = 0;
static uint32_t timer;
void setup() {
mySerial.begin(9600);
Serial.begin(115200);
Serial.println("Starting up");
}
void loop() {
readByte = mySerial.read();
if (readByte == 0xFF) {
}
else {
//Serial.print(readByte, BYTE);
gotByte();
}
if (millis() > timer) {
timer = millis() + (PUSHINTERVAL * 1000);
Serial.println(powerLast);
}
}
/// current cost
void gotByte() {
if (state == 0) {
if (readByte == startPattern[pos]) {
++pos;
if (startPattern[pos] == 0) {
// finished matching start pattern
++state;
power = 0;
}
}
else {
pos = 0;
}
}
else if (state == 1) {
if (readByte == endPattern[0]) {
// finished reading power
powerLast = power;
state = 0;
} else {
// read another digit
power = power * 10 + readByte - '0';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment