-SOLVED
Hi all
I just need to GET a single string onto a Webserver who has supplied me with an APi and Bearer Authentication key.
I have tried http library, Json library but I can't wrap my head around this one.
I know it works, as I used it with Postman and data is saved on webserver.
How do I implement it on ESP32 ?
Please see the attached HTTP code created by Postman
ARDUINO code based on Rui Santos's work
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-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.
*/
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "Botes****";
const char* password = "Bot******";
//Your Domain name with URL path or IP address with path
String serverName = "https://*********.azurewebsites.net/api/SensorData/StoreString?dataString=";
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// 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 sendHeader(const char* aHeaderName, const char* aHeaderValue);
void loop() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName + "000003Y";
// Your Domain name with URL path or IP address with path
http.begin(serverPath.c_str());
http.sendHeader("Authorization: Bearer eyJhbGciOiJIUzI1Ni******************bl9uYW1lIjoiSm9obiBEb2UiLCJyb2xlIjoiR3*********************9lLmNvbSIsIm5iZiI6MTU5ODUzODE**************NDU4MTU0LCJpYXQiOjE1OTg1MzgxNTR9.cBjUCDVW78********************************");
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
POSTMAN CODE
GET /api/SensorData/StoreString?dataString=000003Y HTTP/1.1
Host: bxxxxxxxxxxx.azurewebsites.net
Authorization: Bearer eyJhbGciOiJxxxxxxxxxxxxxxxxxxxxxxCI6IkpXVCJ9.eyJnaXZlxxxxxxxxxxxxxxxxxxxxdWVfbmFtZSI6ImpvaG5AZG9lLmxxxxxxxxxxxxxxxxxxxxxxxxxxxNjI0NDU4MTU0LCJpYXQiOjxxxxxxxxxxxxE1OTg1MzgxNTR9xxxxxxxxxxxxxxxxxxxxxxxxxoIt84Dxfi_ZGszv1KgvyrM
New Text Document.doc (344 Bytes)