Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using the RestClient.h library #14

Open
Ausatel opened this issue May 31, 2021 · 0 comments
Open

Problem using the RestClient.h library #14

Ausatel opened this issue May 31, 2021 · 0 comments
Labels
type: imperfection Perceived defect in any part of project

Comments

@Ausatel
Copy link

Ausatel commented May 31, 2021

Good afternoon. I have a problem using the RestClient.h library. I don’t know if it’s the right place to expose my problem.
I reported the problem to the Arduino forum but I have no answer (https://forum.arduino.cc/t/libreria-restclient-h-fallos-esporadicos-de-conexion/868293)
I have implemented a code, which using POST, GET, ... I consult data, modify records and add information to a database of a server through a web service.
I use the library "RestClient.h"

Everything works, but sometimes, many, too many, I do not get the confirmation data or the data consulted. I was able to do the query or update well three times in a row and fail the next two, I didn't respond (I don't receive any data from the server), and the next query works fine. I don't know why it happens to me and how to locate the problem. The Web service, works well, is checked with an application in VB, in the application VB does not fail. The connection to the network is good and does not fail.

Some idea?

In the code you will see that I use the <avr / wdt.h> to fix if the program does not respond and thus reset it, but it is not a good solution !!!

I'm running it on an Arduino Mega 2560 R3 and an Ethernet shild, I'm running out of memory !!! The connection to the network is by cable.
Thank you and forgive the inconvenience.

#include <Ethernet.h>
#include <SPI.h>
#include "RestClient.h"
#include <avr/wdt.h>   // Control de prenjades.

RestClient client = RestClient("dades.yyyyyyyy.cat");

int idSonda  = 1012;                        // Id de la Sonda, cada sonda ha de portar un Id Unica

int OpenClose = 0;                         // Aquest var indicarà si hem d'obrir el relé o no 0=No 1=Si
int TempsEntreLectures = 1;       // Si son Portes son segons, si son sondes son minuts
int OnOff = 0;                                // 0= no gravem 1=si gravem
int TimeOpen = 500;                   // Temps que tindrem el relé activat 1000 =1segons

char userMySQL[] = "yyyyyyyy";              // MySQL user login username
char passwordMySQL[] = "xxxxxx";        // MySQL user login password

    


void setup() 
  {
  //Autoreset
  wdt_enable(WDTO_8S);  //8segons es el màxim !!!
  //Fi autorreset
  Serial.begin(9600);
  Serial.println();
  Serial.println("Iniciant SETUP...");
  ActivarRele(0);  //Assegurem que el relé està tancat amb un 0.   
  Serial.print("Idsonda:"); Serial.println(idSonda);
  wdt_reset();
  Serial.println("Connectant a la xarxa...");
  client.dhcp();
  wdt_reset();
  LlegirMySQL();
  wdt_reset();
  Serial.println("Fi SETUP");
  Serial.println("*****************");
  delay(1000);
  }


void loop() 
  {
  wdt_reset();
  LlegirMySQL();
  wdt_reset();
  wdt_reset();
  if (OpenClose == 1)
    {
    //Canvia l'estat del rele
    wdt_reset();
    ActivarRele(1);  //Activem el relé amb 1.   
    wdt_reset();
    if (OnOff == 1) 
      {
      ActualitzaMySQL(idSonda, "Pt", 0);  //Canviem l'estat del relé a la BD --> idsonda, tipus de sonda, valor a gravar 
      wdt_reset();
      RegistreMySQL(idSonda, "Pt", 0);    //Creem registre de tracevilitat.
      }
    }
  wdt_disable();
  // Temps que esperem entre captura i captura, en minuts--------------------    
  Esperem (TempsEntreLectures); //esperem 1, 10 minut,...
  // Fi espera entre dades---------------------------------------------------  
  wdt_enable(WDTO_4S);     //wdt_enable(WDTO_8S);
  
  }

//PUT  actualitzar registres dades a la taula de "sondes"
void ActualitzaMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
  {
  int Control = 0 ;
  String response; 
  char idSondaInsertat[6];
  char ValorInsertar[6];  


  Serial.println(" -> Actualitzant dades al MySQL...");
  dtostrf( idSondaSQL , 1, 0, idSondaInsertat);   // zero decimals
  dtostrf( ValorSQL , 1, 0, ValorInsertar);   // dos decimals
 
  char INSERT_POST[] = "/GesRegistresAPI/api/config/1001?";
  char INSERT_BODY[] = "nomUsuari=yyyyyyyy&passwordUsuari=xxxxxx&idSonda=%s&OpenClose=%s&_method=PUT";
  char query[128];

  Repetim:
  sprintf(query, INSERT_BODY,idSondaInsertat,ValorInsertar);         
  Serial.println(query);

  //PUT
  Serial.println(" -> PUT:");  
  response = "";
  client.setHeader("Content-Type: application/x-www-form-urlencoded");
  int statusCode = client.post(INSERT_POST, query, &response);      
  Serial.print("Status code from server: ");
  Serial.println(statusCode);
  if (statusCode != 200) Control++;
  Serial.print("Response body from server: ");
  Serial.println(response);
  if ((statusCode != 200) && ( Control <3)) goto Repetim;
  Serial.println("------------------"); 
  delay(500);   
  }

//POST
void RegistreMySQL(int idSondaSQL, char IdTipusSondaSQL[], float ValorSQL)
//void RegistreMySQL()
  {
  int Control = 0;
  String response; 
  char idSondaInsertat[6];
  char ValorInsertar[6];  

  Serial.println(" -> Gravan registre nou al MySQL...");
  dtostrf( idSondaSQL , 1, 0, idSondaInsertat);   // zero decimals
  dtostrf( ValorSQL , 1, 2, ValorInsertar);   // dos decimals
  
  char INSERT_POST[] = "/GesRegistresAPI/api/registres ";                                                 // <-- *** Línea modificada ***
  char INSERT_BODY[] = "nomUsuari=yyyyyyyy&passwordUsuari=xxxxxx&idSonda=%s&idTipusSonda=%s&valor=%s";  // <-- *** Línea añadida ***
  char query[128];

  Repetim:  
  sprintf(query, INSERT_BODY, idSondaInsertat, "Pt", ValorInsertar);           
  Serial.println(query);

  //POST
  Serial.println(" -> POST:");  
  response = "";
  client.setHeader("Content-Type: application/x-www-form-urlencoded");
  int statusCode = client.post(INSERT_POST, query, &response);      
  Serial.print("Status code from server: ");
  Serial.println(statusCode);
  if (statusCode != 200) Control++;  
  Serial.print("Response body from server: ");
  Serial.println(response);
  if ((statusCode != 200) && ( Control <3)) goto Repetim;
  Serial.println("------------------");  
  delay(500);  
  }


//GET
void LlegirMySQL()
  {
  int Control = 0;  
  char *s;
  char *strings[10];
  String response;   
  char SELECT_GET[] = "/GesRegistresAPI/api/config?nomUsuari=yyyyyyyy&passwordUsuari=xxxxxx&idSonda=%s";
  char query[128];
   
  Serial.println(" -> Llegint dades del MySQL...");
  char idSonaInsertat[6];
  dtostrf( idSonda, 1, 0, idSonaInsertat);   // zero decimals  
  sprintf(query, SELECT_GET, idSonaInsertat);
  Repetim:
  Serial.println(query);
  Serial.println(" -> GET:");
  response = "";
  client.setHeader("Content-Type: application/x-www-form-urlencoded");
  //int statusCodeGet = client.get("/GesRegistresAPI/api/config?nomUsuari=yyyyyyyy&passwordUsuari=xxxxxx&idSonda=1002", &response);
  int statusCodeGet = client.get(query, &response);
  Serial.print("Status code from server: ");
  Serial.println(statusCodeGet);  //si es 200 es que ha retornar dades correctament
  if (statusCodeGet != 200) Control++;
  if (statusCodeGet==200)
    {
    Serial.println("Lectura correcte, les dades son: ");
    // "idSonda""idClient""AlarmaMinima""AlarmaMaxima""Data""idTipusSondes""Calibratge""Periode""OnOff""OpenClose""TimeOpne"
    // 1012;1001;0;0;2021-05-20 00:00:00;Pt;0;20;1;0;1000
    Serial.println(response);
    s = strtok(response.c_str(), ";");
    int index=0;
    while (s!=NULL) 
      {
      strings[index]= s;  
      s=strtok(NULL, ";");
      index++;
      }
    for(int n = 0; n <= index; n++)
      { 
      Serial.print(n);Serial.print("-->");Serial.println(strings[n]);
      if (n==7) TempsEntreLectures = atol(strings[n]);    //Temps entre lectures
      if (n==8) OnOff = atol(strings[n]);                             //Grabem les dades 1=SI i 0=NO
      if (n==9) OpenClose = atol(strings[n]);                    //Obrir porta 1=SI i 0=NO
      if (n==10) TimeOpen = atol(strings[n]);                  //Temps que tindrem el relé clavat
      if (TimeOpen >3000 ) TimeOpen = 3000;              //Maxim temps 3 segons
      }    

    // Mostra els resultats
    Serial.println("------------------------------------"); 
    Serial.print(" -> Temps entre lectures = ");
    Serial.println(TempsEntreLectures);  
    Serial.print(" ->                OnOff = ");
    Serial.println(OnOff);     
    Serial.print(" ->            OpenClose = ");
    Serial.println(OpenClose);  
    Serial.print(" ->             TimeOpen = ");
    Serial.println(TimeOpen);         
    //Fi Calibratge    
    }
  else
    {
    Serial.println("Error al llegir les dades.");  
    if ((statusCodeGet != 200) && ( Control <3)) goto Repetim;    
    }
  Serial.println("------------------------------------"); 
  delay(500);  
  }

void ActivarRele(int Valor)   // ha de ser 0 o 1
  {
  int pin = 9;   //Pin digital 9

  Serial.println(" -> Rele:");
  Serial.println("    HIGH");
  if (Valor == 0) digitalWrite(pin, LOW);       // poner el Pin en LOW 
  if (Valor == 1) digitalWrite(pin, HIGH);      // poner el Pin en HIGH 
  wdt_reset();  
  delay(TimeOpen);                 // manetenim el relé clavat 
  wdt_reset();   
  Serial.println("    LOW");
  digitalWrite(pin, LOW);        // poner el Pin en LOW  sempre acava en "0"
  }

// Funció temps d'espera entre captura de dades, en minuts-------------------
void Esperem (int minuts)
  {
  Serial.print(" -> Esperant (minuts) :");
  Serial.print( minuts);
  Serial.print(" ");
  for (int a=0; a<minuts; a++)
    {      
    delay(1000);  //60000 es un minut   si son sondes 60000 pq son minuts. Si son portes, segons i posem 1000
    Serial.print(".");
    }
  Serial.println (" ");  
  }
// Fi funció temps d'espera--------------------------------------------------
@per1234 per1234 added the type: imperfection Perceived defect in any part of project label May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants