• R/O
  • HTTP
  • SSH
  • HTTPS

ITO-manager5.0:

Nefryで開発するIoTツールです


File Info

Rev. ecdb0462b3d170fd786d4d7cbf1662c2c62d1ae5
Size 4,309 bytes
Time 2017-06-06 16:43:02
Author Tomohiro Ito
Log Message

ESP8266 wifi connect

Content

#include <Nefry.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <FS.h>
#include <ESP8266HTTPClient.h>


//NTP test program for Nefry
//Nefryの時計をntpで同期するためのサンプルコードです.開発中です.
//Nefryのwifi関数を使ってWifiが切れたら再接続するように書き換えた
//SPIFFSを使うとHTTP Clientの動作が不安定になるので,取り急ぎ,開発を中止

extern "C" {
  #include "user_interface.h"
}

#define DEVICEID ESP.getChipId()


const String szCommandReadBaseURL = "https://a.yamagata-u.ac.jp/amenity/network/M2M/KinomaControlRequest.aspx";
const String FingurePrintSha1 ="A5 23 3B D5 FA 6C E9 C2 8F 30 17 8F EF F9 EA C8 C5 18 49 3F";
const char* host = "a.yamagata-u.ac.jp";
const int httpPort = 80;
const int httpsPort = 443;

//char toSSID[] = "xxxx";
//char ssidPASSWD[] = "xxxxxx";
bool BlueLED=false;
int nCounterWifiReconnect=0;
int nCounter1s = 0;
int nCounter30s = 0;
int ADvalue = 0;
bool bCommandReadingHttp = false; 
bool bNoNTPupdated = true;
bool bSPIFFS = false;

WiFiUDP ntpUDP;
// You can specify the time server pool and the offset (in seconds, can be
// changed later with setTimeOffset() ). Additionaly you can specify the
// update interval (in milliseconds, can be changed using setUpdateInterval() ).
NTPClient timeClient(ntpUDP, "ntp.nict.jp", 0, 3600000);


void timer1_ISR (void) {
 if (WiFi.status() != WL_CONNECTED) {
    // コネクション確立済み以外のステータスなので
    // 無線LAN接続NG 赤色LED点灯
    Nefry.setLed(255, 0, 0);
    nCounterWifiReconnect++;
    //WiFi.begin();
      if (nCounterWifiReconnect>30){
        nCounterWifiReconnect=0;
        Nefry.setWifiTimeoutClear();
        Nefry.println("Wifi TimeoutClear");
      }
  } else {
    //コネクションOKなので、青色LED点灯
    nCounterWifiReconnect=0;
    Nefry.setLed(0, 0, 64);
  
  }
  ADvalue =  analogRead(A0);
  char buf[128];
  //Nefry.println(ADvalue);
  //2014-10-10T13:50:40+09:00 (TIMEZONE JST)
  //2014-10-10T04:50:40Z (UTC)
 // sprintf(buf,"%04d-%02d-%02dT%02d:%02d:%02dZ",year(), month(), day(),hour(),minute(),second());
//  Nefry.println(buf);
  nCounter1s++;  
  nCounter30s++;

}

void stop_timer1() {
  timer1_disable();
  timer1_detachInterrupt();
}

void start_timer1(unsigned int nIntervalTime_us) {
    timer1_isr_init();
    timer1_attachInterrupt(timer1_ISR);
    timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
    timer1_write(clockCyclesPerMicrosecond() / 16 * nIntervalTime_us); 
}
void setup() {
  // put your setup code here, to run once:
  //NefryではWifeへの接続は不要
 // WiFi.mode(WIFI_STA);
   //WiFi.begin(toSSID, ssidPASSWD);
   timeClient.begin();
   //Nefry.println(DEVICEID);
   start_timer1(1000000); //1000000 us間隔でタイマーを起動
   //SPIFFSのサンプル特性
//  bSPIFFS = SPIFFS.begin();
  //SPIFFS.format();
}

void loop() {
  
 if (bNoNTPupdated==true){
   if (WiFi.status() == WL_CONNECTED) {
    Nefry.setLed(128, 0, 128);
    timeClient.update();
    bNoNTPupdated=false;
    setTime(timeClient.getEpochTime());
 //   Nefry.println(timeClient.getFormattedTime());
    Nefry.setLed(0, 0, 64);
   }
  }
 
 if (nCounter1s >0) {
    if (WiFi.status() == WL_CONNECTED && bCommandReadingHttp == false) {
      bCommandReadingHttp  = true;
      Nefry.setLed(0, 128, 0);
    HTTPClient http;
    String szCommandReadURL = szCommandReadBaseURL + "?DeviceID=" + DEVICEID;
    http.begin(szCommandReadURL,FingurePrintSha1);
    int httpCode = http.GET();
    //Nefry.print(timeClient.getFormattedTime());
    //Nefry.println(httpCode);
    if(httpCode == HTTP_CODE_OK) {
          String payload = http.getString();
        // Nefry.println(" : " + payload);
    }
    http.end();
    nCounter1s =0;
    bCommandReadingHttp = false;
    Nefry.setLed(0, 0, 64);
    }

  }

  
 if (WiFi.status() == WL_CONNECTED && nCounter30s > 30) {
  nCounter30s =0;
  Nefry.setLed(128, 0, 128);
  timeClient.update();
    setTime(timeClient.getEpochTime());
   // Nefry.println(timeClient.getFormattedTime());
   Nefry.ndelay(100);
   Nefry.setLed(0, 0, 64);
    if (bSPIFFS==false ) {
      Nefry.println("SPIFFS.begin fail");
    }else{
      Nefry.println("SPIFFS.begin complete");
    }
 }
}
Show on old repository browser