#include #include #include #include "Arduino.h" #define JST 3600*9 //--------------------------------------------------------------------------------------------------------- const char *ssid = "SSID"; //SSID const char *password = "PASS"; //PASS String SsubscTime = "0000/00/00_00:00:00"; //登録日時⇒0000/00/00_00:00:00のフォーマットで入力 ※同登録日時を作らないように String Sproject = "project"; //プロジェクト String Splace = "place"; //設置場所 String Shuman = "human"; //利用者 //--------------------------------------------------------------------------------------------------------- String SserverURL = "http://www.48v.me/~08takuya/cgi-bin/IoT/main/IoT_manage.py"; String Smachine = "ESP8266"; String SnowTime; //Sname; 無し //Smail; 無し String SipAdd; void settingWifi() { WiFi.mode(WIFI_STA); delay(500); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); } //ntp準備 configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp"); SipAdd = ipToString(WiFi.localIP()); } void SendData() { GettingTime(); //nowtime更新 PostData(); } void GettingTime(){ time_t t; struct tm *tm; //現在時間取得 t = time(NULL); tm = localtime(&t); //ここからフォーマット合わせ //月 String month = String(tm->tm_mon+1); if(month.length() == 1){ month = "0" + month; } //日 String day = String(tm->tm_mday); if(day.length() == 1){ day = "0" + day; } //時間 String hour = String(tm->tm_hour); if(hour.length() == 1){ hour = "0" + hour; } //分 String minute = String(tm->tm_min); if(minute.length() == 1){ minute = "0" + minute; } //秒 String second = String(tm->tm_sec); if(second.length() == 1){ second = "0" + second; } SnowTime = String(tm->tm_year+1900) + month + day + hour + minute + second; } void PostData(){ WiFiClient client; HTTPClient http; // POSTデータのセットアップ String postData = "subscTime="+SsubscTime+"&serverURL="+SserverURL+"&machine="+Smachine+"&project="+Sproject+"&nowTime="+SnowTime+"&place="+Splace+"&human="+Shuman+"&ipAdd="+SipAdd; if (!http.begin(client,SserverURL)) { return; } http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int responseCode = http.POST(postData); String body = http.getString(); // 改行の位置を探す int newlineIndex = body.indexOf('\n'); // 時間取得 String NextTime = body.substring(0, newlineIndex); // url取得 String SserverURL = body.substring(newlineIndex + 1); SipAdd = ipToString(WiFi.localIP()); http.end(); } String ipToString(uint32_t ip){ String result = ""; result += String((ip & 0xFF), 10); result += "."; result += String((ip & 0xFF00) >> 8, 10); result += "."; result += String((ip & 0xFF0000) >> 16, 10); result += "."; result += String((ip & 0xFF000000) >> 24, 10); return result; }