Skip to content

Instantly share code, notes, and snippets.

@elizabethn119
Created November 4, 2020 23:11
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 elizabethn119/f280a66a5fb9ccec241fe5ba35fd6fe0 to your computer and use it in GitHub Desktop.
Save elizabethn119/f280a66a5fb9ccec241fe5ba35fd6fe0 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <HTTPClient.h>
//-------- User Settings -----------
const char* ssid = "WiFi NAME";
const char* password = "WiFi PASSWORD";
const char* accesskey = "ENTER ACCESS KEY HERE";
const char* bucketkey = "ENTER BUCKET KEY HERE";
//----------------------------------
HTTPClient http;
int counter = 0;
static bool hasWifi = false;
//////////////
// Network //
////////////
static void InitWifi()
{
Serial.println("Connecting...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
hasWifi = true;
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup()
{
Serial.begin(115200);
Serial.println("ESP32 Device");
Serial.println("Initializing...");
// Initialize the WiFi module
Serial.println(" > WiFi");
hasWifi = false;
InitWifi();
if (!hasWifi)
{
return;
}
}
//////////////
//Send Data//
////////////
void loop(){
if (WiFi.status() == WL_CONNECTED) { //if we are connected
counter = 0; //reset counter
Serial.println("Wifi is still connected with IP: ");
Serial.println(WiFi.localIP()); //inform user about his IP address
}else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
delay(500);
Serial.print(".");
counter++;
if(counter>=60){ //30 seconds timeout - reset board
ESP.restart();
}
}
// Create a URL for the request
String url = "https://api.init.st/data/v1/events/latest?accessKey=";
url += accesskey;
url += "&bucketKey=";
url += bucketkey;
Serial.print("*** requesting URL");
// ask.begin;
http.begin(url); //Specify the URL
//Check for the returning code
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.print(httpCode);
Serial.print(payload);
} else {
Serial.println("Error on HTTP request");
}
http.end(); //End
Serial.println("*** End ");
delay(10000); // delay
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment