OSDN Git Service

336dff0fd586f933f5b8925a00a5575fd00d1b60
[ito-manager/ITO-manager5.0.git] / trial / HTTP-test / HTTP-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
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;
25
26 //char toSSID[] = "xxxx";
27 //char ssidPASSWD[] = "xxxxxx";
28 bool BlueLED=false;
29 int nCounterWifiReconnect=0;
30 int nCounter1s = 0;
31 int nCounter30s = 0;
32 int ADvalue = 0;
33 bool bCommandReadingHttp = false; 
34 bool bNoNTPupdated = true;
35
36 WiFiUDP ntpUDP;
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);
41
42
43 void timer1_ISR (void) {
44  if (WiFi.status() != WL_CONNECTED) {
45     // コネクション確立済み以外のステータスなので
46     // 無線LAN接続NG 赤色LED点灯
47     Nefry.setLed(255, 0, 0);
48     nCounterWifiReconnect++;
49     //WiFi.begin();
50       if (nCounterWifiReconnect>30){
51         nCounterWifiReconnect=0;
52         Nefry.setWifiTimeoutClear();
53         Nefry.println("Wifi TimeoutClear");
54       }
55   } else {
56     //コネクションOKなので、青色LED点灯
57     nCounterWifiReconnect=0;
58     Nefry.setLed(0, 0, 64);
59   
60   }
61   ADvalue =  analogRead(A0);
62   char buf[128];
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);
68   nCounter1s++;  
69   nCounter30s++;
70
71 }
72
73 void stop_timer1() {
74   timer1_disable();
75   timer1_detachInterrupt();
76 }
77
78 void start_timer1(unsigned int nIntervalTime_us) {
79     timer1_isr_init();
80     timer1_attachInterrupt(timer1_ISR);
81     timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
82     timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us); 
83 }
84 void setup() {
85   // put your setup code here, to run once:
86   //NefryではWifeへの接続は不要
87  // WiFi.mode(WIFI_STA);
88    //WiFi.begin(toSSID, ssidPASSWD);
89    timeClient.begin();
90    //Nefry.println(DEVICEID);
91    start_timer1(1000000); //1000000 us間隔でタイマーを起動
92
93 }
94
95 void loop() {
96   
97  if (bNoNTPupdated==true){
98    if (WiFi.status() == WL_CONNECTED) {
99     Nefry.setLed(128, 0, 128);
100     timeClient.update();
101     bNoNTPupdated=false;
102     setTime(timeClient.getEpochTime());
103  //   Nefry.println(timeClient.getFormattedTime());
104     Nefry.setLed(0, 0, 64);
105    }
106   }
107  
108  if (nCounter1s >0) {
109   nCounter1s =0;
110     if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
111       bCommandReadingHttp  = true;
112       Nefry.setLed(0, 128, 0);
113     HTTPClient http;
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;
123     }
124     http.end();
125     Nefry.setLed(0, 0, 64);
126     }
127
128   }
129
130   
131  if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
132   nCounter30s =0;
133   Nefry.setLed(128, 0, 128);
134   timeClient.update();
135     setTime(timeClient.getEpochTime());
136    // Nefry.println(timeClient.getFormattedTime());
137    Nefry.ndelay(100);
138    Nefry.setLed(0, 0, 64);
139  }
140 }