Last active
September 2, 2020 17:51
-
-
Save hyukishi/8af40ba9358d0a8dc615704529654858 to your computer and use it in GitHub Desktop.
D1 Mini Lite v1 temp/humidity sensor with OLED 0.96" screen and MQTT output
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
/* | |
* This code is hacked together from 2 separate temp/humidity sensor projects for MQTT and OLED output | |
* Pinout is below this line | |
* OLED GND=GND VCC=VCC SCL=D1 SCA=D2 | |
* DHT22 -=GND OUT=D4 +=VCC | |
* D1 Mini Lite GND=GND VCC=3v3 | |
*/ | |
#include <SPI.h> | |
#include <Wire.h> | |
#include <ESP8266WiFi.h> | |
#include <PubSubClient.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include "DHT.h" | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); | |
#define DHTPIN 2 | |
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 | |
DHT dht(DHTPIN, DHTTYPE); | |
const char* ssid = "YOUR_SSID"; | |
const char* password = "YOUR_SSID_PASSWORD!"; | |
const char* mqtt_server = "YOUR_MQTT_SERVER_IP"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
char inChar; | |
String inString; | |
long now = millis(); | |
long lastMeasure = 0; | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("WiFi connected - ESP IP address: "); | |
Serial.print(WiFi.localIP()); | |
} | |
void callback(String topic, byte* message, unsigned int length) { | |
Serial.print("Message arrived on topic: "); | |
Serial.print(topic); | |
Serial.print(". Message: "); | |
String messageTemp; | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)message[i]); | |
messageTemp += (char)message[i]; | |
} | |
Serial.println(); | |
} | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("YOUR_DEVICE_NAME")) { | |
Serial.println("connected"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() { | |
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;); // Don't proceed, loop forever | |
} | |
display.clearDisplay(); | |
dht.begin(); | |
Serial.begin(115200); | |
setup_wifi(); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
} | |
void displayOled() { | |
// Read humidity | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Compute heat index in Fahrenheit (the default) | |
float hif = dht.computeHeatIndex(f, h); | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, true); | |
display.clearDisplay(); | |
display.setTextColor(WHITE); | |
display.setTextSize(1); | |
display.setCursor(5, 20); | |
display.print("Humidity:"); | |
display.setCursor(80, 20); | |
display.print(h); | |
display.print("%"); | |
display.setCursor(5, 35); | |
display.print("Temperature:"); | |
display.setCursor(80, 35); | |
display.print(f); | |
display.print((char)247); // degree symbol | |
display.print("F"); | |
display.setCursor(5, 50); | |
display.print("Heat Index:"); | |
display.setCursor(80, 50); | |
display.print(hif); | |
display.print((char)247); // degree symbol | |
display.print("F"); | |
display.display(); | |
} | |
void loop() { | |
if (!client.connected()) { | |
reconnect(); | |
} | |
if(!client.loop()) | |
client.connect("Suite2"); | |
now = millis(); | |
// Publishes new temperature and humidity every 30 seconds | |
if (now - lastMeasure > 5000) { | |
lastMeasure = now; | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Read temperature as Fahrenheit (isFahrenheit = true) | |
float f = dht.readTemperature(true); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
float hif = dht.computeHeatIndex(f, h); | |
static char heatIndex[7]; | |
dtostrf(hif, 6, 2, heatIndex); | |
static char temperatureTemp[7]; | |
dtostrf(f, 6, 2, temperatureTemp); | |
static char humidityTemp[7]; | |
dtostrf(h, 6, 2, humidityTemp); | |
client.publish("suite2room/humidity",humidityTemp); | |
client.publish("suite2room/temperature",temperatureTemp); | |
client.publish("suite2room/heatIndex",heatIndex); | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
Serial.println(F("Failed to read from DHT sensor!")); | |
return; | |
} | |
// Compute heat index in Celsius (isFahreheit = false) | |
// float hic = dht.computeHeatIndex(t, h, true); | |
displayOled(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment