Skip to content

Instantly share code, notes, and snippets.

@Ozsie
Last active January 8, 2018 20:12
Show Gist options
  • Save Ozsie/e652cfff84cc63753f7dc4928e783183 to your computer and use it in GitHub Desktop.
Save Ozsie/e652cfff84cc63753f7dc4928e783183 to your computer and use it in GitHub Desktop.
Almost there
#include "wifitemp.h"
bool isConfigured() {
return (strlen(conf.wifiPassword) != 0 && strlen(conf.wifiSsid));
}
void setup() {
Serial.begin(115200);
Serial.println("Hello World");
Serial.println("");
Serial.print("EEPROM size required: ");
Serial.println(0+sizeof(conf)+sizeof("OK"));
pinMode(14, INPUT);
int state = digitalRead(14);
if (state == LOW) {
Serial.print("Pin 14 is LOW, resetting EEPROM");
storeEeprom();
}
loadEeprom();
if (!isConfigured()) {
setupServer();
}
}
void loop() {
if (!isConfigured()) {
server.handleClient();
} else if (WiFi.status() != WL_CONNECTED) {
wifiConnect();
} else {
uint16_t attempts = 0;
float temp;
do {
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
Serial.print("Temperature: ");
Serial.println(temp);
delay(100);
attempts++;
} while ((temp > 80.0 || temp == (-127.0)) && attempts < 4);
if (attempts == 4 && (temp > 80.0 || temp == (-127.0))) {
Serial.println("Could not read temperature");
if (temp == (-127.0)) {
Serial.println("Could not communicate with sensor");
}
Serial.println("Sleeping for one minute before retrying");
ESP.deepSleep(retrySleepTimeS * 1000000);
} else {
sendTemperature(temp);
Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);
}
}
}
void wifiConnect() {
Serial.print("Connecting to AP ");
Serial.print(conf.wifiSsid);
WiFi.disconnect();
WiFi.begin(conf.wifiSsid, conf.wifiPassword);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment