Skip to content
Snippets Groups Projects

9: installed libraries for http post

Merged Ben Luno Becker requested to merge feature/ard-input-send/9 into main
1 unresolved thread
16 files
+ 5367
0
Compare changes
  • Side-by-side
  • Inline
Files
16
+ 72
0
 
#include <ESP8266WiFi.h>
 
#include <ESP8266HTTPClient.h>
 
#include <WiFiClient.h>
 
 
const char* ssid = "REPLACE_WITH_YOUR_SSID";
 
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
 
 
//Your Domain name with URL path or IP address with path
 
String serverName = "http://192.168.1.106:1880/update-sensor";
 
 
// 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 loop() {
 
// Send an HTTP POST request depending on timerDelay
 
if ((millis() - lastTime) > timerDelay) {
 
//Check WiFi connection status
 
if(WiFi.status()== WL_CONNECTED){
 
WiFiClient client;
 
HTTPClient http;
 
 
String serverPath = serverName + "?temperature=24.37";
 
 
// Your Domain name with URL path or IP address with path
 
http.begin(client, serverPath.c_str());
 
 
// If you need Node-RED/server authentication, insert user and password below
 
//http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");
 
 
// 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();
 
}
 
}
 
\ No newline at end of file
Loading