sun peng / wifi_example

Dependents:   STM32F103C8T6_OneNet_IoT

Committer:
dadangjia
Date:
Thu Nov 07 23:56:29 2019 +0000
Revision:
1:8512a7d76959
Parent:
0:cbe8a0553d45
STM32F103C8T6,ONENET,ESP8266,DH11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TaylorGy 0:cbe8a0553d45 1 /************************************************************/
TaylorGy 0:cbe8a0553d45 2 /* (C) 2016 Beijing ARM Accelerator Technology Co., Ltd. */
TaylorGy 0:cbe8a0553d45 3 /* Description: Wifi Client by using Grove_wifi module. */
TaylorGy 0:cbe8a0553d45 4 /* Author: ss.pan */
TaylorGy 0:cbe8a0553d45 5 /* Version: 1.01 */
TaylorGy 0:cbe8a0553d45 6 /* Date: 2017-03-28 */
TaylorGy 0:cbe8a0553d45 7 /************************************************************/
TaylorGy 0:cbe8a0553d45 8
TaylorGy 0:cbe8a0553d45 9 #include "wifi_example.h"
TaylorGy 0:cbe8a0553d45 10
TaylorGy 0:cbe8a0553d45 11 ESP8266 wifi(PA_9,PA_10);
dadangjia 1:8512a7d76959 12 Serial pc(PB_10, PB_11); //蓝牙串口波特率115200
TaylorGy 0:cbe8a0553d45 13
TaylorGy 0:cbe8a0553d45 14 EdpPacket* send_pack;
TaylorGy 0:cbe8a0553d45 15 char send_buf[MAX_SEND_BUF_LEN];
TaylorGy 0:cbe8a0553d45 16
TaylorGy 0:cbe8a0553d45 17 /**
TaylorGy 0:cbe8a0553d45 18 * @brief initial ESP8266,and connect to wifi ap
TaylorGy 0:cbe8a0553d45 19 **/
TaylorGy 0:cbe8a0553d45 20 void connectInit(void)
TaylorGy 0:cbe8a0553d45 21 {
TaylorGy 0:cbe8a0553d45 22 bool ret;
dadangjia 1:8512a7d76959 23 pc.baud(115200);
TaylorGy 0:cbe8a0553d45 24 pc.printf("starting......\r\n");
TaylorGy 0:cbe8a0553d45 25 wifi.reset();
TaylorGy 0:cbe8a0553d45 26 wait_ms(500);
TaylorGy 0:cbe8a0553d45 27 ret = wifi.startup(1); //start as client mode
TaylorGy 0:cbe8a0553d45 28 wait_ms(200);
TaylorGy 0:cbe8a0553d45 29 if(ret != true) {
TaylorGy 0:cbe8a0553d45 30 pc.printf("startup failed\r\n");
TaylorGy 0:cbe8a0553d45 31 ret = true;
TaylorGy 0:cbe8a0553d45 32 } else {
TaylorGy 0:cbe8a0553d45 33 pc.printf("set as client mode\r\n");
TaylorGy 0:cbe8a0553d45 34 }
TaylorGy 0:cbe8a0553d45 35 wifi.connect(APNAME,APPASSWD); //conenet to a existed wifi ap
TaylorGy 0:cbe8a0553d45 36 wait_ms(200);
TaylorGy 0:cbe8a0553d45 37 ret = wifi.isConnected(); //check whether connect successed
TaylorGy 0:cbe8a0553d45 38 if(ret != true) {
TaylorGy 0:cbe8a0553d45 39 pc.printf("connect failed\r\n");
TaylorGy 0:cbe8a0553d45 40 ret = true;
TaylorGy 0:cbe8a0553d45 41 } else {
TaylorGy 0:cbe8a0553d45 42 pc.printf("now connected!\r\n");
TaylorGy 0:cbe8a0553d45 43 }
TaylorGy 0:cbe8a0553d45 44 pc.printf("the ip address is : %s\r\n",wifi.getIPAddress()); //send ip address to pc
TaylorGy 0:cbe8a0553d45 45 ret = wifi.open("TCP", TCPNUM, TCPADD, TCPPORT); //connte to a TCP socket
TaylorGy 0:cbe8a0553d45 46 wait_ms(200);
TaylorGy 0:cbe8a0553d45 47 checkSocketStatus();
TaylorGy 0:cbe8a0553d45 48 }
TaylorGy 0:cbe8a0553d45 49
TaylorGy 0:cbe8a0553d45 50 /**
TaylorGy 0:cbe8a0553d45 51 * @brief checek socket status by calling a rebuilded function ESP8266::isOpen()
TaylorGy 0:cbe8a0553d45 52 **/
TaylorGy 0:cbe8a0553d45 53 int checkSocketStatus(void)
TaylorGy 0:cbe8a0553d45 54 {
TaylorGy 0:cbe8a0553d45 55 int res;
TaylorGy 0:cbe8a0553d45 56 res = wifi.isOpen();
TaylorGy 0:cbe8a0553d45 57 switch(res) {
TaylorGy 0:cbe8a0553d45 58 case 0: {
TaylorGy 0:cbe8a0553d45 59 pc.printf("socket conneceted successfully\r\n");
TaylorGy 0:cbe8a0553d45 60 return res;
TaylorGy 0:cbe8a0553d45 61 }
TaylorGy 0:cbe8a0553d45 62 case -1: {
TaylorGy 0:cbe8a0553d45 63 pc.printf("unknown error\r\n");
TaylorGy 0:cbe8a0553d45 64 return res;
TaylorGy 0:cbe8a0553d45 65 }
TaylorGy 0:cbe8a0553d45 66 case -2: {
TaylorGy 0:cbe8a0553d45 67 pc.printf("connected to ap, get an ip address\r\n");
TaylorGy 0:cbe8a0553d45 68 return res;
TaylorGy 0:cbe8a0553d45 69 }
TaylorGy 0:cbe8a0553d45 70 case -4: {
TaylorGy 0:cbe8a0553d45 71 pc.printf("lost connection\r\n");
TaylorGy 0:cbe8a0553d45 72 return res;
TaylorGy 0:cbe8a0553d45 73 }
TaylorGy 0:cbe8a0553d45 74 case -5: {
TaylorGy 0:cbe8a0553d45 75 pc.printf("unconnected to ap\r\n");
TaylorGy 0:cbe8a0553d45 76 return res;
TaylorGy 0:cbe8a0553d45 77 }
TaylorGy 0:cbe8a0553d45 78 default: {
TaylorGy 0:cbe8a0553d45 79 pc.printf("unknown error:%d\r\n",res);
TaylorGy 0:cbe8a0553d45 80 break;
TaylorGy 0:cbe8a0553d45 81 }
TaylorGy 0:cbe8a0553d45 82 }
TaylorGy 0:cbe8a0553d45 83 return;
TaylorGy 0:cbe8a0553d45 84 }
TaylorGy 0:cbe8a0553d45 85
TaylorGy 0:cbe8a0553d45 86
TaylorGy 0:cbe8a0553d45 87 /**
TaylorGy 0:cbe8a0553d45 88 * @brief send cJSON type data
TaylorGy 0:cbe8a0553d45 89 **/
TaylorGy 0:cbe8a0553d45 90 void sendJsonDat(char* dataName, float data)
TaylorGy 0:cbe8a0553d45 91 {
TaylorGy 0:cbe8a0553d45 92 bool ret;
TaylorGy 0:cbe8a0553d45 93 cJSON *json_data = cJSON_CreateObject(); //create a new json data
TaylorGy 0:cbe8a0553d45 94 cJSON_AddNumberToObject(json_data, dataName, data); //pack data into json package
TaylorGy 0:cbe8a0553d45 95
TaylorGy 0:cbe8a0553d45 96 send_pack = PacketSavedataJson(DEVICEID, json_data, 3); //pack send data into EDP package
TaylorGy 0:cbe8a0553d45 97 pc.printf("now sending JSON from data to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 98 ret = wifi.send(0,(const char*)send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud
TaylorGy 0:cbe8a0553d45 99 if(ret != true) { //check whether send successfully
TaylorGy 0:cbe8a0553d45 100 pc.printf("failed to send Json data\r\n");
TaylorGy 0:cbe8a0553d45 101 ret = true;
TaylorGy 0:cbe8a0553d45 102 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 103 } else {
TaylorGy 0:cbe8a0553d45 104 pc.printf("Json data sended\r\n");
TaylorGy 0:cbe8a0553d45 105 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 106 }
TaylorGy 0:cbe8a0553d45 107 cJSON_Delete(json_data); //delete json_data, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 108 wait_ms(500);
TaylorGy 0:cbe8a0553d45 109 }
TaylorGy 0:cbe8a0553d45 110
TaylorGy 0:cbe8a0553d45 111 /**
TaylorGy 0:cbe8a0553d45 112 * @brief send data to OneNet Cloud
TaylorGy 0:cbe8a0553d45 113 **/
TaylorGy 0:cbe8a0553d45 114 void sendData(char* dataName, float data)
TaylorGy 0:cbe8a0553d45 115 {
TaylorGy 0:cbe8a0553d45 116 char text[25] = {0};
TaylorGy 0:cbe8a0553d45 117 bool ret;
TaylorGy 0:cbe8a0553d45 118 memset(send_buf,0,MAX_SEND_BUF_LEN); //clean send_buf to ensure no remained buffer
TaylorGy 0:cbe8a0553d45 119
TaylorGy 0:cbe8a0553d45 120 strcat(send_buf, ",;");
TaylorGy 0:cbe8a0553d45 121 strcat(send_buf, dataName);
TaylorGy 0:cbe8a0553d45 122 strcat(send_buf, ",");
TaylorGy 0:cbe8a0553d45 123 sprintf(text,"%8.3f",data);
TaylorGy 0:cbe8a0553d45 124 strcat(send_buf, text);
TaylorGy 0:cbe8a0553d45 125 strcat(send_buf, ";");
TaylorGy 0:cbe8a0553d45 126
TaylorGy 0:cbe8a0553d45 127 pc.printf("now sending NORMAL data to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 128 send_pack = PacketSavedataSimpleString(DEVICEID, send_buf); //pack data into EDP Package
TaylorGy 0:cbe8a0553d45 129 ret = wifi.send(0,(const char*)send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud
TaylorGy 0:cbe8a0553d45 130 if(ret != true) { //check whether send successfully
TaylorGy 0:cbe8a0553d45 131 pc.printf("failed to send normal data\r\n");
TaylorGy 0:cbe8a0553d45 132 ret = true;
TaylorGy 0:cbe8a0553d45 133 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 134 } else {
TaylorGy 0:cbe8a0553d45 135 pc.printf("normal data sended\r\n");
TaylorGy 0:cbe8a0553d45 136 }
TaylorGy 0:cbe8a0553d45 137 wait_ms(500);
TaylorGy 0:cbe8a0553d45 138 }
TaylorGy 0:cbe8a0553d45 139
TaylorGy 0:cbe8a0553d45 140 /**
TaylorGy 0:cbe8a0553d45 141 * @brief make the device link to OneNet Cloud
TaylorGy 0:cbe8a0553d45 142 **/
TaylorGy 0:cbe8a0553d45 143 void devLink(const char* devid, const char* auth_key)
TaylorGy 0:cbe8a0553d45 144 {
TaylorGy 0:cbe8a0553d45 145 bool ret;
TaylorGy 0:cbe8a0553d45 146 pc.printf("now linking to OneNet...\r\n");
TaylorGy 0:cbe8a0553d45 147 send_pack = PacketConnect1(devid,auth_key); //pack Device authentication information into EDP package
TaylorGy 0:cbe8a0553d45 148 wait_ms(200);
TaylorGy 0:cbe8a0553d45 149 ret = wifi.send(0,(const char*)send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud
TaylorGy 0:cbe8a0553d45 150 wait_ms(500);
TaylorGy 0:cbe8a0553d45 151 if(ret != true) { //check whether send successfully
TaylorGy 0:cbe8a0553d45 152 pc.printf("failed to link to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 153 ret = true;
TaylorGy 0:cbe8a0553d45 154 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 155 } else {
TaylorGy 0:cbe8a0553d45 156 pc.printf("linked to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 157 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 158 }
TaylorGy 0:cbe8a0553d45 159 }