Skip to content

Instantly share code, notes, and snippets.

@dwblair
Last active May 24, 2021 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwblair/731c806170a11bfc536c32d126559495 to your computer and use it in GitHub Desktop.
Save dwblair/731c806170a11bfc536c32d126559495 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "YOURSSID";
const char* password = "YOURPASSWORD";
//Your Domain name with URL path or IP address with path
//const char* serverName = "http://192.168.1.106:1880/update-sensor";
const char* serverName = "https://edgecollective.farmos.net/farm/sensor/listener/47803f44fef9cf01489d033d2eef4424?private_key=9f6a52fb667566b828069794274fad22";
// 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 90 seconds (5000)
unsigned long timerDelay = 90000;
StaticJsonDocument<200> doc;
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() {
doc["temp"] = 23.5;
doc["humidity"] = 50.2;
//serializeJson(doc, Serial);
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
//http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Data to send with HTTP POST
//String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&value1=24.25&value2=49.54&value3=1005.14";
// Send HTTP POST request
//int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: application/json, use the following:
http.addHeader("Content-Type", "application/json");
//int httpResponseCode = http.POST("{\"api_key\":\"tPmAT5Ab3j7F9\",\"sensor\":\"BME280\",\"value1\":\"24.25\",\"value2\":\"49.54\",\"value3\":\"1005.14\"}");
String output;
serializeJson(doc, output);
int httpResponseCode = http.POST(output);
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
@TharushiSuwaris
Copy link

why httpResponseCode results -1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment