Created
March 10, 2019 12:47
-
-
Save SmithyAT/6e1fc9de7d374e84aaa2730aa6a8efdd to your computer and use it in GitHub Desktop.
Sparkfun Thing (ESP8266) Humidor Logger with SHT31 Sensor
This file contains 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 <Arduino.h> | |
#include <Wire.h> | |
#include <ESP8266WiFi.h> | |
#include "Adafruit_SHT31.h" | |
#include <PubSubClient.h> | |
//#define BLYNK_PRINT Serial | |
#include <BlynkSimpleEsp8266.h> | |
const int sleepInterval = 15; // in Minuten | |
extern "C" { | |
#include "user_interface.h" | |
} | |
Adafruit_SHT31 sht31 = Adafruit_SHT31(); | |
WiFiClient wclient; | |
PubSubClient client(wclient); | |
void go2sleep() { | |
Serial.println("go to sleep"); | |
WiFi.disconnect( true ); | |
delay( 1 ); | |
ESP.deepSleep(sleepInterval * 60 * 1000000, WAKE_RF_DISABLED ); | |
delay(100); | |
} | |
void connect_wifi() { | |
char hostname[] = "ESP_HumiLogger"; | |
const char *WIFI_SSID = "SSID"; | |
const char *WIFI_PWD = "PWD"; | |
IPAddress staticIP(192,168,0,201); | |
IPAddress gateway(192,168,0,1); | |
IPAddress subnet(255,255,255,0); | |
IPAddress dns(192,168,0,1); | |
int retryCount = 0; | |
WiFi.forceSleepWake(); | |
delay( 1 ); | |
wifi_station_set_hostname(hostname); | |
WiFi.persistent( false ); | |
WiFi.mode( WIFI_STA ); | |
WiFi.config(staticIP, dns, gateway, subnet); | |
WiFi.begin(WIFI_SSID, WIFI_PWD); | |
Serial.printf("\nConnecting to %s\n", WIFI_SSID); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(250); | |
Serial.print("."); | |
retryCount++; | |
if (retryCount == 40) { | |
Serial.println("\nUnable to connect!"); | |
go2sleep(); | |
} | |
} | |
Serial.println(); | |
Serial.print("Connected, IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void connect_mqtt() { | |
client.setServer(IPAddress(1,2,3,4), 1883); | |
int retryCount = 0; | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
if (client.connect("HumiLogger", "USER", "PWD", "home/humiLogger/status", 1, 1, "Offline (unexpected)")) { | |
Serial.println("connected"); | |
} else { | |
Serial.println("could not connect"); | |
retryCount++; | |
delay(500); | |
if (retryCount == 3) { | |
break; | |
} | |
} | |
} | |
} | |
void connect_blynk() { | |
Serial.print("Attempting Blynk connection..."); | |
Blynk.config("AUTH", IPAddress(1,2,3,4), 8080); | |
Blynk.connect(); | |
if (Blynk.connected()) { | |
Serial.println("connected"); | |
} else { | |
Serial.println("could not connect"); | |
} | |
} | |
void setup() { | |
WiFi.mode( WIFI_OFF ); | |
WiFi.forceSleepBegin(); | |
delay( 1 ); | |
Serial.begin(115200); | |
Serial.println(); | |
if (! sht31.begin(0x44)) { | |
Serial.println("Couldn't find SHT31"); | |
go2sleep(); | |
} | |
float t = sht31.readTemperature(); | |
float h = sht31.readHumidity(); | |
if (! isnan(t)) { | |
Serial.print("Temp *C = "); Serial.println(t); | |
} else { | |
Serial.println("Failed to read temperature"); | |
} | |
if (! isnan(h)) { | |
Serial.print("Hum. % = "); Serial.println(h); | |
} else { | |
Serial.println("Failed to read humidity"); | |
} | |
yield(); | |
connect_wifi(); | |
connect_mqtt(); | |
connect_blynk(); | |
if (client.connected()) { | |
client.publish("home/humiLogger/status", "Online", true); | |
client.publish("home/humiLogger/temp", String(t).c_str(), true); | |
client.publish("home/humiLogger/humidity", String(h).c_str(), true); | |
client.publish("home/humiLogger/status", "Offline", true); | |
client.disconnect(); | |
} | |
if (Blynk.connected()) { | |
Blynk.virtualWrite(V1, t); | |
Blynk.virtualWrite(V2, h); | |
} | |
go2sleep(); | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment