research_king / Mbed 2 deprecated WiFi_Camera

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of WiFi_Camera by Hiroshi Nakanishi

Committer:
thorai
Date:
Thu Oct 27 12:20:18 2016 +0000
Revision:
4:4ea6299ad00e
Child:
5:dd5bc7b037c7
seadrone wifi + motortest

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thorai 4:4ea6299ad00e 1 #include "mbed.h"
thorai 4:4ea6299ad00e 2 #include "ESP8266.h"
thorai 4:4ea6299ad00e 3
thorai 4:4ea6299ad00e 4 Serial mc(P2_14, P2_15); //tx rx
thorai 4:4ea6299ad00e 5
thorai 4:4ea6299ad00e 6 /****************************************************************************/
thorai 4:4ea6299ad00e 7 /* Wi-Fi関連の定義 */
thorai 4:4ea6299ad00e 8 /****************************************************************************/
thorai 4:4ea6299ad00e 9 SerialBuffered serial_buffered(P8_13, P8_11);
thorai 4:4ea6299ad00e 10 ESP8266 wifi;
thorai 4:4ea6299ad00e 11 //#define WLAN_SSID "HWD15_8C34FD1A799E" // AP Name
thorai 4:4ea6299ad00e 12 //#define WLAN_PASS "2d3n4gnia2m05g7" // AP PassWord
thorai 4:4ea6299ad00e 13 #define WLAN_SSID "4CE67630E22B" // AP Name
thorai 4:4ea6299ad00e 14 #define WLAN_PASS "t3340pn5mkmkh" // AP PassWord
thorai 4:4ea6299ad00e 15 #define HOST_NAME "192.168.0.104" // HOST Address
thorai 4:4ea6299ad00e 16 #define HOST_PORT 80 // HOST Port
thorai 4:4ea6299ad00e 17 #define ESP_Serial serial_buffered // WiFi Module Port
thorai 4:4ea6299ad00e 18 #define ESP_Baud 115200 // WiFi Module Port
thorai 4:4ea6299ad00e 19 #define WIFI_BUF 256 // WiFi Buffer Length
thorai 4:4ea6299ad00e 20
thorai 4:4ea6299ad00e 21 /****************************************************************************/
thorai 4:4ea6299ad00e 22 /* グローバル変数の定義 */
thorai 4:4ea6299ad00e 23 /****************************************************************************/
thorai 4:4ea6299ad00e 24 int isEnableWiFi = false;
thorai 4:4ea6299ad00e 25
thorai 4:4ea6299ad00e 26 /****************************************************************************/
thorai 4:4ea6299ad00e 27 /* プロトタイプ宣言 */
thorai 4:4ea6299ad00e 28 /****************************************************************************/
thorai 4:4ea6299ad00e 29 int setupWiFi();
thorai 4:4ea6299ad00e 30 int sendWiFi();
thorai 4:4ea6299ad00e 31
thorai 4:4ea6299ad00e 32 int main() {
thorai 4:4ea6299ad00e 33
thorai 4:4ea6299ad00e 34 // Setup Wi-Fi
thorai 4:4ea6299ad00e 35 isEnableWiFi = setupWiFi();
thorai 4:4ea6299ad00e 36
thorai 4:4ea6299ad00e 37 // Send Wi-Fi
thorai 4:4ea6299ad00e 38 sendWiFi();
thorai 4:4ea6299ad00e 39
thorai 4:4ea6299ad00e 40
thorai 4:4ea6299ad00e 41 printf("[Serial Start]\n");
thorai 4:4ea6299ad00e 42 mc.baud(115200);
thorai 4:4ea6299ad00e 43
thorai 4:4ea6299ad00e 44 char data1[7] = {0xFF,0x1C,0x04,0x03,0x01,0x01,0x28};//時計回り
thorai 4:4ea6299ad00e 45 mc.printf(data1);
thorai 4:4ea6299ad00e 46 printf(data1);
thorai 4:4ea6299ad00e 47
thorai 4:4ea6299ad00e 48 wait(3);
thorai 4:4ea6299ad00e 49
thorai 4:4ea6299ad00e 50 char data2[7] = {0xFF,0x1C,0x04,0x03,0x01,0x02,0x28};//反時計まわり
thorai 4:4ea6299ad00e 51 mc.printf(data2);
thorai 4:4ea6299ad00e 52 printf(data2);
thorai 4:4ea6299ad00e 53
thorai 4:4ea6299ad00e 54 wait(3);
thorai 4:4ea6299ad00e 55
thorai 4:4ea6299ad00e 56 char data3[7] = {0xFF,0x1C,0x04,0x03,0x01,0x00,0x3C};//モータ停止TODO:00
thorai 4:4ea6299ad00e 57 // mc.printf(data3);
thorai 4:4ea6299ad00e 58 for(int i = 0 ; i < sizeof(data3) ; i++){
thorai 4:4ea6299ad00e 59 mc.putc(data3[i]);
thorai 4:4ea6299ad00e 60 }
thorai 4:4ea6299ad00e 61 // printf(data3);
thorai 4:4ea6299ad00e 62
thorai 4:4ea6299ad00e 63 printf("[Serial End]\n");
thorai 4:4ea6299ad00e 64
thorai 4:4ea6299ad00e 65 }
thorai 4:4ea6299ad00e 66
thorai 4:4ea6299ad00e 67 /****************************************************************************/
thorai 4:4ea6299ad00e 68 // 機能 : Wi-Fi通信機能のセットアップ
thorai 4:4ea6299ad00e 69 // 引数 : なし
thorai 4:4ea6299ad00e 70 // 返り値 : ret FALSE(0):失敗 TRUE(1):成功
thorai 4:4ea6299ad00e 71 // 備考 : なし
thorai 4:4ea6299ad00e 72 /****************************************************************************/
thorai 4:4ea6299ad00e 73 int setupWiFi()
thorai 4:4ea6299ad00e 74 {
thorai 4:4ea6299ad00e 75 int ret = false;
thorai 4:4ea6299ad00e 76
thorai 4:4ea6299ad00e 77 // FWバージョンを取得する
thorai 4:4ea6299ad00e 78 wifi.begin(ESP_Serial, ESP_Baud);
thorai 4:4ea6299ad00e 79 printf("FW Version:");
thorai 4:4ea6299ad00e 80
thorai 4:4ea6299ad00e 81 printf(wifi.getVersion().c_str());
thorai 4:4ea6299ad00e 82 printf("\r\n");
thorai 4:4ea6299ad00e 83
thorai 4:4ea6299ad00e 84 // 接続の準備
thorai 4:4ea6299ad00e 85 if (wifi.setOprToStationSoftAP()) {
thorai 4:4ea6299ad00e 86 printf("to station + softap ok\r\n");
thorai 4:4ea6299ad00e 87 } else {
thorai 4:4ea6299ad00e 88 printf("to station + softap err\r\n");
thorai 4:4ea6299ad00e 89 //return ret;
thorai 4:4ea6299ad00e 90 }
thorai 4:4ea6299ad00e 91
thorai 4:4ea6299ad00e 92 // Serial.println(wifi.getAPList().c_str());
thorai 4:4ea6299ad00e 93 // APに接続する
thorai 4:4ea6299ad00e 94 if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) {
thorai 4:4ea6299ad00e 95 printf("Join AP success\r\n");
thorai 4:4ea6299ad00e 96 printf("IP:");
thorai 4:4ea6299ad00e 97 printf( wifi.getLocalIP().c_str());
thorai 4:4ea6299ad00e 98 printf("\r\n");
thorai 4:4ea6299ad00e 99 } else {
thorai 4:4ea6299ad00e 100 printf("Join AP failure\r\n");
thorai 4:4ea6299ad00e 101 return ret;
thorai 4:4ea6299ad00e 102 }
thorai 4:4ea6299ad00e 103
thorai 4:4ea6299ad00e 104 // コネクションを1つのみに設定
thorai 4:4ea6299ad00e 105 if (wifi.disableMUX()) {
thorai 4:4ea6299ad00e 106 printf("single ok\r\n");
thorai 4:4ea6299ad00e 107 } else {
thorai 4:4ea6299ad00e 108 printf("single err\r\n");
thorai 4:4ea6299ad00e 109 return ret;
thorai 4:4ea6299ad00e 110 }
thorai 4:4ea6299ad00e 111
thorai 4:4ea6299ad00e 112 printf("setup end\r\n");
thorai 4:4ea6299ad00e 113 ret = true;
thorai 4:4ea6299ad00e 114
thorai 4:4ea6299ad00e 115 return ret;
thorai 4:4ea6299ad00e 116 }
thorai 4:4ea6299ad00e 117
thorai 4:4ea6299ad00e 118 /****************************************************************************/
thorai 4:4ea6299ad00e 119 // 機能 : Wi-Fi送信機能
thorai 4:4ea6299ad00e 120 // 引数 : buffer 送信データ
thorai 4:4ea6299ad00e 121 // 返り値 : ret FALSE(0):失敗 TRUE(1):成功
thorai 4:4ea6299ad00e 122 // 備考 : なし
thorai 4:4ea6299ad00e 123 /****************************************************************************/
thorai 4:4ea6299ad00e 124 int sendWiFi()
thorai 4:4ea6299ad00e 125 {
thorai 4:4ea6299ad00e 126 int ret = false;
thorai 4:4ea6299ad00e 127
thorai 4:4ea6299ad00e 128 uint8_t buffer[2048] = {0};
thorai 4:4ea6299ad00e 129 bool sendRet = false;
thorai 4:4ea6299ad00e 130
thorai 4:4ea6299ad00e 131 if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
thorai 4:4ea6299ad00e 132 printf("create tcp ok\r\n");
thorai 4:4ea6299ad00e 133 } else {
thorai 4:4ea6299ad00e 134 printf("create tcp err\r\n");
thorai 4:4ea6299ad00e 135 }
thorai 4:4ea6299ad00e 136
thorai 4:4ea6299ad00e 137 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";
thorai 4:4ea6299ad00e 138 //char *hello = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
thorai 4:4ea6299ad00e 139
thorai 4:4ea6299ad00e 140 sendRet = wifi.send((const uint8_t*)hello, strlen(hello));
thorai 4:4ea6299ad00e 141 if(sendRet == true) {
thorai 4:4ea6299ad00e 142 printf("Success!!\r\n");
thorai 4:4ea6299ad00e 143 } else {
thorai 4:4ea6299ad00e 144 printf("Fail!!\r\n");
thorai 4:4ea6299ad00e 145 }
thorai 4:4ea6299ad00e 146
thorai 4:4ea6299ad00e 147 uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
thorai 4:4ea6299ad00e 148 if (len > 0) {
thorai 4:4ea6299ad00e 149 printf("Received:[");
thorai 4:4ea6299ad00e 150 for(uint32_t i = 0; i < len; i++) {
thorai 4:4ea6299ad00e 151 printf("%c", buffer[i]);
thorai 4:4ea6299ad00e 152 }
thorai 4:4ea6299ad00e 153 printf("]\r\n");
thorai 4:4ea6299ad00e 154 }
thorai 4:4ea6299ad00e 155
thorai 4:4ea6299ad00e 156 if (wifi.releaseTCP()) {
thorai 4:4ea6299ad00e 157 printf("release tcp ok\r\n");
thorai 4:4ea6299ad00e 158 } else {
thorai 4:4ea6299ad00e 159 printf("release tcp err\r\n");
thorai 4:4ea6299ad00e 160 }
thorai 4:4ea6299ad00e 161 return ret;
thorai 4:4ea6299ad00e 162 }
thorai 4:4ea6299ad00e 163