Page 1 of 1

ESP32 http.get sometimes return error

Posted: Sat Sep 01, 2018 11:29 pm
by Pcborges
Hi, my project is to collect data from a sensor and send it to a mysql table at the internet.
The code I am testing is as bellow:

Code: Select all

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid     = "Skynet";
const char* password = "xaxaxa";
const char* host     = "myhost.com";
const char* protStr  = "http://";

int    jd    = 1234567;
char  *area  = "Area01";
char   type  = 'h';
int    value = 100;

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

    // We start by connecting to a WiFi network
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void loop(){
  String  url;
  if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
    url  = protStr;
    url += host;
    url += "/esp32/insert.php?";
    url += "jd=";            
    url += jd;
    url += "&area=";            
    url += area;
    url += "&type=";
    url += 'h';
    url += "&value=";
    url += value;
    value++;

//    Serial.print("Requesting URL: ");
//    Serial.println(url);
    
    HTTPClient http;
    http.begin(url); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
    if (httpCode > 0) { //Check for the returning code
      int payload = http.getString().toInt();
      Serial.print(httpCode);
      Serial.print(" - ");
      Serial.print(payload);
      Serial.print("  -  ");
      Serial.println(value);
    } else {
      Serial.print("Error on HTTP request");
      Serial.print("  -  ");
      Serial.println(value);      
    }
    http.end(); //Free the resources
  }
  delay(2000);
}
But it sometimes return errors as follow:
200 - 0 - 232
Error on HTTP request - 233
200 - 0 - 234
200 - 0 - 235
200 - 0 - 236
200 - 0 - 237
200 - 0 - 238
200 - 0 - 239
200 - 0 - 240
200 - 0 - 241
200 - 0 - 242
200 - 0 - 243
200 - 0 - 244
200 - 0 - 245
200 - 0 - 246
200 - 0 - 247
Error on HTTP request - 248
200 - 0 - 249

Although records reported on error as above (233 and 248) they have been recorded at the Mysql table.
The bigger problem is that sometimes, although it reports OK (200 - 0 - XXX) the record is missing at the mysql table.

The PHP code involved is as follow:

Code: Select all

<?php
   try{
      $HOST= "mysqlcluster7.registeredsite.com";
      $DB  = "esp32";
      $USER = "xxxxxxxxx";
      $PASS =  "xxxxxxxxx";

      $PDO = new PDO("mysql:host=" . $HOST . ";dbname=" . $DB . ";charset=utf8", $USER, $PASS);
      
    } catch (PDOexception $erro){
      echo "Connection Error: " . $erro->getMessage();
    }
?>
And:

Code: Select all

<?php
   include('connection.php'); 
   $jd    = $_GET['jd'];   
   $area  = $_GET['area'];   
   $type  = $_GET['type'];   
   $value = $_GET['value']; 
   
   $sql = "INSERT INTO activity (jd,area,type,value) VALUES (:jd,:area,:type,:value)";   

   $stmt = $PDO->prepare($sql);   
   $stmt->bindParam(':jd',$jd);
   $stmt->bindParam(':area',$area);
   $stmt->bindParam(':type',$type);
   $stmt->bindParam(':value',$value);

   if($stmt->execute()) {
       echo "Sucessful SQL Insertion";
    } else {
      echo "SQL Insert Failed.";  
    }
?>
I am puzzled...
Assistance welcome.
Thanks
Paulo

Re: ESP32 http.get sometimes return error

Posted: Sun Sep 02, 2018 6:59 pm
by boarchuz
You're not giving yourself much information to work with.
With that code, it's treating every response as "OK" even if the server responds with an error or your database operations fail.
You want to at least print the debug output in your php for a start:

Code: Select all

if (httpCode > 0) { 
	//Something like this:
	Serial.print(http.getString());   //eg. "SQL Insert Failed."
} 
And if httpCode <= 0, surely you want to debug that too rather than printing a generic "Error on HTTP request" which tells you nothing.

Code: Select all

else {
	//What was the error...?
      Serial.print( http.errorToString(httpCode) );
    }