Hi guys, still beginner so be soft on my low level of knowledge. (Topic is getting data and posting data from ESP8266)
Trying to get data and post data from sensors to api website. But facing a lot of issues (already tried many examples but it never works for me).
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Code compatible with ESP8266 Boards Version 3.0.0 or above
(see in Tools > Boards > Boards Manager > ESP8266)
*/
unsigned long lastTime = 0;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST
String httpRequestData = "[{\"SensorId\": 1, \"Value\": 77.7}]";
int httpResponseCode = http.POST(httpRequestData);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
the monitor report which I get is:
02:03:41.409 -> .......
02:03:45.239 -> Connected to WiFi network with IP Address: 192.168.xxx.xxx
02:03:45.239 -> Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.
02:03:45.845 -> HTTP Response code: 301
"The HTTP response status code 301 Moved Permanently is used for permanent redirecting, "
Using Azure webSite which is accessible from http and https. In this request I am trying over http.
I tried to find template for HTTPs post from Arduino studio but without success.
So I will update question on: during posting getting 301. Dont know why, over postMan I am able to send HTTPS and HTTP request. Here in above code I am posting by HTTP and getting 301 and HTTPS I was not able to figure out how to send.
Update: when on azure I turn off "only https" (only https = false) I will get answer 307.
( HTTP 307 Temporary Redirect redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location headers.)
Can I get more somehow more information from answer of request?
By this code I get empty string:
The same HTTPS but as POST, just dont work properly.
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-http-get-post-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Code compatible with ESP8266 Boards Version 3.0.0 or above
(see in Tools > Boards > Boards Manager > ESP8266)
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "******";
const char* password = "******";
//Your Domain name with URL path or IP address with path
const char* serverName = "https://*******.azurewebsites.net/api/Measurements/";
unsigned long lastTime = 0;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED)
{
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
HTTPClient https;
https.addHeader("Content-Type", "application/json");
https.addHeader("Content-Length", "65");
https.addHeader("Accept", "*/*");
String httpRequestData = "[{\"SensorId\": 1, \"Value\": 77.7}]";
if (https.begin(*client, serverName))
{
Serial.print("[HTTPS] POST...\n");
int httpCode = https.POST(httpRequestData);
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
{
String payload = https.getString();
Serial.println(payload);
}
else
{
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
}
else
{
Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
}
https.end();
}
else
{
Serial.printf("[HTTPS] Unable to connect\n");
}
}
lastTime = millis();
}
}
Monitor:
18:47:25.886 -> ..
18:47:26.397 -> Connected to WiFi network with IP Address: 192.168.1.189
18:47:26.397 -> Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.
18:47:30.322 -> [HTTPS] POST...
18:47:31.394 -> [HTTPS] POST... code: 415
18:47:31.394 -> [HTTPS] POST... failed, error:
18:47:36.409 -> [HTTPS] POST...
18:47:37.438 -> [HTTPS] POST... code: 415
18:47:37.438 -> [HTTPS] POST... failed, error:
18:47:42.440 -> [HTTPS] POST...
And 415 code is : The HTTP 415 Unsupported Media Type client error response code
May someone give me hint what I am doing wrong? I have feeling that I am coming from one error code to another
I tried even that one. ("application/json" even "text/json")
Not sure what can be wrong.
Do you see in the code some issue or do you think that this code should be "ok" and maybe problem is on server side?
(there is not much to break, I tried it over postman and it never get any error with sending json).
Or do you have any working code for posting for HTTPS (insecured).
I am in the dead end.