2 #include <ESP8266WiFi.h>
6 #include <ESP8266HTTPClient.h>
9 //NTP test program for Nefry
10 //Nefryの時計をntpで同期するためのサンプルコードです.開発中です.
11 //Nefryのwifi関数を使ってWifiが切れたら再接続するように書き換えた
14 #include "user_interface.h"
17 #define DEVICEID ESP.getChipId()
20 const String szCommandReadBaseURL = "https://a.yamagata-u.ac.jp/amenity/network/M2M/KinomaControlRequest.aspx";
21 const String FingurePrintSha1 ="A5 23 3B D5 FA 6C E9 C2 8F 30 17 8F EF F9 EA C8 C5 18 49 3F";
22 const char* host = "a.yamagata-u.ac.jp";
23 const int httpPort = 80;
24 const int httpsPort = 443;
26 //char toSSID[] = "xxxx";
27 //char ssidPASSWD[] = "xxxxxx";
29 int nCounterWifiReconnect=0;
33 bool bCommandReadingHttp = false;
34 bool bNoNTPupdated = true;
37 // You can specify the time server pool and the offset (in seconds, can be
38 // changed later with setTimeOffset() ). Additionaly you can specify the
39 // update interval (in milliseconds, can be changed using setUpdateInterval() ).
40 NTPClient timeClient(ntpUDP, "ntp.nict.jp", 0, 3600000);
43 void timer1_ISR (void) {
44 if (WiFi.status() != WL_CONNECTED) {
45 // コネクション確立済み以外のステータスなので
47 Nefry.setLed(255, 0, 0);
48 nCounterWifiReconnect++;
50 if (nCounterWifiReconnect>30){
51 nCounterWifiReconnect=0;
52 Nefry.setWifiTimeoutClear();
53 Nefry.println("Wifi TimeoutClear");
57 nCounterWifiReconnect=0;
58 Nefry.setLed(0, 0, 64);
61 ADvalue = analogRead(A0);
63 //Nefry.println(ADvalue);
64 //2014-10-10T13:50:40+09:00 (TIMEZONE JST)
65 //2014-10-10T04:50:40Z (UTC)
66 // sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02dZ",year(), month(), day(),hour(),minute(),second());
67 // Nefry.println(buf);
75 timer1_detachInterrupt();
78 void start_timer1(unsigned int nIntervalTime_us) {
80 timer1_attachInterrupt(timer1_ISR);
81 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
82 timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us);
85 // put your setup code here, to run once:
87 // WiFi.mode(WIFI_STA);
88 //WiFi.begin(toSSID, ssidPASSWD);
90 //Nefry.println(DEVICEID);
91 start_timer1(1000000); //1000000 us間隔でタイマーを起動
97 if (bNoNTPupdated==true){
98 if (WiFi.status() == WL_CONNECTED) {
99 Nefry.setLed(128, 0, 128);
102 setTime(timeClient.getEpochTime());
103 // Nefry.println(timeClient.getFormattedTime());
104 Nefry.setLed(0, 0, 64);
110 if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
111 bCommandReadingHttp = true;
112 Nefry.setLed(0, 128, 0);
114 String szCommandReadURL = szCommandReadBaseURL + "?DeviceID=" + DEVICEID;
115 http.begin(szCommandReadURL,FingurePrintSha1);
116 int httpCode = http.GET();
117 //Nefry.print(timeClient.getFormattedTime());
118 //Nefry.println(httpCode);
119 if(httpCode == HTTP_CODE_OK) {
120 String payload = http.getString();
121 // Nefry.println(" : " + payload);
122 bCommandReadingHttp = false;
125 Nefry.setLed(0, 0, 64);
131 if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
133 Nefry.setLed(128, 0, 128);
135 setTime(timeClient.getEpochTime());
136 // Nefry.println(timeClient.getFormattedTime());
138 Nefry.setLed(0, 0, 64);