Created
November 4, 2020 23:13
-
-
Save elizabethn119/9faefc19aad520531fe709d3c59a703c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
const char* signalname = "ENTER SIGNAL NAME"; | |
//---------------------------------- | |
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; | |
url += "&key="; | |
url += signalname; | |
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