research_king / Mbed 2 deprecated WiFi_Camera

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of WiFi_Camera by Hiroshi Nakanishi

Revision:
5:dd5bc7b037c7
Parent:
4:4ea6299ad00e
Child:
6:ac677b091f76
--- a/main.cpp	Thu Oct 27 12:20:18 2016 +0000
+++ b/main.cpp	Thu Nov 03 16:26:35 2016 +0000
@@ -1,19 +1,21 @@
 #include "mbed.h"
 #include "ESP8266.h"
+#include "gps.h"
+ 
+#define GPS_ENABLE  1
 
 Serial mc(P2_14, P2_15); //tx rx
 
 /****************************************************************************/
 /* Wi-Fi関連の定義                                                          */
 /****************************************************************************/
-SerialBuffered serial_buffered(P8_13, P8_11);
+SerialBuffered serial_buffered(P8_14, P8_15);
 ESP8266 wifi;
-//#define WLAN_SSID       "HWD15_8C34FD1A799E"          // AP Name
-//#define WLAN_PASS       "2d3n4gnia2m05g7"             // AP PassWord
 #define WLAN_SSID       "4CE67630E22B"                  // AP Name
 #define WLAN_PASS       "t3340pn5mkmkh"                 // AP PassWord
-#define HOST_NAME       "192.168.0.104"                 // HOST Address
-#define HOST_PORT       80                              // HOST Port
+#define HOST_NAME       "104.199.222.173"               // HOST Address
+#define HOST_URL        "/r-king/space/webapi.php?"     // HOST URL
+#define HOST_PORT       (80)                            // HOST Port
 #define ESP_Serial      serial_buffered                 // WiFi Module Port
 #define ESP_Baud        115200                          // WiFi Module Port
 #define WIFI_BUF        256                             // WiFi Buffer Length
@@ -21,26 +23,53 @@
 /****************************************************************************/
 /* グローバル変数の定義                                                     */
 /****************************************************************************/
-int isEnableWiFi =      false;
+int     isEnableWiFi =      false;
+int     isEnableGPS  =      false;
+double  g_lat        =      12.3f;
+double  g_lon        =      45.6f;
+double  g_alt        =      78.9f;
+int     g_year       =      2016;
+int     g_month      =      01;
+int     g_day        =      01;
+int     g_hour       =      00;
+int     g_min        =      00;
+int     g_sec        =      00;
 
 /****************************************************************************/
 /* プロトタイプ宣言                                                         */
 /****************************************************************************/
-int setupWiFi();
-int sendWiFi();
+static void loop(void);
+static int setupWiFi();
+static int sendWiFi();
+static void resetWiFi();
+#if GPS_ENABLE
+int setupGPS();
+static void onGpsGdaReceivedCallback(SerialGPS::NMEA_GGA* data);
+static void onGpsZdaReceivedCallback(SerialGPS::NMEA_ZDA* data);
+static SerialGPS    s_SerialGPS(P2_14, P2_15);    // ★PIN番号は機器にあわせること★
+static SerialGPS::GpsCallbackTable s_GpsCallbackTable = {
+    /*.log_cbfunc = */ NULL,
+    /*.gga_cbfunc = */ onGpsGdaReceivedCallback,
+    /*.gll_cbfunc = */ NULL,
+    /*.gsa_cbfunc = */ NULL,
+    /*.gsv_cbfunc = */ NULL,
+    /*.rmc_cbfunc = */ NULL,
+    /*.vtg_cbfunc = */ NULL,
+    /*.zda_cbfunc = */ onGpsZdaReceivedCallback
+};
+#endif //GPS_ENABLE
 
 int main() {
+        printf("[Serial Start]\r\n");
         
         // Setup Wi-Fi
         isEnableWiFi = setupWiFi();
-
-        // Send Wi-Fi
-        sendWiFi();
         
+        // Setup GPS
+        isEnableGPS = setupGPS();
         
-        printf("[Serial Start]\n");
-        mc.baud(115200);
-        
+        //mc.baud(115200);
+        /*
         char data1[7] = {0xFF,0x1C,0x04,0x03,0x01,0x01,0x28};//時計回り
         mc.printf(data1);
         printf(data1);
@@ -61,7 +90,30 @@
 //        printf(data3);
         
         printf("[Serial End]\n");
+        */
         
+        loop();
+}
+int cnt = 0;
+void loop(void)
+{
+    while(true)
+    {
+#if GPS_ENABLE
+        s_SerialGPS.Processing();//
+#endif  //GPS_ENABLE   
+        wait_ms(10);
+        
+        if(cnt >= 1000)
+        {
+            if(isEnableWiFi == true)
+            {
+                sendWiFi();
+            }
+            cnt = 0;   
+        }
+        cnt++;
+    }
 }
 
 /****************************************************************************/
@@ -70,28 +122,37 @@
 //  返り値     : ret           FALSE(0):失敗 TRUE(1):成功
 //  備考      : なし
 /****************************************************************************/
-int setupWiFi()
+static int setupWiFi()
 {
     int ret    = false;
 
-    // FWバージョンを取得する
+    // Wi-Fiとのシリアル通信を開始する
     wifi.begin(ESP_Serial, ESP_Baud);
+    
+    // Wi-Fiモジュールをリセット
+    resetWiFi();
+    
+    // FWバージョンを取得
     printf("FW Version:");
-
-    printf(wifi.getVersion().c_str());
+    printf(wifi.getVersion().c_str());            // AT+GMR
     printf("\r\n");
     
+    // 既に接続済みであれば切断
+    wifi.leaveAP();                               // AT+CWQAP
+    
     // 接続の準備
-    if (wifi.setOprToStationSoftAP()) {
+    if (wifi.setOprToStationSoftAP()) {           // AT+CWMODE=3
         printf("to station + softap ok\r\n");
     } else {
         printf("to station + softap err\r\n");
         //return ret;
     }
-
-//  Serial.println(wifi.getAPList().c_str());
+    
+    // 接続可能なAP一覧を表示
+    //printf("%s\r\n", wifi.getAPList().c_str()); // AT+CWLAP
+    
     // APに接続する
-    if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) {
+    if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) {      // AT+CWJAP="SSID","PASSWORD"
         printf("Join AP success\r\n");
         printf("IP:");
         printf( wifi.getLocalIP().c_str());       
@@ -102,10 +163,11 @@
     }
 
     // コネクションを1つのみに設定
-    if (wifi.disableMUX()) {
-        printf("single ok\r\n");
+    
+    if (wifi.disableMUX()) {                        // AT+CIPMUX=0
+        printf("multi ok\r\n");
     } else {
-        printf("single err\r\n");
+        printf("multi err\r\n");
         return ret;
     }
     
@@ -121,29 +183,50 @@
 //  返り値     : ret           FALSE(0):失敗 TRUE(1):成功
 //  備考      : なし
 /****************************************************************************/
-int sendWiFi()
+static int sendWiFi()
 {
     int ret = false;
     
     uint8_t buffer[2048] = {0};
     bool sendRet = false;
 
-    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
+    printf("sendWiFi\r\n");
+    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {         // AT+CIPSTART="TCP","104.199.222.173",80
         printf("create tcp ok\r\n");
     } else {
         printf("create tcp err\r\n");
+        // Wi-Fiモジュールをリセット
+        resetWiFi();
+        return ret;
     }
+    wait(1);
+    
+    // build GET Request
+    char buf[1024] = {'\0'};
+    // sprintf(buf, "GET %saction=add&token=001&lat=%lf&lon=%lf&height=%lf HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
+    sprintf(buf, "GET %saction=add&token=001&lat=%lf&lon=%lf&height=%lf HTTP/1.1\r\nHost: %s\r\n\r\n",
+        HOST_URL,
+        g_lat,
+        g_lon,
+        g_alt,
+        HOST_NAME);
 
-    char *hello = "GET / HTTP/1.1\r\nHost: yamaguchi-note:80/researchking/databasetest/dbupdate.php?token=COTTON&lat=34.639422423473846&lon=135.22933959960938&type=3\r\nConnection: close\r\n\r\n";
-    //char *hello = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
+    printf(buf);
     
-    sendRet = wifi.send((const uint8_t*)hello, strlen(hello));
+    // Send GET Request
+    sendRet = wifi.send((const uint8_t*)buf, strlen(buf));  // AT+CIPSEND=送信バイト数
     if(sendRet == true) {
         printf("Success!!\r\n");
+        ret = true;
     } else {
         printf("Fail!!\r\n");
+        // Wi-Fiモジュールをリセット
+        resetWiFi();
+        return ret;
     }
+    wait(1);
     
+    // wait Response (T/O 10Sec)
     uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
     if (len > 0) {
         printf("Received:[");
@@ -152,12 +235,107 @@
         }
         printf("]\r\n");
     }
-    
+
+    // release TCP
     if (wifi.releaseTCP()) {
         printf("release tcp ok\r\n");
     } else {
         printf("release tcp err\r\n");
     }
+
+    return ret;
+}
+
+/****************************************************************************/
+//  機能      : Wi-Fiリセット機能
+//  引数      : buffer        送信データ
+//  返り値     : ret           FALSE(0):失敗 TRUE(1):成功
+//  備考      : なし
+/****************************************************************************/
+static void resetWiFi()
+{
+    printf("Wi-Fi kick\r\n");
+    wifi.kick();                                   // AT
+    printf("Wi-Fi reset\r\n");
+    wifi.restart();                                // AT+RST
+    wait(10);
+}
+
+
+#if GPS_ENABLE
+/****************************************************************************
+モジュール名  : GPS Setup
+関数名         : setupGPS
+引数          : なし
+戻り値         : GPS受信データ
+処理概要        : GPS受信処理
+注意事項        : なし
+****************************************************************************/
+int setupGPS()
+{
+    int ret = false;
+#if GPS_ENABLE
+    // GPSライブラリの呼び出しサンプルコード
+    s_SerialGPS.Init(); // GPS初期化
+    s_SerialGPS.SetGpsCallback(&s_GpsCallbackTable);  // GPSコールバック登録
+    s_SerialGPS.RcvIsrEnable();   // GPS受信有効
+    ret = true;
+#endif // GPS_ENABLE
     return ret;
 }
 
+
+/****************************************************************************
+モジュール名  : GPS受信コールバック
+関数名         : onGpsGdaReceivedCallback
+引数          : なし
+戻り値         : GPS受信データ
+処理概要        : GPS受信処理
+注意事項        : なし
+****************************************************************************/
+void onGpsGdaReceivedCallback(SerialGPS::NMEA_GGA* data)
+{
+    double d_lat,d_lon,m_lat,m_lon;
+    
+    // lat
+    d_lat=int(data->latitude/100);
+    m_lat=(data->latitude - d_lat*100)/60;
+    g_lat=d_lat+m_lat;
+    
+    // lon
+    d_lon=int(data->longitude/100);
+    m_lon=(data->longitude - d_lon*100)/60;
+    g_lon=d_lon+m_lon;
+    
+    // alt
+    g_alt = data->altitude;
+
+    printf("lat=%lf, lon=%lf, alt=%lf\r\n", g_lat, g_lon, g_alt);
+}
+
+/****************************************************************************
+モジュール名  : GPS受信コールバック
+関数名         : onGpsZdaReceivedCallback
+引数          : なし
+戻り値         : GPS受信データ
+処理概要        : GPS受信処理
+注意事項        : なし
+****************************************************************************/
+void onGpsZdaReceivedCallback(SerialGPS::NMEA_ZDA* data)
+{
+    g_year      = data->year;
+    g_month     = data->month;
+    g_day       = data->day;
+    g_hour      = (data->hour) + (data->hour_diff);
+    g_min       = (data->min)  + (data->min_diff);
+    g_sec       = data->sec;
+    
+    printf("TIME:%04d/%02d/%02d %02d:%02d:%02d\r\n",
+        g_year,
+        g_month,
+        g_day,
+        g_hour,
+        g_min,
+        g_sec);
+} 
+#endif //GPS_ENABLE
\ No newline at end of file