Created
July 1, 2020 11:27
-
-
Save terryspitz/66b0c99698646fec8e7020642f101179 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 <ArduinoOTA.h> | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <WiFiManager.h> | |
#include <BlynkSimpleEsp8266.h> | |
// Auth Token for Blynk App. | |
// see http://docs.blynk.cc/#getting-started-getting-started-with-the-blynk-app-4-auth-token | |
char blynkAuth[] = "<your code here>"; | |
OneWire ds(D4); //Data pin | |
DallasTemperature sensors(&ds); | |
BlynkTimer timer; | |
void setup() { | |
Serial.begin(115200); | |
Serial.println("Starting...."); | |
pinMode(D2, OUTPUT); digitalWrite(D2, LOW); //GND pin | |
WiFiManager wifiManager; | |
// wifiManager.setAPCallback(configModeCallback); | |
wifiManager.setTimeout(180); //in seconds | |
//wifiManager.resetSettings(); //for testing | |
//fetches configured ssid and password and tries to connect | |
if (!wifiManager.autoConnect("TemperatureSensor")) { | |
Serial.println("Failed WiFi connect: Restarting..."); | |
delay(3000); | |
//reset and try again | |
ESP.reset(); | |
delay(5000); | |
} | |
Serial.println("WiFi Connected:" + WiFi.SSID()); | |
delay(1000); | |
Serial.println("Connect to Blynk..."); | |
Blynk.config(blynkAuth); | |
blynkConnect(); | |
Blynk.virtualWrite(V0, -1); | |
sensors.begin(); | |
//sensors.setWaitForConversion(false); | |
Serial.println("Found " + String(sensors.getDeviceCount()) + " temperature sensors."); | |
timer.setInterval(5000L, readTemp); | |
} | |
void blynkConnect() { | |
Serial.println("Blynk reconnecting"); | |
if (!Blynk.connect()) | |
{ | |
Serial.println("Failed Blynk connect, Restarting..."); | |
delay(3000); | |
//reset and try again | |
ESP.reset(); | |
delay(1000); | |
} | |
Serial.println("Connect to Blynk: OK"); | |
} | |
void readTemp() { | |
Serial.print("Requesting..."); | |
sensors.requestTemperatures(); | |
float temp = sensors.getTempCByIndex(0); | |
Serial.println("Sending temp: " + String(temp)); | |
Blynk.virtualWrite(V0, temp); | |
Blynk.virtualWrite(V1, millis() / 1000); | |
} | |
void loop() { | |
Blynk.run(); | |
timer.run(); | |
} | |
BLYNK_APP_DISCONNECTED() { | |
Serial.println("Blynk disconnected."); | |
} | |
void configModeCallback (WiFiManager *myWiFiManager) | |
{ | |
Serial.println("Resetting"); | |
ESP.reset(); | |
delay(1000); | |
Serial.println("WiFi: Entered config mode as: " + WiFi.softAPIP()); | |
Serial.println("Connect to Wifi hotspot: " + myWiFiManager->getConfigPortalSSID()); | |
} | |
//remote reset | |
BLYNK_WRITE(V2) { | |
Serial.println("Resetting"); | |
ESP.reset(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment