以前 ESP-WROOM-02 で使った温度センサ HDC1000 は秋月からはディスコンの様なので、新タイプのSHT31を入手して使ってみる事にします。
秋月のページはこちら SHT31
秋月のモジュールは HDC1000と同じくRaspberryPiのピン配置に合わせて配線してありますので、そのまま挿す事ができます。
Arduino用のサンプルコードが用意されております。 Arduino Sample ライブラリ(zip)
このライブラリのサンプルスケッチへ、
#include <ESP8266WiFi.h>
を追加しただけで何の変更もなく動作しました。
前回のスケッチへ少し変更を加え、 wifiでntpから時刻を取得し、スイッチサイエンスのmini LCD で温度・湿度を表示させる様にしてみました。
/*
* WROOM-02 TEST
*
* WiFi
* NTP Sync
* mini LCD
* SHT31 module
*
*/
#include <AE_SHT31.h>
#include <time.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
//Access Point Setting
const char *ssid = "********";
const char *pass = "********";
#define LED_PIN 13
#define lcdaddr 0x3e // LCD Address
byte contrast = 33; // コントラスト(0~63)
AE_SHT31 SHT31 = AE_SHT31(0x45);
#define JST 3600*9
IPAddress myIP;
void setup() {
int t=0;
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Wire.begin();
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00111001); // function set
lcd_cmd(0b00000100); // EntryModeSet
lcd_cmd(0b00010100); // interval osc
lcd_cmd(0b01110000 | (contrast & 0xF)); // contrast Low
lcd_cmd(0b01011100 | ((contrast >> 4) & 0x3)); // contast High/icon/power
lcd_cmd(0b01101100); // follower control
delay(200);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00001100); // Display On
lcd_cmd(0b00000001); // Clear Display
delay(2);
// WIFI
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(t++>20){
Serial.println("ERROR Wifi CONNECT");
break;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
myIP=WiFi.localIP();
Serial.println(myIP);
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
// SH31
SHT31.SoftReset();
// 内蔵ヒーター 0:OFF 1:ON
SHT31.Heater(0);
}
/*
* Get or Set Info
*/
void getinfo()
{
Serial.print("ESSID :");
Serial.println(ssid);
Serial.print("IP address: ");
myIP=WiFi.localIP();
Serial.println(myIP);
}
void setinfo()
{
}
int count=0;
void loop() {
char str1[10];
char str2[10];
char stemp[10];
char shumid[10];
time_t t;
struct tm *tm;
int c;
if(Serial.available()){
c = Serial.read();
switch(c){
case 'g':getinfo();
break;
case 's':setinfo();
}
}
else if (count++==200){
digitalWrite(LED_PIN,digitalRead(LED_PIN)^1);
t= time(NULL);
tm = localtime(&t);
sprintf(str1,"%02d/%02d ", tm->tm_mon+1, tm->tm_mday);
sprintf(str2,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
lcd_setCursor(0, 0);
lcd_printStr(str1);
lcd_setCursor(0, 1);
lcd_printStr(str2);
} else if(count==400){
digitalWrite(LED_PIN,digitalRead(LED_PIN)^1);
SHT31.GetTempHum();
dtostrf(SHT31.Temperature(),3,1,stemp);
dtostrf(SHT31.Humidity(),3,1,shumid);
sprintf(str1,"%s 'C ",stemp);
sprintf(str2,"%s %% ",shumid);
lcd_setCursor(0, 0);
lcd_printStr(str1);
lcd_setCursor(0, 1);
lcd_printStr(str2);
count=0;
}
delay(10);
}
void lcd_cmd(byte x) {
Wire.beginTransmission(lcdaddr);
Wire.write(0b00000000); // CO = 0,RS = 0
Wire.write(x);
Wire.endTransmission();
}
void lcd_contdata(byte x) {
Wire.write(0b11000000); // CO = 1, RS = 1
Wire.write(x);
}
void lcd_lastdata(byte x) {
Wire.write(0b01000000); // CO = 0, RS = 1
Wire.write(x);
}
// 文字の表示
void lcd_printStr(const char *s) {
Wire.beginTransmission(lcdaddr);
while (*s) {
if (*(s + 1)) {
lcd_contdata(*s);
} else {
lcd_lastdata(*s);
}
s++;
}
Wire.endTransmission();
}
// 表示位置の指定
void lcd_setCursor(byte x, byte y) {
lcd_cmd(0x80 | (y * 0x40 + x));
}
==========
* WROOM-02 TEST
*
* WiFi
* NTP Sync
* mini LCD
* SHT31 module
*
*/
#include <AE_SHT31.h>
#include <time.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
//Access Point Setting
const char *ssid = "********";
const char *pass = "********";
#define LED_PIN 13
#define lcdaddr 0x3e // LCD Address
byte contrast = 33; // コントラスト(0~63)
AE_SHT31 SHT31 = AE_SHT31(0x45);
#define JST 3600*9
IPAddress myIP;
void setup() {
int t=0;
pinMode(LED_PIN, OUTPUT);
Serial.begin(9600);
Wire.begin();
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00111001); // function set
lcd_cmd(0b00000100); // EntryModeSet
lcd_cmd(0b00010100); // interval osc
lcd_cmd(0b01110000 | (contrast & 0xF)); // contrast Low
lcd_cmd(0b01011100 | ((contrast >> 4) & 0x3)); // contast High/icon/power
lcd_cmd(0b01101100); // follower control
delay(200);
lcd_cmd(0b00111000); // function set
lcd_cmd(0b00001100); // Display On
lcd_cmd(0b00000001); // Clear Display
delay(2);
// WIFI
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(t++>20){
Serial.println("ERROR Wifi CONNECT");
break;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
myIP=WiFi.localIP();
Serial.println(myIP);
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
// SH31
SHT31.SoftReset();
// 内蔵ヒーター 0:OFF 1:ON
SHT31.Heater(0);
}
/*
* Get or Set Info
*/
void getinfo()
{
Serial.print("ESSID :");
Serial.println(ssid);
Serial.print("IP address: ");
myIP=WiFi.localIP();
Serial.println(myIP);
}
void setinfo()
{
}
int count=0;
void loop() {
char str1[10];
char str2[10];
char stemp[10];
char shumid[10];
time_t t;
struct tm *tm;
int c;
if(Serial.available()){
c = Serial.read();
switch(c){
case 'g':getinfo();
break;
case 's':setinfo();
}
}
else if (count++==200){
digitalWrite(LED_PIN,digitalRead(LED_PIN)^1);
t= time(NULL);
tm = localtime(&t);
sprintf(str1,"%02d/%02d ", tm->tm_mon+1, tm->tm_mday);
sprintf(str2,"%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
lcd_setCursor(0, 0);
lcd_printStr(str1);
lcd_setCursor(0, 1);
lcd_printStr(str2);
} else if(count==400){
digitalWrite(LED_PIN,digitalRead(LED_PIN)^1);
SHT31.GetTempHum();
dtostrf(SHT31.Temperature(),3,1,stemp);
dtostrf(SHT31.Humidity(),3,1,shumid);
sprintf(str1,"%s 'C ",stemp);
sprintf(str2,"%s %% ",shumid);
lcd_setCursor(0, 0);
lcd_printStr(str1);
lcd_setCursor(0, 1);
lcd_printStr(str2);
count=0;
}
delay(10);
}
void lcd_cmd(byte x) {
Wire.beginTransmission(lcdaddr);
Wire.write(0b00000000); // CO = 0,RS = 0
Wire.write(x);
Wire.endTransmission();
}
void lcd_contdata(byte x) {
Wire.write(0b11000000); // CO = 1, RS = 1
Wire.write(x);
}
void lcd_lastdata(byte x) {
Wire.write(0b01000000); // CO = 0, RS = 1
Wire.write(x);
}
// 文字の表示
void lcd_printStr(const char *s) {
Wire.beginTransmission(lcdaddr);
while (*s) {
if (*(s + 1)) {
lcd_contdata(*s);
} else {
lcd_lastdata(*s);
}
s++;
}
Wire.endTransmission();
}
// 表示位置の指定
void lcd_setCursor(byte x, byte y) {
lcd_cmd(0x80 | (y * 0x40 + x));
}
==========