HTTP GET using Ethernet shield getting interrupted

Hello all,

I'm using Arduino UNO with Ethernet Shield to send data to a data base with the aid of a php script.

The database is a paid subscription so it doesn't have the problems that often come from free hosting.

Arduino collects data from multiple sensors and sends them to the cloud every 1 minute (later i will make it 5 but for now i need it to be 1)
So the problem is that i am able to upload data for once or twice and then it will fail for a couple of times and then it will upload again, so i really cannot understand that behavior and what is causing it.....

I can't afford to lose any data from the project i'm building, and it seems the code isn't able to keep on uploading

Also, please note that i tried to execute the HTTP GET for big amount of time using just a browser to figure out if the issue is from the server, but when using the browser i get all of them which means the server is ok

Here is an example of what the serial monitor shows:

8:22:51.886 -> connecting to realtimepv.alcoholdatabase.net...
18:22:51.974 -> Connected to server
18:22:51.974 -> GET /PVInsert.php?voltage=20.57&current=0.21&lux=10.71&AmbTemp=28.00&SurTemp=10&Power=10 HTTP/1.1
18:22:52.069 -> HTTP/1.1 400 Bad Request
18:22:52.119 -> Con
18:22:52.119 -> disconnected
18:22:58.073 -> connecting to realtimepv.alcoholdatabase.net...
18:22:58.317 -> Connected to server
18:22:58.364 -> GET /PVInsert.php?voltage=20.57&current=0.21&lux=10.71&AmbTemp=28.00&SurTemp=10&Power=10 HTTP/1.1
18:22:58.552 -> HTTP/1.1 200 OK
18:22:58.599 -> Connection: close
18:22:58.599 -> x-powered-by: PHP/7.4.30
18:22:58.646 -> content-type: text/html; charset=UTF-8
18:22:58.693 -> content-length: 33
18:22:58.693 -> date: Fri, 08 Jul 2022 10:22:59 GMT
18:22:58.740 -> server: LiteSpeed
18:22:58.740 ->
18:22:58.740 ->
18:22:58.740 -> New record created successfully
18:22:58.895 -> disconnected
18:23:04.915 -> connecting to realtimepv.alcoholdatabase.net...
18:23:04.962 -> connection failed
18:23:05.008 -> connecting to realtimepv.alcoholdatabase.net...
18:23:05.054 -> connection failed
18:23:05.102 -> connecting to realtimepv.alcoholdatabase.net...
18:23:05.149 -> connection failed
18:23:05.193 -> connecting to realtimepv.alcoholdatabase.net...
18:23:05.427 -> Connected to server
18:23:05.427 -> GET /PVInsert.php?voltage=20.57&current=0.25&lux=10.71&AmbTemp=28.00&SurTemp=10&Power=10 HTTP/1.1
18:23:05.663 -> HTTP/1.1 400 Bad Request
18:23:05.663 -> Connection: close
18:23:05.709 -> cache-control: private, no-cache, no-store, must-revalidate, max-age=0
18:23:05.755 -> pragma: no-cache
18:23:05.802 -> content-type: text/html
18:23:05.802 -> content-length: 681
18:23:05.849 -> date: Fri, 08 Jul 2022 10:23:06 GMT
18:23:05.897 -> server: LiteSpeed
18:23:05.897 ->
18:23:05.897 -> disconnected
18:23:11.890 -> connecting to realtimepv.alcoholdatabase.net...
18:23:12.123 -> Connected to server
18:23:12.123 -> GET /PVInsert.php?voltage=20.57&current=0.21&lux=10.71&AmbTemp=28.00&SurTemp=10&Power=10 HTTP/1.1
18:23:12.355 -> HTTP/1.1 200 OK
18:23:12.355 -> Connection: close
18:23:12.401 -> x-powered-by: PHP/7.4.30
18:23:12.401 -> content-type: text/html; charset=UTF-8
18:23:12.448 -> content-length: 33
18:23:12.495 -> date: Fri, 08 Jul 2022 10:23>12 GMT
18:23:12.542 -> server: LiteSpeed
18:23:12.542 ->
18:23:12.542 ->
18:23:12.542 -> New record created successfully
18:23:12.683 -> disconnected

I need some ideas and thoughts on how to solve this issue

Here is my code

#include <Ethernet.h>
EthernetClient client;

#include <DFRobot_B_LUX_V30B.h>
DFRobot_B_LUX_V30B    myLux(13);//The sensor chip is set to 13 pins, SCL and SDA adopt default configuration

#include <dht.h>
#define dataPin 3 // Defines pin number to which the sensor is connected
dht DHT; // Creats a DHT object



byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


int STATE = 0 ;
int sending = 1;
int tryingagain = 2;
int delaying = 3;


// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

int HTTP_PORT = 80;
String HTTP_METHOD = "GET";
char HOST_NAME[] = "realtimepv.alcoholdatabase.net";
String PATH_NAME = "/PVInsert.php?";


int SurTemp = 10;
int Power = 10;


void setup() {
  Serial.begin(9600);
  ethernetsetup();
  STATE = sending;
  delay(1000);
  myLux.begin();
}





void loop() {
  Ethernet.maintain();


  float lux = myLux.lightStrengthLux();
  // read the input on analog pin 0 & 1:
  int sensorVol = analogRead(A0);
  int sensorAmp = analogRead(A1);

  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = 13.07 * sensorVol * (5.0 / 1023.0);
  float current = 8.4 * sensorAmp * (5.0 / 1023.0);

  /* // print out the value you read:
    Serial.print("Voltage: ");
    Serial.print(voltage, 2); //print the voltge
    Serial.print("V\t\t");
    Serial.print("Current: ");
    Serial.print(current, 3); //print the voltge
    Serial.println("A");
    //Serial.print("Analog Reading: ");
    //Serial.println(sensorValue);//print the voltge
    delay(500);*/

  int readData = DHT.read22(dataPin); // Reads the data from the sensor
  float AmbTemp = DHT.temperature; // Gets the values of the temperature


  if (STATE == sending) {
    Ethernet.maintain();
    String queryString = String("voltage=" + String(voltage) + String("&current=") + String(current) + String("&lux=") + String(lux) + String("&AmbTemp=") + String(AmbTemp) + String("&SurTemp=") + String(SurTemp) + String("&Power=") + String(Power));
    //////////////////////////ethernet code///////////////////////////////
    Serial.print("connecting to ");
    Serial.print(HOST_NAME);
    Serial.println("...");
    // connect to web server on port 80:
    if (client.connect(HOST_NAME, HTTP_PORT)) {
      // if connected:
      Serial.println("Connected to server");
      // make a HTTP request:
      // send HTTP header
      client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
      Serial.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
      client.println("Host: " + String(HOST_NAME));
      client.println("Connection: close");
      client.println(); // end HTTP header

      while (client.connected()) {
        if (client.available()) {
          // read an incoming byte from the server and print it to serial monitor:
          char c = client.read();
          Serial.print(c);
        }
      }

      // the server's disconnected, stop the client:
      client.stop();
      Serial.println();
      Serial.println("disconnected");
      client.flush();
      STATE = delaying;
    }

    else {// if not connected:
      client.flush();
      Serial.println("connection failed");
      STATE = tryingagain;
    }
  }


  else if (STATE == delaying) { //reporting once every minute 
    delay (60000);
    STATE = sending;
  }

  else if (STATE == tryingagain) {// when i get a fail, though it is not fixing my issue, it is still returning bad request 
    delay(1);
    STATE = sending;
  }

}



void ethernetsetup() {
  // initialize the Ethernet shield using DHCP:
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  Serial.println("HI");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }

    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }

    // try to configure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
  }

  else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
}

What does the server logs say? For me this looks like a very bad connection to that server or a server very picky about how small or big network packets must be.

Just because it's paid doesn't ensure good quality, unfortunately.

this is the response I get

i got it from HOSTINGER

This is what you get on the client. Don't you have access to the server logs?

It looks like your server answers with a 400 result code before it got the complete request. Without server logs you probably won't be able to find the reason for this.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.