Client.connect only working on startup, after startup no updates

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

      if(decodeTelegram(len+1));{
        UpdateElectricity();
        UpdateGas();
}

That first semicolon should not be there. If you auto-format your code before posting, you might spot that kind of mistake. Check ALL of your 'if' statements to see if you have made that mistake in other places.

First of all thank you for trying to help me with the code :slight_smile:

I am starting the update of the data here:

if((mEVLT >= 10) && (mEVHT >= 10) && (mGAS >= 10)){
  updateall();
}

It is in decodetelegram, The other update part were you are talking about in the code is in there but I am not using it. I think myself that the problem is opening a connection to the database which is going wrong. Or something like that. So I am talking about the part:

On startup:
eth_start

in loop:
Updateall
Updatereal

Hope that you can help me finding the error.

I also checked the code with auto-format but there were no more errors in the code excepts this one you told me.

Best regards Justinn

Set your compiler warnings to ALL and see if any warnings show up, like:

Arduino: 1.8.5 (Mac OS X), Board: "Generic ESP8266 Module, 80 MHz, Flash, ck, 26 MHz, 40MHz, QIO, 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

sketch_oct31a:30: error: 'D5' was not declared in this scope
 #define SERIAL_RX     D5  // pin for SoftwareSerial RX
                       ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:31:25: note: in expansion of macro 'SERIAL_RX'
 SoftwareSerial mySerial(SERIAL_RX, -1, true, MAXLINELENGTH);
                         ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void UpdateGas()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:85:31: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
     sprintf(sValue, "%d", mGAS);
                               ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:85:31: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void UpdateElectricity()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
   sprintf(sValue, "%d;%d;%d;%d;%d;%d", mEVLT, mEVHT, mEOLT, mEOHT, mEAV, mEAT);
                                                                              ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 7 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 8 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 7 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 8 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'bool decodeTelegram(int)':
sketch_oct31a:155: error: 'CRC16' was not declared in this scope
     currentCRC = CRC16(0x0000, (unsigned char *) telegram + startChar, len - startChar);
                                                                                       ^
sketch_oct31a:165: error: 'CRC16' was not declared in this scope
     currentCRC = CRC16(currentCRC, (unsigned char*)telegram + endChar, 1);
                                                                         ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:174:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);
                                                      ^
sketch_oct31a:183: error: 'CRC16' was not declared in this scope
     currentCRC = CRC16(currentCRC, (unsigned char*)telegram, len);
                                                                 ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:190:8: warning: unused variable 'val' [-Wunused-variable]
   long val = 0;
        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:191:8: warning: unused variable 'val2' [-Wunused-variable]
   long val2 = 0;
        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void updatereal()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:268:54: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
   numChars += sprintf(&buf[numChars], "GAS=%d,", mGAS);
                                                      ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:268:54: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:269:62: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
   numChars += sprintf(&buf[numChars], "LAAGTARIEF=%d,", mEVLT);
                                                              ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:269:62: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:270:61: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
   numChars += sprintf(&buf[numChars], "HOOGTARIEF=%d", mEVHT);
                                                             ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:270:61: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
exit status 1
'D5' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

johnwasser:
Set your compiler warnings to ALL and see if any warnings show up, like:

Arduino: 1.8.5 (Mac OS X), Board: "Generic ESP8266 Module, 80 MHz, Flash, ck, 26 MHz, 40MHz, QIO, 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

sketch_oct31a:30: error: 'D5' was not declared in this scope
#define SERIAL_RX    D5  // pin for SoftwareSerial RX
                      ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:31:25: note: in expansion of macro 'SERIAL_RX'
SoftwareSerial mySerial(SERIAL_RX, -1, true, MAXLINELENGTH);
                        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void UpdateGas()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:85:31: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
    sprintf(sValue, "%d", mGAS);
                              ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:85:31: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void UpdateElectricity()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  sprintf(sValue, "%d;%d;%d;%d;%d;%d", mEVLT, mEVHT, mEOLT, mEOHT, mEAV, mEAT);
                                                                              ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 7 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 8 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 5 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 6 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 7 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 8 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'bool decodeTelegram(int)':
sketch_oct31a:155: error: 'CRC16' was not declared in this scope
    currentCRC = CRC16(0x0000, (unsigned char ) telegram + startChar, len - startChar);
                                                                                      ^
sketch_oct31a:165: error: 'CRC16' was not declared in this scope
    currentCRC = CRC16(currentCRC, (unsigned char
)telegram + endChar, 1);
                                                                        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:174:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);
                                                      ^
sketch_oct31a:183: error: 'CRC16' was not declared in this scope
    currentCRC = CRC16(currentCRC, (unsigned char*)telegram, len);
                                                                ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:190:8: warning: unused variable 'val' [-Wunused-variable]
  long val = 0;
        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:191:8: warning: unused variable 'val2' [-Wunused-variable]
  long val2 = 0;
        ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino: In function 'void updatereal()':
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:268:54: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  numChars += sprintf(&buf[numChars], "GAS=%d,", mGAS);
                                                      ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:268:54: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:269:62: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  numChars += sprintf(&buf[numChars], "LAAGTARIEF=%d,", mEVLT);
                                                              ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:269:62: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:270:61: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
  numChars += sprintf(&buf[numChars], "HOOGTARIEF=%d", mEVHT);
                                                            ^
/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:270:61: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
exit status 1
'D5' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

That is correct I am seeing the same types of errors. Is there an error in the sprintf? Because the first upload to the database is working(when I startup the nodemcu esp8266). After the startup it won't update to database anymore when the p1 meter data changes.

I've just had a quick look at the warnings

/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]

gives you a very good indication of what is wrong.

You're passing a long integer to sprintf and asking it to treat the long integer (4 bytes) as an integer (2 bytes)
Doing this has a lot of ramifications on your output.

Study the printf reference. Go through the parameters table carefully and use the correct combination of length and specifier for the type of variable you're using with sprintf()

(I'm not going to comment on the errors)

darrob:
I've just had a quick look at the warnings

/Users/john/Documents/Arduino/sketch_oct31a/sketch_oct31a.ino:93:78: warning: format '%d' expects argument of type 'int', but argument 4 has type 'long unsigned int' [-Wformat=]

gives you a very good indication of what is wrong.

You're passing a long integer to sprintf and asking it to treat the long integer (4 bytes) as an integer (2 bytes)
Doing this has a lot of ramifications on your output.

Study the printf reference. Go through the parameters table carefully and use the correct combination of length and specifier for the type of variable you're using with sprintf()

(I'm not going to comment on the errors)

But if this is the error then why is it possible that it uploads to the database on startup? If this was the error it should not even update on startup of the esp8266. Or is this wrong thinking?

I can't comment on why it would work once only, I've not looked at the code yet.
The warnings about sprintf() do need fixing though. You'll find that the output you do get is probably incorrect.
I'll be in a position to look at the code bit better later on. In the meantime, look at each warning and fix the issue related to it :wink:

darrob:
I can't comment on why it would work once only, I've not looked at the code yet.
The warnings about sprintf() do need fixing though. You'll find that the output you do get is probably incorrect.
I'll be in a position to look at the code bit better later on. In the meantime, look at each warning and fix the issue related to it :wink:

I have looked at the information you gave me and the errors are gone now. I changed it to %ld. Later this week I will be able to test the changed code if this will work.

If you can take a look on the code would be very nice, I will appreciate it. Maybe there is another code error I've made why it shouldn't update.

Best regards Justinn

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include "CRC16.h"

/** Server address wich runs InfluxDB */
const byte eth_server[] = {xx, xx, xx, xx};

/** InfluxDB HTTP port */
const int eth_port = xxxx;

/** Size of the buffer for HTTP content */
const int bufferSize = 2048;

/** An character array filled with null terminate chars */
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; //Meter reading Electrics - return low tariff
unsigned long mEOHT = 0; //Meter reading Electrics - return high tariff
unsigned long mEAV;  //Meter reading Electrics - Actual consumption
unsigned long mEAT = 0;  //Meter reading Electrics - Actual return
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); // (RX, TX. inverted, buffer)
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());
}
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) {
  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]);
    }
    //Serial.println("Start found!");
  }
  else if (endChar >= 0)
  {
    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:2.8.1", strlen("1-0:2.8.1")) == 0)
  // mEOLT = getValue(telegram, len);

  //  if (strncmp(telegram, "1-0:2.8.2", strlen("1-0:2.8.2")) == 0)
  //  mEOHT = getValue(telegram, len);

  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=%ld,", mGAS);
  numChars += sprintf(&buf[numChars], "LAAGTARIEF=%ld,", mEVLT);
  numChars += sprintf(&buf[numChars], "HOOGTARIEF=%ld", mEVHT);
  Serial.print("Sending following dataset to InfluxDB: ");
  Serial.println(buf);
  eth_send_data(buf, numChars);
  memset(buf, '\0', bufferSize);
  delay(1000); //some small delay!
  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)) {
        Serial.print("len_+1");
      }
    }
  }
}
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=p1 HTTP/1.1");
  client.println("Host: www.embedonix.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); //wait for server to process data
  Serial.println("Reply from InfluxDB");
  while (client.connected()) {
    while (client.available()) {
      Serial.print(client.read());
    }
  }
  Serial.println();
  client.stop();
}
void loop() {
  readTelegram();
}

