OSDN Git Service

34f6adb34f60ba793a403e69975fc0f25b5c3805
[ito-manager/ITO-manager5.0.git] / trial / HTTP-MEMORY-test / HTTP-MEMORY-test.ino
1 #include <Nefry.h>
2 #include <ESP8266WiFi.h>
3 #include <NTPClient.h>
4 #include <WiFiUdp.h>
5 #include <TimeLib.h>
6 #include <ESP8266HTTPClient.h>
7
8
9 //NTP test program for Nefry
10 //Nefryの時計をntpで同期するためのサンプルコードです.開発中です.
11 //Nefryのwifi関数を使ってWifiが切れたら再接続するように書き換えた
12
13 extern "C" {
14   #include "user_interface.h"
15 }
16
17 #define DEVICEID ESP.getChipId()
18
19 struct ADData
20 {
21     int  nBootedClock;
22     long  nEphochTime;
23     int  ADValue;
24 };
25
26 const String szCommandReadBaseURL = "https://a.yamagata-u.ac.jp/amenity/network/M2M/KinomaControlRequest.aspx";
27 const String FingurePrintSha1 ="A5 23 3B D5 FA 6C E9 C2 8F 30 17 8F EF F9 EA C8 C5 18 49 3F";
28 const char* host = "a.yamagata-u.ac.jp";
29 const int httpPort = 80;
30 const int httpsPort = 443;
31
32 //char toSSID[] = "xxxx";
33 //char ssidPASSWD[] = "xxxxxx";
34 bool BlueLED=false;
35 int nCounterWifiReconnect=0;
36 int nCounter1s = 0;
37 int nCounter30s = 0;
38 int ADvalue = 0;
39 bool bCommandReadingHttp = false; 
40 bool bNoNTPupdated = true;
41 ADData AD1[128];
42 int nADcount=0;
43 long nBootedClock=0;
44
45 WiFiUDP ntpUDP;
46 // You can specify the time server pool and the offset (in seconds, can be
47 // changed later with setTimeOffset() ). Additionaly you can specify the
48 // update interval (in milliseconds, can be changed using setUpdateInterval() ).
49 NTPClient timeClient(ntpUDP, "ntp.nict.jp", 0, 3600000);
50
51
52 void timer1_ISR (void) {
53  if (WiFi.status() != WL_CONNECTED) {
54     // コネクション確立済み以外のステータスなので
55     // 無線LAN接続NG 赤色LED点灯
56     Nefry.setLed(255, 0, 0);
57     nCounterWifiReconnect++;
58     //WiFi.begin();
59       if (nCounterWifiReconnect>30){
60         nCounterWifiReconnect=0;
61         Nefry.setWifiTimeoutClear();
62         Nefry.println("Wifi TimeoutClear");
63       }
64   } else {
65     //コネクションOKなので、青色LED点灯
66     nCounterWifiReconnect=0;
67     Nefry.setLed(0, 0, 64);
68   
69   }
70
71     ADvalue =  analogRead(A0);
72     AD1[nADcount].nBootedClock = nBootedClock; 
73     AD1[nADcount].nEphochTime=(long)timeClient.getEpochTime();
74     AD1[nADcount].ADValue = ADvalue;
75        
76
77   char buf[128];
78   //Nefry.println(ADvalue);
79   //2014-10-10T13:50:40+09:00 (TIMEZONE JST)
80   //2014-10-10T04:50:40Z (UTC)
81  // sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02dZ",year(), month(), day(),hour(),minute(),second());
82 //  Nefry.println(buf);
83   nCounter1s++; 
84   nBootedClock++; 
85   nCounter30s++;
86
87 }
88
89 void stop_timer1() {
90   timer1_disable();
91   timer1_detachInterrupt();
92 }
93
94 void start_timer1(unsigned int nIntervalTime_us) {
95     timer1_isr_init();
96     timer1_attachInterrupt(timer1_ISR);
97     timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
98     timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us); 
99 }
100 void setup() {
101   // put your setup code here, to run once:
102   //NefryではWifeへの接続は不要
103  // WiFi.mode(WIFI_STA);
104    //WiFi.begin(toSSID, ssidPASSWD);
105    timeClient.begin();
106    //Nefry.println(DEVICEID);
107    start_timer1(1000000); //1000000 us間隔でタイマーを起動
108
109 }
110
111 void loop() {
112   
113  if (bNoNTPupdated==true){
114    if (WiFi.status() == WL_CONNECTED) {
115     Nefry.setLed(128, 0, 128);
116     timeClient.update();
117     bNoNTPupdated=false;
118     setTime(timeClient.getEpochTime());
119     //Nefry.println(timeClient.getFormattedTime());
120     Nefry.setLed(0, 0, 64);
121    }
122   }
123  
124  if (nCounter1s >0) {
125
126     if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
127     bCommandReadingHttp  = true;
128     Nefry.setLed(0, 128, 0);
129     HTTPClient http;
130     String szCommandReadURL = szCommandReadBaseURL + "?DeviceID=" + DEVICEID;
131     //以下3行は,デバッグ用に追加
132     szCommandReadURL = szCommandReadURL + "&AD1=" + AD1[nADcount].ADValue;
133     szCommandReadURL = szCommandReadURL + "&EphochTime=" + AD1[nADcount].nEphochTime;
134     szCommandReadURL = szCommandReadURL + "&BootedClock=" +  AD1[nADcount].nBootedClock;
135     http.begin(szCommandReadURL,FingurePrintSha1);
136     int httpCode = http.GET();
137     //Nefry.print(timeClient.getFormattedTime());
138     //Nefry.println(httpCode);
139     if(httpCode == HTTP_CODE_OK) {
140           String payload = http.getString();
141         // Nefry.println(" : " + payload);
142     }
143     http.end();
144     nCounter1s =0;
145     bCommandReadingHttp = false;
146     Nefry.setLed(0, 0, 64);
147     }
148   }
149
150   
151  if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
152   nCounter30s =0;
153   Nefry.setLed(128, 0, 128);
154   timeClient.update();
155   setTime(timeClient.getEpochTime());
156   // Nefry.println(timeClient.getFormattedTime());
157   //Nefry.println((String)AD1[nADcount].nBootedClock+"++");
158   //Nefry.ndelay(100);
159   Nefry.setLed(0, 0, 64);
160  }
161 }