OSDN Git Service

メモリにデータを記録するコードを追加
[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 #define MAXROW 128
19
20 struct ADData
21 {
22     long  nBootedClock;
23     long  nEphochTime;
24     int  ADValue;
25 };
26
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;
32
33 //char toSSID[] = "xxxx";
34 //char ssidPASSWD[] = "xxxxxx";
35 bool BlueLED=false;
36 int nCounterWifiReconnect=0;
37 int nCounter1s = 0;
38 int nCounter30s = 0;
39 int ADvalue = 0;
40 bool bCommandReadingHttp = false; 
41 bool bNoNTPupdated = true;
42 ADData AD1[MAXROW];
43 int nADcount=0;
44 long nBootedClock=0;
45
46 WiFiUDP ntpUDP;
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);
51
52 void ADRowAdd(){
53   if (nADcount > MAXROW-1) {
54     int row;
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;
59     }
60     nADcount=127;
61   }
62     AD1[nADcount].nBootedClock = nBootedClock; 
63     AD1[nADcount].nEphochTime=(long)timeClient.getEpochTime();
64     AD1[nADcount].ADValue = ADvalue;
65     nADcount++;
66 }
67
68 void timer1_ISR (void) {
69  if (WiFi.status() != WL_CONNECTED) {
70     // コネクション確立済み以外のステータスなので
71     // 無線LAN接続NG 赤色LED点灯
72     Nefry.setLed(255, 0, 0);
73     nCounterWifiReconnect++;
74     //WiFi.begin();
75       if (nCounterWifiReconnect>30){
76         nCounterWifiReconnect=0;
77         Nefry.setWifiTimeoutClear();
78         Nefry.println("Wifi TimeoutClear");
79       }
80   } else {
81     //コネクションOKなので、青色LED点灯
82     nCounterWifiReconnect=0;
83     Nefry.setLed(0, 0, 64);
84   
85   }
86
87     ADvalue =  analogRead(A0);
88     ADRowAdd();
89    // AD1[nADcount].nBootedClock = nBootedClock; 
90    // AD1[nADcount].nEphochTime=(long)timeClient.getEpochTime();
91    // AD1[nADcount].ADValue = ADvalue;
92        
93
94   char buf[128];
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);
100   nCounter1s++; 
101   nBootedClock++; 
102   nCounter30s++;
103
104 }
105
106 void stop_timer1() {
107   timer1_disable();
108   timer1_detachInterrupt();
109 }
110
111 void start_timer1(unsigned int nIntervalTime_us) {
112     timer1_isr_init();
113     timer1_attachInterrupt(timer1_ISR);
114     timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
115     timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us); 
116 }
117 void setup() {
118   // put your setup code here, to run once:
119   //NefryではWifeへの接続は不要
120  // WiFi.mode(WIFI_STA);
121    //WiFi.begin(toSSID, ssidPASSWD);
122    timeClient.begin();
123    //Nefry.println(DEVICEID);
124    start_timer1(1000000); //1000000 us間隔でタイマーを起動
125
126 }
127
128 void loop() {
129   
130  if (bNoNTPupdated==true){
131    if (WiFi.status() == WL_CONNECTED) {
132     Nefry.setLed(128, 0, 128);
133     timeClient.update();
134     bNoNTPupdated=false;
135     setTime(timeClient.getEpochTime());
136     //Nefry.println(timeClient.getFormattedTime());
137     Nefry.setLed(0, 0, 64);
138    }
139   }
140  
141  if (nCounter1s >0) {
142
143     if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
144     bCommandReadingHttp  = true;
145     Nefry.setLed(0, 128, 0);
146     HTTPClient http;
147     String szCommandReadURL = szCommandReadBaseURL + "?DeviceID=" + DEVICEID;
148     //以下3行は,デバッグ用に追加
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);
160     }
161     http.end();
162     nCounter1s =0;
163     bCommandReadingHttp = false;
164     Nefry.setLed(0, 0, 64);
165     }
166   }
167
168   
169  if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
170   nCounter30s =0;
171   Nefry.setLed(128, 0, 128);
172   timeClient.update();
173   setTime(timeClient.getEpochTime());
174   // Nefry.println(timeClient.getFormattedTime());
175   //Nefry.println((String)AD1[nADcount].nBootedClock+"++");
176   //Nefry.ndelay(100);
177   Nefry.setLed(0, 0, 64);
178  }
179 }
180
181
182
183