Skip to content

Instantly share code, notes, and snippets.

@DzikuVx
Created February 4, 2017 09:49
Show Gist options
  • Save DzikuVx/0f6c3fa95b8f7d4e0f29f1975d8e418a to your computer and use it in GitHub Desktop.
Save DzikuVx/0f6c3fa95b8f7d4e0f29f1975d8e418a to your computer and use it in GitHub Desktop.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266mDNS.h>
#include <ESP8266WiFi.h>
#define ALLOWED_CONNECT_CYCLES 40
#define ALLOWED_READ_CYCLES 80
#define ONE_WIRE_BUS 2 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
/*
* Update those 3 line to configure WiFi and ThingSpeak project
*/
const String apiKey = "[THINGSPEAK_API_KEY_GOES_HERE]";
const String wifiSSID = "[WIFI_NETWORK_SSID_GOES_HERE]";
const String wifiPassword = "[WIFI_NETWORK_PASSWORD_GOES_HERE]";
#define SLEEP_TIME_SECONDS 900 //900 for 15 minutes
const char* host = "api.thingspeak.com";
void setup() {
/*
* If after changing WiFi settings (SSID or password) ESP8266
* does not want to connect again, uncomment this line and reflash
*/
//ESP.eraseConfig();
WiFi.begin(wifiSSID, wifiPassword);
uint8_t counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
counter++;
if (counter > ALLOWED_CONNECT_CYCLES) {
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
}
WiFiClient client;
const int httpPort = 80;
if (client.connect(host, httpPort)) {
digitalWrite(0, HIGH);
float temperature;
counter = 0;
do {
DS18B20.requestTemperatures();
temperature = DS18B20.getTempCByIndex(0);
counter++;
if (counter > ALLOWED_READ_CYCLES) {
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
} while (temperature == 85.0 || temperature == (-127.0));
client.print(String("GET /update?api_key=" + apiKey + "&field2=" + String(temperature)) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
}
delay(2000);
ESP.deepSleep(SLEEP_TIME_SECONDS * 1000000);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment