Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
Fork of WiFi_Camera by
main.cpp
- Committer:
- h_nakanishi
- Date:
- 2016-11-03
- Revision:
- 5:dd5bc7b037c7
- Parent:
- 4:4ea6299ad00e
- Child:
- 6:ac677b091f76
File content as of revision 5:dd5bc7b037c7:
#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_14, P8_15);
ESP8266 wifi;
#define WLAN_SSID "4CE67630E22B" // AP Name
#define WLAN_PASS "t3340pn5mkmkh" // AP PassWord
#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
/****************************************************************************/
/* グローバル変数の定義 */
/****************************************************************************/
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;
/****************************************************************************/
/* プロトタイプ宣言 */
/****************************************************************************/
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();
// Setup GPS
isEnableGPS = setupGPS();
//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");
*/
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++;
}
}
/****************************************************************************/
// 機能 : Wi-Fi通信機能のセットアップ
// 引数 : なし
// 返り値 : ret FALSE(0):失敗 TRUE(1):成功
// 備考 : なし
/****************************************************************************/
static int setupWiFi()
{
int ret = false;
// Wi-Fiとのシリアル通信を開始する
wifi.begin(ESP_Serial, ESP_Baud);
// Wi-Fiモジュールをリセット
resetWiFi();
// FWバージョンを取得
printf("FW Version:");
printf(wifi.getVersion().c_str()); // AT+GMR
printf("\r\n");
// 既に接続済みであれば切断
wifi.leaveAP(); // AT+CWQAP
// 接続の準備
if (wifi.setOprToStationSoftAP()) { // AT+CWMODE=3
printf("to station + softap ok\r\n");
} else {
printf("to station + softap err\r\n");
//return ret;
}
// 接続可能なAP一覧を表示
//printf("%s\r\n", wifi.getAPList().c_str()); // AT+CWLAP
// APに接続する
if (wifi.joinAP(WLAN_SSID, WLAN_PASS)) { // AT+CWJAP="SSID","PASSWORD"
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()) { // AT+CIPMUX=0
printf("multi ok\r\n");
} else {
printf("multi err\r\n");
return ret;
}
printf("setup end\r\n");
ret = true;
return ret;
}
/****************************************************************************/
// 機能 : Wi-Fi送信機能
// 引数 : buffer 送信データ
// 返り値 : ret FALSE(0):失敗 TRUE(1):成功
// 備考 : なし
/****************************************************************************/
static int sendWiFi()
{
int ret = false;
uint8_t buffer[2048] = {0};
bool sendRet = false;
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);
printf(buf);
// 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:[");
for(uint32_t i = 0; i < len; i++) {
printf("%c", buffer[i]);
}
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
