[SOLVED] Arduino Mega & ethernet shield - "Bad request" (error 400)

Hi guys,

I’m working on a project but i have some troubles with the Ubidots Ethernet library.
Here is my code :
#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
#include “DHT.h”
#include “MQ135.h”

#define VARLABEL_1  "*******" //temperature  
#define VARLABEL_2  "*******" //humidity 
#define VARLABEL_3  "*******" //luminosity
#define VARLABEL_4  "*******" //lightOn
#define VARLABEL_5  "*******" //PPM
#define VARLABEL_6  "*******" //Pot 1
#define VARLABEL_7  "*******" //Pot 2
#define VARLABEL_8 "*******" //Pot 3
#define VARLABEL_9 "*******" //Pot 4

#define TOKEN  "*******"  

// DHT sensor
#define DHTPIN 7
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

// Light level sensor
#define LIGHT_SENSOR_PIN A0

//CO2
const int mq135Pin = 1;

//Humidity sensor
byte humidity_sensor_pin1 = A5;
byte humidity_sensor_vcc1 = 48;

byte humidity_sensor_pin2 = A4;
byte humidity_sensor_vcc2 = 36;

byte humidity_sensor_pin3 = A3;
byte humidity_sensor_vcc3 = 24;

byte humidity_sensor_pin4 = A2;
byte humidity_sensor_vcc4 = 3;


MQ135 gasSensor = MQ135(mq135Pin);
#define RZERO 63.11

const int ledpin = 2;

unsigned long lastSend;
float delai = 5000; //by default, delay between two checks is 5 sec.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 177);

Ubidots client(TOKEN);

void setup(){
    lastSend = 0;
    pinMode(ledpin, OUTPUT);
    digitalWrite(ledpin, LOW);    

    pinMode(humidity_sensor_vcc1, OUTPUT);
    digitalWrite(humidity_sensor_vcc1, LOW);
    pinMode(humidity_sensor_vcc2, OUTPUT);
    digitalWrite(humidity_sensor_vcc2, LOW);
    pinMode(humidity_sensor_vcc3, OUTPUT);
    digitalWrite(humidity_sensor_vcc3, LOW);
    pinMode(humidity_sensor_vcc4, OUTPUT);
    digitalWrite(humidity_sensor_vcc4, LOW);
    
    // Init DHT sensor
    dht.begin();
    
    Serial.begin(9600);
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);

    digitalWrite(ledpin, HIGH);

    //Get a value from Ubidots : delay
    delai = client.getValue("58dba6c27625427df35f19aa") * 1000;
    Serial.print("Délai récupéré :");
    Serial.print(delai);
    Serial.println(" ms");
    
}
void loop(){

    if ( millis() - lastSend > delai ) {
      getAndSendDatas();
      lastSend = millis();
    }
}

void getAndSendDatas()
{

Serial.println("Collecting data.");
  
    // Reading temperature or humidity takes about 250 milliseconds!
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
  
    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
  
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.println(" *C ");
  


    //Reading luminosity value
    float luminosity = analogRead(LIGHT_SENSOR_PIN) / 1024.*100.;
    float lightOn = 0;
    if (luminosity <= 28)
      lightOn = 1;
    else
      lightOn = 0;

    Serial.print("Luminosity: ");
    Serial.println(luminosity);
    

    //Reading CO2 value
    float ppm = gasSensor.getPPM();
    double rzero = gasSensor.getRZero();

    Serial.print("PPM: ");
    Serial.println(ppm);
    
    //lecture de l'humidité
    digitalWrite(humidity_sensor_vcc1, HIGH);
    delay(500);
    int value = analogRead(humidity_sensor_pin1);
    digitalWrite(humidity_sensor_vcc1, LOW);
    int humiditySensor1 = map(value, 1003, 216, 0, 100);
    Serial.print("humiditySensor1=");
    Serial.println(humiditySensor1);
    
    digitalWrite(humidity_sensor_vcc2, HIGH);
    delay(500);
    value = analogRead(humidity_sensor_pin2);
    digitalWrite(humidity_sensor_vcc2, LOW);
    int humiditySensor2 = map(value, 1003, 216, 0, 100);
    Serial.print("humiditySensor2=");
    Serial.println(humiditySensor2);
    
    digitalWrite(humidity_sensor_vcc3, HIGH);
    delay(500);
    value = analogRead(humidity_sensor_pin3);
    digitalWrite(humidity_sensor_vcc3, LOW);
    int humiditySensor3 = map(value, 1003, 216, 0, 100);
    Serial.print("humiditySensor3=");
    Serial.println(humiditySensor3);
    
    digitalWrite(humidity_sensor_vcc4, HIGH);
    delay(500);
    value = analogRead(humidity_sensor_pin4);
    digitalWrite(humidity_sensor_vcc4, LOW);
    int humiditySensor4 = map(value, 1003, 216, 0, 100);        
    Serial.print("humiditySensor4=");
    Serial.println(humiditySensor4);      

    Serial.println("**** FIRST SEND *****");      
    client.add(VARLABEL_1, t);
    client.add(VARLABEL_2, h);
    client.add(VARLABEL_3, luminosity);
    client.add(VARLABEL_4, lightOn);
    client.add(VARLABEL_5, ppm);
    client.sendAll();

    delay (2000);
    Serial.println("**** SECOND SEND *****");      
    client.add(VARLABEL_6, humiditySensor1);
    client.add(VARLABEL_7, humiditySensor2);
    client.add(VARLABEL_8, humiditySensor3);
    client.add(VARLABEL_9, humiditySensor4);
    client.sendAll();
}

And here are my Serial monitor :
Geting your variable
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:09 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

Délai récupéré :0.00 ms
Collecting data.
Humidity: 61.20 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:12 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:15 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.20 %	Temperature: 21.90 *C 
Luminosity: 5.96
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:18 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:21 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Collecting data.
Humidity: 61.10 %	Temperature: 21.90 *C 
Luminosity: 5.96
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:24 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:28 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.00 %	Temperature: 21.90 *C 
Luminosity: 5.96
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:31 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:34 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.00 %	Temperature: 21.90 *C 
Luminosity: 5.96
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:37 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
**** SECOND SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:40 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Collecting data.
Humidity: 61.60 %	Temperature: 21.90 *C 
Luminosity: 5.96
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:44 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:47 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.80 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:50 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
**** SECOND SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:04:53 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Collecting data.
Humidity: 62.00 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:04:57 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:00 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.60 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:03 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:05:06 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Collecting data.
Humidity: 61.30 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:32 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.20 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:35 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:05:38 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Collecting data.
Humidity: 61.10 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 382.37
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:41 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:44 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.10 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:48 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

6e
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

**** SECOND SEND *****
Posting your variables
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 12 Apr 2017 16:05:51 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Vary: Accept, Cookie
Allow: POST, OPTIONS

58
[{"status_code": 201}, {"status_code": 201}, {"status_code": 201}, {"status_code": 201}]
0

Collecting data.
Humidity: 61.70 %	Temperature: 21.90 *C 
Luminosity: 6.05
PPM: 383.69
humiditySensor1=0
humiditySensor2=0
humiditySensor3=0
humiditySensor4=0
**** FIRST SEND *****
Posting your variables
HTTP/1.1 400 Bad Request
Server: nginx
Date: Wed, 12 Apr 2017 16:05:54 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

As you can see, sometimes it works : i can send data to ubidots.
But sometimes it doesn’t work : error 400

Do you have an idea ?

Thanks !

AstroMan

Hello @AstroMan,

If you use the example provided by Ubidots you are still getting the bad request?

To correct your issue please make sure you’re using the last version of our library, and try the example provided on the README.

Let me know how it goes!

Best Regards,
Maria C.