This is the code now. Only a few errors left these are:

C:\Users\justi\Documents\Arduino\p1-meter-influxdb-grafana-upload_on_changement\p1-meter-influxdb-grafana-upload_on_changement.ino:172:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

     validCRCFound = (strtol(messageCRC, NULL, 16) == currentCRC);

                                                      ^

C:\Users\justi\Documents\Arduino\p1-meter-influxdb-grafana-upload_on_changement\p1-meter-influxdb-grafana-upload_on_changement.ino:189:8: warning: unused variable 'val' [-Wunused-variable]

   long val = 0;

        ^

C:\Users\justi\Documents\Arduino\p1-meter-influxdb-grafana-upload_on_changement\p1-meter-influxdb-grafana-upload_on_changement.ino:190:8: warning: unused variable 'val2' [-Wunused-variable]

   long val2 = 0;

        ^
C:\Users\justi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\SoftwareSerial\SoftwareSerial.cpp: In member function 'void SoftwareSerial::rxRead()':

C:\Users\justi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\SoftwareSerial\SoftwareSerial.cpp:241:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

   if (next != m_outPos) {

               ^

It doesn't look like those warning will keep your sketch from running. Does it do what you want?

johnwasser:
It doesn't look like those warning will keep your sketch from running. Does it do what you want?

Goodmorning,

Yesterday I tried the editted code on the p1 meter. It doesn't update at all now. I am seeing the right numbers from the integers: mEVLT, mEVHT and mGAS. But it does not update not even on startup anymore. Is there a problem with the client.connect?

Am I not making the good or stable connection or is it keeping the connection open?

Best regards Justinn

So... What does your serial output say is happening?

I notice that eth_start() returns a true/false value to let you know if the start was successful and you don't bother to check the result.

Are there any other function results that you don't check?

johnwasser:
So... What does your serial output say is happening?

I notice that eth_start() returns a true/false value to let you know if the start was successful and you don't bother to check the result.

Are there any other function results that you don't check?

What do you mean with what the serial output says?

At the moment it shows me the decoded telegram (I mean by this the serial.print which only shows me the data of mEVLT, mEVHT and mGAS at all times).
And on startup it says connected to influxdb and connected to wifi. Is this maybe an problem with reconnecting to the database when trying to send it? Maybe a wrong client.stop?

I don't see the problem with eth_start because when it first tries the connection it connects to the database.

Best regards justinn

Is there maybe another way to disconnect from a database?

Tried it yesterday again and it uploads again on startup maybe connection error.
But it still does not update on changement. I really start thinking that I close the connection wrong?

Any ideas?

void eth_send_data(char* data, int dataSize)
{
  client.connect(eth_server, eth_port);
  int conState = client.connect(eth_server, eth_port);

One idea is to do the connection only once.

johnwasser:

void eth_send_data(char* data, int dataSize)

{
  client.connect(eth_server, eth_port);
  int conState = client.connect(eth_server, eth_port);





One idea is to do the connection only once.

I am only creating a connection in eth_start now:

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

  //do a fast test if we can connect to server
  client.connect(eth_server, eth_port);
  int conState = client.connect(eth_server, eth_port);

  if (conState > 0) {
    Serial.println("Connected to InfluxDB server");
    return true;
  }

  //print the error number and return false
  Serial.print("Could not connect to InfluxDB Server, Error #");
  Serial.println(conState);
  return false;
}

And in update part:

void eth_send_data(char* data, int dataSize) {
  
  int conState = client.connect(eth_server, eth_port);
  if (conState > 0) {
    Serial.println("Connected to InfluxDB server - in send data");
  }
  if (conState < 0) { //check if connection to server is stablished
    client.connect(eth_server, eth_port);
    Serial.print("Could not connect to InfluxDB Server, Error # - in send data");
    Serial.println(conState);
    return;
  }

  //Send HTTP header and buffer
  client.println("POST /write?db=p1 HTTP/1.1");
  client.println("Host: www.embedonix.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); //wait for server to process data

  //Now we read what server has replied and then we close the connection
  Serial.println("Reply from InfluxDB");
  Serial.println(); //empty line
}

In the send part I check if the connection is still there. It now does only update on startup again and not on changement. So keeping the connection open is not helping either. Or am I creating two connections without knowing it?