0.96インチ, 128×64 OLED(有機EL) を入手できたので、
BME280センサと組合せて、esp8266経由で
クラウド連携試してみました。
表示内容: 2パターン
1)
1行目:気温
2行目:湿度(%)
2)
気圧(hPa)
通信: I2C のみで制御
*) OLEDは、eBayさんから入手しました。
# 動画 : センサー値はクラウド側へ送信
# クラウド側、ログ

# 表示側の基板
I2Cラインを引きまわして、esp8266からは
4本の配線。
*) BME配線は、LCD版とほぼ同じです。
# 配線

# ドライバ
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
/* | |
OLED 0.96 inch+ BME280 | |
with esp8266 | |
OLED: | |
https://github.com/squix78/esp8266-oled-ssd1306 | |
*/ | |
#include <Wire.h> | |
#include <BME280_MOD-1022.h> | |
#include "SSD1306.h" | |
#include <ESP8266WiFi.h> | |
//#include <WiFiClient.h> | |
// | |
SSD1306 display(0x3c, 4, 5); | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* mHost = "api.thingspeak.com"; | |
const char* mHostTime = ""; | |
String mAPI_KEY="your-KEY"; | |
String mTimeStr="0000"; | |
float mTemp=0; | |
float mHumidity=0; | |
float mPressure=0; | |
uint32_t mNextHttp= 30000; | |
uint32_t mTimerTmpInit=30000; | |
uint32_t mTimerDisp; | |
uint32_t mTimerTime; | |
uint32_t mTimerTemp; | |
// Arduino needs this to pring pretty numbers | |
void printFormattedFloat(float x, uint8_t precision) { | |
char buffer[10]; | |
dtostrf(x, 7, precision, buffer); | |
Serial.print(buffer); | |
} | |
// print out the measurements | |
void printCompensatedMeasurements(void) { | |
//float temp, humidity, pressure, pressureMoreAccurate; | |
float pressureMoreAccurate; | |
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate; | |
char buffer[80]; | |
mTemp = BME280.getTemperature(); | |
mHumidity = BME280.getHumidity(); | |
mPressure = BME280.getPressure(); | |
pressureMoreAccurate = BME280.getPressureMoreAccurate(); // t_fine already calculated from getTemperaure() above | |
tempMostAccurate = BME280.getTemperatureMostAccurate(); | |
humidityMostAccurate = BME280.getHumidityMostAccurate(); | |
pressureMostAccurate = BME280.getPressureMostAccurate(); | |
} | |
// | |
void init_BME280(){ | |
uint8_t chipID; | |
//Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!"); | |
//Serial.println("Embedded Adventures (www.embeddedadventures.com)"); | |
chipID = BME280.readChipId(); | |
// find the chip ID out just for fun | |
Serial.print("ChipID = 0x"); | |
Serial.print(chipID, HEX); | |
// need to read the NVM compensation parameters | |
BME280.readCompensationParams(); | |
// Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything | |
BME280.writeOversamplingPressure(os1x); // 1x over sampling (ie, just one sample) | |
BME280.writeOversamplingTemperature(os1x); | |
BME280.writeOversamplingHumidity(os1x); | |
// example of a forced sample. After taking the measurement the chip goes back to sleep | |
BME280.writeMode(smForced); | |
while (BME280.isMeasuring()) { | |
Serial.println("Measuring..."); | |
delay(50); | |
} | |
Serial.println("Done!"); | |
// read out the data - must do this before calling the getxxxxx routines | |
BME280.readMeasurements(); | |
// Example for "indoor navigation" | |
// We'll switch into normal mode for regular automatic samples | |
BME280.writeStandbyTime(tsb_0p5ms); // tsb = 0.5ms | |
BME280.writeFilterCoefficient(fc_16); // IIR Filter coefficient 16 | |
BME280.writeOversamplingPressure(os16x); // pressure x16 | |
BME280.writeOversamplingTemperature(os2x); // temperature x2 | |
BME280.writeOversamplingHumidity(os1x); // humidity x1 | |
BME280.writeMode(smNormal); | |
} | |
// | |
void proc_http(String sTemp, String sHum, String sPre){ | |
//Serial.print("connecting to "); | |
//Serial.println(mHost); | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(mHost, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
String url = "/update?key="+ mAPI_KEY + "&field1="+ sTemp +"&field2=" + sHum+ "&field3="+sPre; | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + mHost + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
int iSt=0; | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
//Serial.print(line); | |
} | |
} | |
// | |
void setup() { | |
Serial.begin(9600); | |
mTimerTemp= mTimerTmpInit; | |
Wire.begin(); | |
Serial.println("#setup-Start"); | |
init_BME280(); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
//OLED | |
display.init(); | |
display.flipScreenVertically(); | |
display.displayOn(); | |
display.clear(); | |
} | |
// | |
void display_OLED(String sTmp, String sHum, String sPre){ | |
sTmp="Temp:"+ sTmp+ " C"; | |
sHum="Hum :"+ sHum+ " %"; | |
sPre=sPre+ " hPa"; | |
display.setFont(ArialMT_Plain_24); | |
display.setTextAlignment(TEXT_ALIGN_LEFT); | |
display.drawString( 0, 0, sTmp ); | |
display.drawString( 0, 32, sHum ); | |
display.display(); | |
delay(2000); | |
display.clear(); | |
//2 | |
display.drawString( 0, 0, "Pressure:"); | |
display.drawString( 0, 32, sPre ); | |
display.display(); | |
delay(2000); | |
display.clear(); | |
} | |
// | |
void loop() { | |
delay(100); | |
while (BME280.isMeasuring()) { | |
} | |
String sTmp=""; | |
String sHum=""; | |
String sPre=""; | |
if (millis() > mTimerDisp) { | |
mTimerDisp = millis()+ 3000; | |
// read out the data - must do this before calling the getxxxxx routines | |
BME280.readMeasurements(); | |
printCompensatedMeasurements(); | |
int itemp =(int)mTemp; | |
int iHum = (int)mHumidity; | |
int iPress = (int)mPressure; | |
Serial.print("dat="); | |
char cTemp[4+1]; | |
char cHum[4+1]; | |
char cPres[4+1]; | |
sprintf(cTemp, "%04d", itemp); | |
sprintf(cHum, "%04d", iHum); | |
sprintf(cPres, "%04d", iPress); | |
Serial.print(cTemp); | |
Serial.print(cHum); | |
Serial.println(cPres); | |
sTmp= String(itemp); | |
sHum= String( iHum); | |
sPre= String( iPress); | |
display_OLED(sTmp, sHum, sPre); | |
//Serial.println( millis() ); | |
} // if_timeOver | |
if (millis() > mTimerTemp) { | |
mTimerTemp = millis()+ mNextHttp+ mTimerTmpInit; | |
int itemp =(int)mTemp; | |
int iHum = (int)mHumidity; | |
int iPress = (int)mPressure; | |
sTmp=String(itemp); | |
sHum=String(iHum); | |
sPre=String(iPress); | |
proc_http(sTmp, sHum, sPre); | |
delay(100 ); | |
} | |
} | |
# 謝辞と、お礼
今回も多くのブログ記事、ライブラリ等参考にさせて頂きました。
感謝申し上げます。
https://github.com/squix78/esp8266-oled-ssd1306
# 関連の記事
LCDで 気温/湿度/気圧/時刻の表示。AQM0802+BME280
http://knaka0209.blogspot.jp/2016/06/LCD-BME280-1.html
# 関連まとめ [IoT な電子工作まとめ]
http://knaka0209.blogspot.jp/2015/11/iot-matome.html