Skip to content

Instantly share code, notes, and snippets.

@zeromancer1972
Created March 7, 2021 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeromancer1972/674e3434105fd9bbf447f2be86e0771c to your computer and use it in GitHub Desktop.
Save zeromancer1972/674e3434105fd9bbf447f2be86e0771c to your computer and use it in GitHub Desktop.
This gist drives a SSD1306 OLED with values coming from a ESP8266 (Wemos D1 Mini) given by a DHT22 temp/humidity sensor.
/**************************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/category/63_98
This example is for a 128x32 pixel display using I2C to communicate
3 pins are required to interface (two I2C and one reset).
Adafruit invests time and resources providing this open
source code, please support Adafruit and open-source
hardware by purchasing products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries,
with contributions from the open source community.
BSD license, check license.txt for more information
All text above, and the splash screen below must be
included in any redistribution.
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMono9pt7b.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Arduino_JSON.h>
#include <DHT.h>
#define DHTPIN 0 // Arduino!
uint8_t DHTPin = D3;
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float humidity, temperature, dewpoint;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const char* SSID = "yourSSID";
const char* PSK = "yourWifiPassword";
const char* MQTT_BROKER = "yourMQTTHostIP";
const char* CLIENT_ID = "yourMQTTClientID";
const char* MQTT_USER = "yourMQTTHostUser";
const char* MQTT_PASSWORD = "yourMQTTHostPassword";
JSONVar myObject;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50], hum[50], temp[50], dew[50];
int value = 0;
IPAddress ipAddress;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
// clear the screen
clearDisplay();
// start Wifi
setup_wifi();
// start MQT
client.setServer(MQTT_BROKER, 1883);
// start DHT
setupDHT();
}
// Clear the display at startup
void clearDisplay() {
// Clear the buffer
Serial.println(F("Clear display..."));
display.clearDisplay();
display.display();
}
// Start Wifi for MQTT
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
ipAddress = WiFi.localIP();
Serial.println(WiFi.localIP());
}
void setupDHT() {
pinMode(DHTPin, INPUT);
dht.begin();
}
void printIp() {
display.setFont(null);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.print("IP: ");
display.println(ipAddress);
display.drawLine(0, 10, 132, 10, SSD1306_WHITE);
}
void readTempDHT() {
humidity = dht.readHumidity();
temperature = dht.readTemperature();
// calibrate
temperature--;
dewpoint = calcDewpoint(humidity, temperature);
Serial.println(temperature);
Serial.println(humidity);
Serial.println(dewpoint);
snprintf (hum, 50, "LF %d.%d %%", (int)humidity, (int)(humidity * 10) % 10);
snprintf (temp, 50, "TEMP %d.%d C", (int)temperature, (int)(temperature * 10) % 10);
snprintf (dew, 50, "TP %d.%d C", (int)dewpoint, (int)(dewpoint * 10) % 10);
displayTempHum(temp, hum, dew);
}
float calcDewpoint(float humi, float temp) {
float k;
k = log(humi / 100) + (17.62 * temp) / (243.12 + temp);
return 243.12 * k / (17.62 - k);
}
void displayTempHum(char* t, char* h, char* d) {
Serial.println("------------- DISPLAY");
Serial.println(h);
Serial.println(t);
Serial.println(d);
display.setFont(&FreeMono9pt7b);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 26);
display.println(t);
display.println(h);
display.println(d);
}
void connectMQT() {
while (!client.connected()) {
Serial.print("Reconnecting...");
if (!client.connect(CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
}
char* string2char(String command) {
if (command.length() != 0) {
char *p = const_cast<char*>(command.c_str());
return p;
}
}
void loop() {
display.clearDisplay();
printIp();
readTempDHT();
display.display();
if (!client.connected()) {
connectMQT();
}
client.loop();
myObject["uptime"] = millis();
myObject["type"] = "D1 mini/DHT22/SSD1306";
myObject["hum"] = humidity;
myObject["temp"] = temperature;
myObject["dew"] = dewpoint;
char buf[100] = "";
strcat(buf, "tele/");
strcat(buf, CLIENT_ID);
strcat(buf, "/SENSOR");
Serial.println(String(buf));
Serial.println(JSON.stringify(myObject));
client.publish(string2char(String(buf)), string2char(JSON.stringify(myObject)));
delay(10000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment