Last active
January 7, 2020 16:27
-
-
Save elizabethn119/44f43dff9a2e154b4a4ba0119e129e97 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> | |
#include "DHT.h" | |
#define DHTPIN 4 // Digital pin connected to the DHT sensor | |
#define DHTTYPE DHT22 | |
DHT dht(DHTPIN, DHTTYPE); | |
//-------- User Settings ----------- | |
const char* ssid = "SSID"; | |
const char* password = "PASSWORD"; | |
const char* accesskey = "INITIAL STATE ACCESS KEY"; | |
const char* bucketkey = "INITIAL STATE BUCKET KEY"; | |
const char* signalname = "temperature"; | |
//---------------------------------- | |
HTTPClient ask; | |
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..."); | |
dht.begin(); | |
// 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(); | |
} | |
} | |
// Reading temperature or humidity | |
float t = dht.readTemperature(true); | |
// Create a URL for the request | |
String url = "https://groker.init.st/api/events?accessKey="; | |
url += accesskey; | |
url += "&bucketKey="; | |
url += bucketkey; | |
url += "&"; | |
url += signalname; | |
url += "="; | |
url += t; | |
Serial.print("*** requesting URL"); | |
// ask.begin; | |
ask.begin(url); //Specify the URL | |
//Check for the returning code | |
int httpCode = ask.GET(); | |
Serial.print(httpCode); | |
if (httpCode > 0) { | |
String payload = ask.getString(); | |
} else { | |
Serial.println("Error on HTTP request"); | |
} | |
ask.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