research_king / Mbed 2 deprecated WiFi_Camera

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of WiFi_Camera by Hiroshi Nakanishi

Revision:
4:4ea6299ad00e
Child:
5:dd5bc7b037c7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 27 12:20:18 2016 +0000
@@ -0,0 +1,163 @@
+#include "mbed.h"
+#include "ESP8266.h"
+
+Serial mc(P2_14, P2_15); //tx rx
+
+/****************************************************************************/
+/* Wi-Fi関連の定義                                                          */
+/****************************************************************************/
+SerialBuffered serial_buffered(P8_13, P8_11);
+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 ESP_Serial      serial_buffered                 // WiFi Module Port
+#define ESP_Baud        115200                          // WiFi Module Port
+#define WIFI_BUF        256                             // WiFi Buffer Length
+
+/****************************************************************************/
+/* グローバル変数の定義                                                     */
+/****************************************************************************/
+int isEnableWiFi =      false;
+
+/****************************************************************************/
+/* プロトタイプ宣言                                                         */
+/****************************************************************************/
+int setupWiFi();
+int sendWiFi();
+
+int main() {
+        
+        // Setup Wi-Fi
+        isEnableWiFi = setupWiFi();
+
+        // Send Wi-Fi
+        sendWiFi();
+        
+        
+        printf("[Serial Start]\n");
+        mc.baud(115200);
+        
+        char data1[7] = {0xFF,0x1C,0x04,0x03,0x01,0x01,0x28};//時計回り
+        mc.printf(data1);
+        printf(data1);
+        
+        wait(3);
+        
+        char data2[7] = {0xFF,0x1C,0x04,0x03,0x01,0x02,0x28};//反時計まわり
+        mc.printf(data2);
+        printf(data2);
+        
+        wait(3);
+        
+        char data3[7] = {0xFF,0x1C,0x04,0x03,0x01,0x00,0x3C};//モータ停止TODO:00
+//        mc.printf(data3);
+        for(int i = 0 ; i < sizeof(data3) ; i++){
+            mc.putc(data3[i]);
+        }
+//        printf(data3);
+        
+        printf("[Serial End]\n");
+        
+}
+
+/****************************************************************************/
+//  機能      : Wi-Fi通信機能のセットアップ
+//  引数      : なし
+//  返り値     : ret           FALSE(0):失敗 TRUE(1):成功
+//  備考      : なし
+/****************************************************************************/
+int setupWiFi()
+{
+    int ret    = false;
+
+    // FWバージョンを取得する
+    wifi.begin(ESP_Serial, ESP_Baud);
+    printf("FW Version:");
+
+    printf(wifi.getVersion().c_str());
+    printf("\r\n");
+    
+    // 接続の準備
+    if (wifi.setOprToStationSoftAP()) {
+        printf("to station + softap ok\r\n");
+    } else {
+        printf("to station + softap err\r\n");
+        //return ret;
+    }
+
+//  Serial.println(wifi.getAPList().c_str());
+    // APに接続する
+    if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) {
+        printf("Join AP success\r\n");
+        printf("IP:");
+        printf( wifi.getLocalIP().c_str());       
+        printf("\r\n");
+    } else {
+        printf("Join AP failure\r\n");
+        return ret;
+    }
+
+    // コネクションを1つのみに設定
+    if (wifi.disableMUX()) {
+        printf("single ok\r\n");
+    } else {
+        printf("single err\r\n");
+        return ret;
+    }
+    
+    printf("setup end\r\n");
+    ret = true;
+
+    return ret;
+}
+
+/****************************************************************************/
+//  機能      : Wi-Fi送信機能
+//  引数      : buffer        送信データ
+//  返り値     : ret           FALSE(0):失敗 TRUE(1):成功
+//  備考      : なし
+/****************************************************************************/
+int sendWiFi()
+{
+    int ret = false;
+    
+    uint8_t buffer[2048] = {0};
+    bool sendRet = false;
+
+    if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
+        printf("create tcp ok\r\n");
+    } else {
+        printf("create tcp err\r\n");
+    }
+
+    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";
+    
+    sendRet = wifi.send((const uint8_t*)hello, strlen(hello));
+    if(sendRet == true) {
+        printf("Success!!\r\n");
+    } else {
+        printf("Fail!!\r\n");
+    }
+    
+    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
+    if (len > 0) {
+        printf("Received:[");
+        for(uint32_t i = 0; i < len; i++) {
+            printf("%c", buffer[i]);
+        }
+        printf("]\r\n");
+    }
+    
+    if (wifi.releaseTCP()) {
+        printf("release tcp ok\r\n");
+    } else {
+        printf("release tcp err\r\n");
+    }
+    return ret;
+}
+