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()
27 const String szCommandReadBaseURL = "https://a.yamagata-u.ac.jp/amenity/network/M2M/KinomaControlRequest.aspx";
28 const String FingurePrintSha1 ="A5 23 3B D5 FA 6C E9 C2 8F 30 17 8F EF F9 EA C8 C5 18 49 3F";
29 const char* host = "a.yamagata-u.ac.jp";
30 const int httpPort = 80;
31 const int httpsPort = 443;
33 //char toSSID[] = "xxxx";
34 //char ssidPASSWD[] = "xxxxxx";
36 int nCounterWifiReconnect=0;
40 bool bCommandReadingHttp = false;
41 bool bNoNTPupdated = true;
47 // You can specify the time server pool and the offset (in seconds, can be
48 // changed later with setTimeOffset() ). Additionaly you can specify the
49 // update interval (in milliseconds, can be changed using setUpdateInterval() ).
50 NTPClient timeClient(ntpUDP, "ntp.nict.jp", 0, 3600000);
53 if (nADcount > MAXROW-1) {
55 for(row =1; row <MAXROW; row++){
56 AD1[row-1].nBootedClock =AD1[row].nBootedClock;
57 AD1[row-1].nEphochTime=AD1[row-1].nEphochTime;
58 AD1[row-1].ADValue = AD1[row-1].ADValue;
62 AD1[nADcount].nBootedClock = nBootedClock;
63 AD1[nADcount].nEphochTime=(long)timeClient.getEpochTime();
64 AD1[nADcount].ADValue = ADvalue;
68 void timer1_ISR (void) {
69 if (WiFi.status() != WL_CONNECTED) {
70 // コネクション確立済み以外のステータスなので
72 Nefry.setLed(255, 0, 0);
73 nCounterWifiReconnect++;
75 if (nCounterWifiReconnect>30){
76 nCounterWifiReconnect=0;
77 Nefry.setWifiTimeoutClear();
78 Nefry.println("Wifi TimeoutClear");
82 nCounterWifiReconnect=0;
83 Nefry.setLed(0, 0, 64);
87 ADvalue = analogRead(A0);
89 // AD1[nADcount].nBootedClock = nBootedClock;
90 // AD1[nADcount].nEphochTime=(long)timeClient.getEpochTime();
91 // AD1[nADcount].ADValue = ADvalue;
95 //Nefry.println(ADvalue);
96 //2014-10-10T13:50:40+09:00 (TIMEZONE JST)
97 //2014-10-10T04:50:40Z (UTC)
98 // sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02dZ",year(), month(), day(),hour(),minute(),second());
99 // Nefry.println(buf);
108 timer1_detachInterrupt();
111 void start_timer1(unsigned int nIntervalTime_us) {
113 timer1_attachInterrupt(timer1_ISR);
114 timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
115 timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us);
118 // put your setup code here, to run once:
120 // WiFi.mode(WIFI_STA);
121 //WiFi.begin(toSSID, ssidPASSWD);
123 //Nefry.println(DEVICEID);
124 start_timer1(1000000); //1000000 us間隔でタイマーを起動
130 if (bNoNTPupdated==true){
131 if (WiFi.status() == WL_CONNECTED) {
132 Nefry.setLed(128, 0, 128);
135 setTime(timeClient.getEpochTime());
136 //Nefry.println(timeClient.getFormattedTime());
137 Nefry.setLed(0, 0, 64);
143 if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
144 bCommandReadingHttp = true;
145 Nefry.setLed(0, 128, 0);
147 String szCommandReadURL = szCommandReadBaseURL + "?DeviceID=" + DEVICEID;
149 szCommandReadURL = szCommandReadURL + "&AD1=" + AD1[nADcount-1].ADValue;
150 szCommandReadURL = szCommandReadURL + "&EphochTime=" + AD1[nADcount-1].nEphochTime;
151 szCommandReadURL = szCommandReadURL + "&BootedClock=" + AD1[nADcount-1].nBootedClock;
152 szCommandReadURL = szCommandReadURL + "&ADCount=" + nADcount;
153 http.begin(szCommandReadURL,FingurePrintSha1);
154 int httpCode = http.GET();
155 //Nefry.print(timeClient.getFormattedTime());
156 //Nefry.println(httpCode);
157 if(httpCode == HTTP_CODE_OK) {
158 String payload = http.getString();
159 // Nefry.println(" : " + payload);
163 bCommandReadingHttp = false;
164 Nefry.setLed(0, 0, 64);
169 if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
171 Nefry.setLed(128, 0, 128);
173 setTime(timeClient.getEpochTime());
174 // Nefry.println(timeClient.getFormattedTime());
175 //Nefry.println((String)AD1[nADcount].nBootedClock+"++");
177 Nefry.setLed(0, 0, 64);