Helllo,
I am trying to send my smart meter (p1 meter) to influxdb. It is working when I plug in the esp8266 but after that it won't update anymore. Can someone check my code if I keep the connection open somewere or if I don't open it correctly?
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include "CRC16.h"
const byte eth_server[] = {xx, xx, xx, xx};
const int eth_port = xxxx;
const int bufferSize = 2048;
char buf[bufferSize] = {'\0'};
const char meter[] = "meter";
int lastGivenPlace = 0;
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxx";
const char* hostName = "xxxxxxxxxx";
const bool outputOnSerial = false;
unsigned long mEVLT; //Meter reading Electrics - consumption low tariff
unsigned long prevEVLT;
unsigned long mEVHT; //Meter reading Electrics - consumption high tariff
unsigned long prevEVHT;
unsigned long mEOLT = 0;
unsigned long mEOHT = 0;
unsigned long mEAV; //Meter reading Electrics - Actual consumption
unsigned long mEAT = 0;
unsigned long mGAS; //Meter reading Gas
unsigned long prevGAS;
#define MAXLINELENGTH 1024 // longest normal line is 47 char (+3 for \r\n\0)
char telegram[MAXLINELENGTH];
#define SERIAL_RX D5 // pin for SoftwareSerial RX
SoftwareSerial mySerial(SERIAL_RX, -1, true, MAXLINELENGTH);
unsigned int currentCRC=0;
WiFiClient client;
const char* get_place() {
switch(lastGivenPlace) {
case 0:
lastGivenPlace = 1;
return &meter[0];
}
return "unknown_place";
}
bool eth_start(){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
delay(2000); //delay to allow connection to be done
int conState = client.connect(eth_server, eth_port);
if(conState > 0) {
Serial.println("Connected to InfluxDB server");
client.stop();
return true;
}
Serial.print("Could not connect to InfluxDB Server, Error #");
Serial.println(conState);
return false;
}
void setup() {
Serial.begin(9600);
Serial.println("Booting");
eth_start();
mySerial.begin(115200);
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void UpdateGas(){
if(prevGAS!=mGAS)
{
char sValue[10];
sprintf(sValue, "%d", mGAS);
prevGAS=mGAS;
}
}
void UpdateElectricity(){
char sValue[255];
sprintf(sValue, "%d;%d;%d;%d;%d;%d", mEVLT, mEVHT, mEOLT, mEOHT, mEAV, mEAT);
}
bool isNumber(char* res, int len){
for (int i = 0; i < len; i++) {
if (((res[i] < '0') || (res[i] > '9')) && (res[i] != '.' && res[i] != 0)) {
return false;
}
}
return true;
}
int FindCharInArrayRev(char array[], char c, int len) {
for (int i = len - 1; i >= 0; i--) {
if (array[i] == c) {
return i;
}
}
return -1;
}
long getValidVal(long valNew, long valOld, long maxDiffer){
//check if the incoming value is valid
if(valOld > 0 && ((valNew - valOld > maxDiffer) && (valOld - valNew > maxDiffer)))
return valOld;
return valNew;
}
long getValue(char* buffer, int maxlen) {
int s = FindCharInArrayRev(buffer, '(', maxlen - 2);
if (s < 8) return 0;
if (s > 32) s = 32;
int l = FindCharInArrayRev(buffer, '*', maxlen - 2) - s - 1;
if (l < 4) return 0;
if (l > 12) return 0;
char res[16];
memset(res, 0, sizeof(res));
if (strncpy(res, buffer + s + 1, l)) {
if (isNumber(res, l)) {
return (1000 * atof(res));
}
}
return 0;
}
bool decodeTelegram(int len) {
//need to check for start
int startChar = FindCharInArrayRev(telegram, '/', len);
int endChar = FindCharInArrayRev(telegram, '!', len);
bool validCRCFound = false;
if(startChar>=0)
{
//start found. Reset CRC calculation
currentCRC=CRC16(0x0000,(unsigned char *) telegram+startChar, len-startChar);
if(outputOnSerial)
{
for(int cnt=startChar; cnt<len-startChar;cnt++)
Serial.print(telegram[cnt]);
}
}
else if(endChar>=0)
{
//add to crc calc
currentCRC=CRC16(currentCRC,(unsigned char*)telegram+endChar, 1);
char messageCRC[5];
strncpy(messageCRC, telegram + endChar + 1, 4);
messageCRC[4]=0; //thanks to HarmOtten (issue 5)
if(outputOnSerial)
{
for(int cnt=0; cnt<len;cnt++)
Serial.print(telegram[cnt]);
}
validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);
if(validCRCFound)
Serial.println("\nVALID CRC FOUND!");
else
Serial.println("\n===INVALID CRC FOUND!===");
currentCRC = 0;
}
else
{
currentCRC=CRC16(currentCRC, (unsigned char*)telegram, len);
if(outputOnSerial)
{
for(int cnt=0; cnt<len;cnt++)
Serial.print(telegram[cnt]);
}
}
long val =0;
long val2=0;
if (strncmp(telegram, "1-0:1.8.1", strlen("1-0:1.8.1")) == 0)
mEVLT = getValue(telegram, len) / 1000;
Serial.print("Verbruik Laag =:");
Serial.println(mEVLT);
if (strncmp(telegram, "1-0:1.8.2", strlen("1-0:1.8.2")) == 0)
mEVHT = getValue(telegram, len) / 1000;
Serial.print("Verbruik Hoog =:");
Serial.println(mEVHT);
if (strncmp(telegram, "1-0:1.7.0", strlen("1-0:1.7.0")) == 0)
mEAV = getValue(telegram, len);
if (strncmp(telegram, "1-0:2.7.0", strlen("1-0:2.7.0")) == 0)
mEAT = getValue(telegram, len);
if (strncmp(telegram, "0-1:24.2.1", strlen("0-1:24.2.1")) == 0)
mGAS = getValue(telegram, len) / 1000;
Serial.print("GAS=:");
Serial.println(mGAS);
if((mEVLT >= 10) && (mEVHT >= 10) && (mGAS >= 10)){
updateall();
}
return validCRCFound;
}
void updateall(){
if((mEVLT != prevEVLT) && (mEVHT == prevEVHT) && (mGAS == prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
if((mEVLT == prevEVLT) && (mEVHT != prevEVHT) && (mGAS == prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
if((mEVLT == prevEVLT) && (mEVHT == prevEVHT) && (mGAS != prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
if((mEVLT != prevEVLT) && (mEVHT == prevEVHT) && (mGAS != prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
if((mEVLT == prevEVLT) && (mEVHT != prevEVHT) && (mGAS != prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
if((mEVLT != prevEVLT) && (mEVHT != prevEVHT) && (mGAS == prevGAS)){
updatereal();
prevEVLT = mEVLT;
prevEVHT = mEVHT;
prevGAS = mGAS;
}
}
void updatereal(){
int numChars = 0;
numChars = sprintf(buf, "p1,");
numChars += sprintf(&buf[numChars], "SOURCE=arduino_1,PLACE=%s ", get_place());
numChars += sprintf(&buf[numChars], "GAS=%d,", mGAS);
numChars += sprintf(&buf[numChars], "LAAGTARIEF=%d,", mEVLT);
numChars += sprintf(&buf[numChars], "HOOGTARIEF=%d", mEVHT);
Serial.print("Sending following dataset to InfluxDB: ");
Serial.println(buf);
eth_send_data(buf, numChars);
memset(buf, '\0', bufferSize);
delay(1000);
Serial.print("updated");
}
void readTelegram() {
if (mySerial.available()) {
memset(telegram, 0, sizeof(telegram));
while (mySerial.available()) {
int len = mySerial.readBytesUntil('\n', telegram, MAXLINELENGTH);
telegram[len] = '\n';
telegram[len+1] = 0;
yield();
if(decodeTelegram(len+1));{
UpdateElectricity();
UpdateGas();
}
}
}
}
void eth_send_data(char* data, int dataSize) {
client.connect(eth_server, eth_port);
int conState = client.connect(eth_server, eth_port);
if(conState <= 0) { //check if connection to server is stablished
Serial.print("Could not connect to InfluxDB Server, Error #");
Serial.println(conState);
return;
}
client.println("POST /write?db=db HTTP/1.1");
client.println("Host: www.xxxxxxxxx.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(dataSize);
client.println();
client.println(data);
delay(50);
Serial.println("Reply from InfluxDB");
while(client.available()) {
Serial.print((char)client.read());
}
Serial.println();
client.stop();
}
void loop() {
readTelegram();
}
hope someone can help me solve this problem because I don't see te problem.
Best regards,
Justinn