Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
ESP32+ OLED 0.96 (SSD1306) +dht11
Author : Kouji Nakashima / kuc-arc-f.com
date : 2017/03/08
SDA: #21, SCL: #22 connect.
thanks:
https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_i2c_Si7021_oled
u8g2 LIB: https://github.com/olikraus/u8g2
*/
#include <U8g2lib.h>
#include <Wire.h>
//U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // Adafruit ESP8266/32u4/ARM Boards + FeatherWing OLED
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
/* Common addresses definition for temperature sensor. */
#include <DHT.h>
#define DHTPIN 14 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
static int pos;
//
void setup() {
Serial.begin(115200);
dht.begin();
pos=0;
u8g2.begin();
}
//
void proc_display(float humi, float temp){
// static float humi, temp;
String stringOut;
char buf[512];
char degree = 0xB0;
if (pos%4==0) { // Heardbeat
u8g2.drawDisc(122,28,3);
u8g2.sendBuffer(); // transfer internal memory to the display
}
// humi= 38.5;
// temp= 17.6;
stringOut = "";
stringOut += dtostrf(humi, 3, 1, buf);
stringOut += "% ";
stringOut += dtostrf(temp, 3, 1, buf);
stringOut += degree;
stringOut += "C";
stringOut.getBytes((unsigned char *)buf, 512, 0);
Serial.print(pos++);
Serial.print(" ");
Serial.println(buf);
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_10x20_tf);
u8g2.drawStr(0,20,buf); // write to the internal memory
u8g2.drawBox(0,26,humi,6);
u8g2.drawFrame(0,26,100,6);
u8g2.sendBuffer(); // transfer internal memory to the display
}
uint32_t mTimerTemp=0;
//
void loop() {
if( millis() > mTimerTemp ){
mTimerTemp= millis() + 1000;
float fHum = dht.readHumidity();
float fTmp = dht.readTemperature();
if (isnan(fHum ) || isnan(fTmp ) ) {
Serial.println("Failed to read from DHT sensor!");
return;
}
proc_display(fHum, fTmp );
}
//delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment