Bad request 400 | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Bad request 400
Avatar
Bulldogbob
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
December 8, 2017 - 5:37 am
sp_Permalink sp_Print

Hello , i use a WeMos D1 mini with a BME280 on my channel . It has worked with this sketch , but now i get bad request 400 . What went wrong ?? 

 

#include <BME280_MOD-1022.h>
#include <Wire.h>

// Wifi and ThingSpeak settings
#include <ESP8266WiFi.h>

const char* ssid = "XXXXXX";
const char* password = "XXXXXXXXX";

const char* server = "api.thingspeak.com";
const char* api_key = "DFFSB76BCWGXXXX";

// Measurement interval (seconds)
const int interval = 300; //5 mins

#define LED D4

WiFiClient client;

void printFormattedFloat(float x, uint8_t precision) {
char buffer[10];

dtostrf(x, 7, precision, buffer);
Serial.print(buffer);

}

void printCompensatedMeasurements(void) {

float temp, humidity, pressure, pressureMoreAccurate;
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate;
char buffer[80];

temp = BME280.getTemperature();
humidity = BME280.getHumidity();
pressure = BME280.getPressure();

pressureMoreAccurate = BME280.getPressureMoreAccurate(); // t_fine already calculated from getTemperaure() above

tempMostAccurate = BME280.getTemperatureMostAccurate();
humidityMostAccurate = BME280.getHumidityMostAccurate();
pressureMostAccurate = BME280.getPressureMostAccurate();

Serial.print("Temperature: ");
printFormattedFloat(tempMostAccurate, 2);
Serial.println();

Serial.print("Humidity: ");
printFormattedFloat(humidityMostAccurate, 2);
Serial.println();

Serial.print("Pressure: ");
printFormattedFloat(pressureMostAccurate, 2);
Serial.println();

// Post data to ThingSpeak
postData(tempMostAccurate, humidityMostAccurate, pressureMostAccurate);
Serial.println();
}

void postData(float temperature, float humidity, float pressure){
// Send data to ThingSpeak
if (client.connect(server,80)) {
Serial.println("Connect to ThingSpeak - OK");

String dataToThingSpeak = "";
dataToThingSpeak+="GET /update?api_key=";
dataToThingSpeak+=api_key;

dataToThingSpeak+="&field1=";
dataToThingSpeak+=String(temperature);

dataToThingSpeak+="&field2=";
dataToThingSpeak+=String(humidity);

dataToThingSpeak+="&field3=";
dataToThingSpeak+=String(pressure);

dataToThingSpeak+=" HTTP/1.1
Host: a.c.d
Connection: close

";
dataToThingSpeak+="";
client.print(dataToThingSpeak);

int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println("Error: Client Timeout!");
client.stop();
return;
}
}
}
while(client.available()){
String line = client.readStringUntil('
');
Serial.print(line);
}
}

 

// Setup wire and serial
void setup()
{
Wire.begin();
Serial.begin(115200);
pinMode(LED, OUTPUT);
delay(10);
Serial.println("Connecting to wifi...");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED){

// Blink LED when connecting to wifi
digitalWrite(LED, LOW);
delay(250);
digitalWrite(LED, HIGH);
delay(250);
}
Serial.println("WiFi connected");

// Prepare LED to turn on when measuring and send data

}

// main loop
void loop()
{
// need to read the NVM compensation parameters
BME280.readCompensationParams();

// We'll switch into normal mode for regular automatic samples
BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms
BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16
BME280.writeOversamplingPressure(os16x); // pressure x16
BME280.writeOversamplingTemperature(os2x); // temperature x2
BME280.writeOversamplingHumidity(os1x); // humidity x1

BME280.writeMode(smNormal);

while (1) {
//digitalWrite(LED, LOW);
while (BME280.isMeasuring()) {
//Serial.println("Measuring...");
//delay(100);
}

// read out the data - must do this before calling the getxxxxx routines
BME280.readMeasurements();
printCompensatedMeasurements();
// digitalWrite(LED, HIGH);

delay(interval*1000);
Serial.println();
}
}

Avatar
cstapels

Gold
Forum Posts: 129
sp_UserOfflineSmall Offline
2
December 11, 2017 - 9:08 am
sp_Permalink sp_Print

Can you look at the output that is generated from "client.print(dataToThingSpeak);"  You could add a Serial.println(dataToThingSpeak); and then copy and paste that line to the address bar in your browser (don't forget to delete the "GET").  This might help show why it is failing. Can you post it here?

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
27 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

epicpaymentsny

DanielBobby

mercedeszw18

ujuxoketate

olihfez

winnieeq69

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee