Skip to content

Instantly share code, notes, and snippets.

@sithumonline
Created July 8, 2020 06:41
Show Gist options
  • Select an option

  • Save sithumonline/8c110abfadabab61b18f0d25a0eff249 to your computer and use it in GitHub Desktop.

Select an option

Save sithumonline/8c110abfadabab61b18f0d25a0eff249 to your computer and use it in GitHub Desktop.
/*
Guide from xe-non to P-Use
*/
#include <WiFi.h>
#include <HTTPClient.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
const char *serverName = "https://mern-01.now.sh/api/books";
// 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 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/json");
// Data to send with HTTP POST
int httpResponseCode = http.POST("
{
\"title\":\"Pumal's Book\",
\"isbn\":\"08-07-2020\",
\"author\":\"Sithum Online\"
}
");
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
// Free resources
http.end();
}
else
{
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment