11
11
#include < ESP8266WiFi.h>
12
12
#include < ESP8266HTTPClient.h>
13
13
14
- #include < time.h>
14
+ #include < Ambient.h>
15
+ #include < DHT.h>
16
+
15
17
// Please enter your sensitive data in the Secret tab or arduino_secrets.h(SECRET_xxxx defines)
16
18
#include " arduino_secret.h"
17
19
18
- #define JST 9 *3600
19
- #define USER_DATA_ADDR 66
20
+ // Uncomment the type of sensor in use:
21
+ // #define DHTTYPE DHT11 // DHT 11
22
+ #define DHTTYPE DHT22 // DHT 22 (AM2302)
23
+ // #define DHTTYPE DHT21 // DHT 21 (AM2301)
24
+
25
+ #define DHTPIN D3
20
26
21
27
// for SPI pin definitions see e.g.:
22
28
// C:\Users\xxx\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\variants\generic\common.h
23
29
24
- GxIO_Class io (SPI, /* CS=D8*/ D8, /* DC=D3*/ D3 , /* RST=D4*/ D4 ); // arbitrary selection of D3(=0), D4(=2), selected for default of GxEPD_Class
25
- GxEPD_Class display (io, /* RST=D4*/ D4 , /* BUSY=D2*/ D2 ); // default selection of D4(=2), D2(=4)
30
+ GxIO_Class io (SPI, /* CS=D8*/ D8, /* DC=D3*/ D4 , /* RST=D4*/ D2 ); // arbitrary selection of D3(=0), D4(=2), selected for default of GxEPD_Class
31
+ GxEPD_Class display (io, /* RST=D4*/ D2 , /* BUSY=D2*/ D1 ); // default selection of D4(=2), D2(=4)
26
32
HTTPClient client;
27
33
BearSSL::WiFiClientSecure secure;
34
+ Ambient ambient;
35
+ WiFiClient ambientClient;
36
+ DHT dht (DHTPIN, DHTTYPE);
28
37
29
38
const uint32_t getWeatherInfoPeriod = 10 * 60 * 1000 ;
30
- const uint32_t getCurrentTempPeriod = 10 * 60 * 1000 ;
39
+ const uint32_t getCurrentTempPeriod = 1 * 60 * 1000 ;
31
40
32
41
struct {
33
42
uint64_t getWeatherInfoJpeg;
@@ -40,7 +49,6 @@ void setup()
40
49
Serial.begin (74880 );
41
50
Serial.println ();
42
51
Serial.println (" setup" );
43
-
44
52
display.init (74880 ); // enable diagnostic output on Serial
45
53
46
54
WiFi.begin (SECRET_SSID, SECRET_SSID_PASSWORD);
@@ -56,13 +64,8 @@ void setup()
56
64
Serial.println (WiFi.localIP ());
57
65
secure.setInsecure ();
58
66
59
- // Serial.print("Time syncing");
60
- // configTzTime("JST-9", "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
61
- // while (time(NULL) <= 100000){
62
- // delay(500);
63
- // Serial.print(".");
64
- // }
65
- // Serial.println("done");
67
+ ambient.begin (SECRET_AMBIENT_CHANNEL, SECRET_AMBIENT_WRITE, &ambientClient);
68
+ dht.begin ();
66
69
67
70
Serial.println (" Init data" );
68
71
lastExecDate.getCurrentTemp = 0 ;
@@ -73,9 +76,6 @@ void setup()
73
76
74
77
void loop ()
75
78
{
76
- // time_t now = time(NULL);
77
- // tm *tm = localtime(&now);
78
- // Serial.printf("Now: %02d/%02d %02d:%02d:%02d\n", tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
79
79
uint32_t now = millis ();
80
80
81
81
if (now < lastExecDate.getCurrentTemp || now < lastExecDate.getWeatherInfoJpeg ){
@@ -92,14 +92,10 @@ void loop()
92
92
}
93
93
if (lastExecDate.getCurrentTemp == 0 || now - lastExecDate.getCurrentTemp > getCurrentTempPeriod)
94
94
{
95
- // TODO: 現在の気温を取得する
96
95
Serial.println (" getCurrentTemp()" );
96
+ getCurrentTemp ();
97
97
lastExecDate.getCurrentTemp = now;
98
98
}
99
- // 最終実行日時を保存
100
- // ESP.rtcUserMemoryWrite(USER_DATA_ADDR, (uint32_t *)&lastExecDate, sizeof(lastExecDate));
101
-
102
- // delay(1000);
103
99
}
104
100
105
101
void getWeatherInfoJpeg (){
@@ -131,4 +127,16 @@ httpaccess:
131
127
delay (1000 * 10 );
132
128
goto httpaccess;
133
129
}
130
+ }
131
+
132
+ void getCurrentTemp ()
133
+ {
134
+ float t = dht.readTemperature ();
135
+ float h = dht.readHumidity ();
136
+
137
+ Serial.printf (" temp:%f, humi:%f\n " , t, h);
138
+
139
+ ambient.set (1 , t);
140
+ ambient.set (2 , h);
141
+ ambient.send ();
134
142
}
0 commit comments