wifi example for ASC platform, sending data to OneNet Cloud

Dependents:   8-0_OneNet_IoT_demo

Committer:
TaylorGy
Date:
Wed Mar 29 03:08:46 2017 +0000
Revision:
0:cbe8a0553d45
wifi example by using Grove_UART_wifi module(based on ESP8266), send data in EDP form

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);
TaylorGy 0:cbe8a0553d45 12 Serial pc(SERIAL_TX, SERIAL_RX);
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;
TaylorGy 0:cbe8a0553d45 23 pc.printf("starting......\r\n");
TaylorGy 0:cbe8a0553d45 24 wifi.reset();
TaylorGy 0:cbe8a0553d45 25 wait_ms(500);
TaylorGy 0:cbe8a0553d45 26 ret = wifi.startup(1); //start as client mode
TaylorGy 0:cbe8a0553d45 27 wait_ms(200);
TaylorGy 0:cbe8a0553d45 28 if(ret != true) {
TaylorGy 0:cbe8a0553d45 29 pc.printf("startup failed\r\n");
TaylorGy 0:cbe8a0553d45 30 ret = true;
TaylorGy 0:cbe8a0553d45 31 } else {
TaylorGy 0:cbe8a0553d45 32 pc.printf("set as client mode\r\n");
TaylorGy 0:cbe8a0553d45 33 }
TaylorGy 0:cbe8a0553d45 34 wifi.connect(APNAME,APPASSWD); //conenet to a existed wifi ap
TaylorGy 0:cbe8a0553d45 35 wait_ms(200);
TaylorGy 0:cbe8a0553d45 36 ret = wifi.isConnected(); //check whether connect successed
TaylorGy 0:cbe8a0553d45 37 if(ret != true) {
TaylorGy 0:cbe8a0553d45 38 pc.printf("connect failed\r\n");
TaylorGy 0:cbe8a0553d45 39 ret = true;
TaylorGy 0:cbe8a0553d45 40 } else {
TaylorGy 0:cbe8a0553d45 41 pc.printf("now connected!\r\n");
TaylorGy 0:cbe8a0553d45 42 }
TaylorGy 0:cbe8a0553d45 43 pc.printf("the ip address is : %s\r\n",wifi.getIPAddress()); //send ip address to pc
TaylorGy 0:cbe8a0553d45 44 ret = wifi.open("TCP", TCPNUM, TCPADD, TCPPORT); //connte to a TCP socket
TaylorGy 0:cbe8a0553d45 45 wait_ms(200);
TaylorGy 0:cbe8a0553d45 46 checkSocketStatus();
TaylorGy 0:cbe8a0553d45 47 }
TaylorGy 0:cbe8a0553d45 48
TaylorGy 0:cbe8a0553d45 49 /**
TaylorGy 0:cbe8a0553d45 50 * @brief checek socket status by calling a rebuilded function ESP8266::isOpen()
TaylorGy 0:cbe8a0553d45 51 **/
TaylorGy 0:cbe8a0553d45 52 int checkSocketStatus(void)
TaylorGy 0:cbe8a0553d45 53 {
TaylorGy 0:cbe8a0553d45 54 int res;
TaylorGy 0:cbe8a0553d45 55 res = wifi.isOpen();
TaylorGy 0:cbe8a0553d45 56 switch(res) {
TaylorGy 0:cbe8a0553d45 57 case 0: {
TaylorGy 0:cbe8a0553d45 58 pc.printf("socket conneceted successfully\r\n");
TaylorGy 0:cbe8a0553d45 59 return res;
TaylorGy 0:cbe8a0553d45 60 }
TaylorGy 0:cbe8a0553d45 61 case -1: {
TaylorGy 0:cbe8a0553d45 62 pc.printf("unknown error\r\n");
TaylorGy 0:cbe8a0553d45 63 return res;
TaylorGy 0:cbe8a0553d45 64 }
TaylorGy 0:cbe8a0553d45 65 case -2: {
TaylorGy 0:cbe8a0553d45 66 pc.printf("connected to ap, get an ip address\r\n");
TaylorGy 0:cbe8a0553d45 67 return res;
TaylorGy 0:cbe8a0553d45 68 }
TaylorGy 0:cbe8a0553d45 69 case -4: {
TaylorGy 0:cbe8a0553d45 70 pc.printf("lost connection\r\n");
TaylorGy 0:cbe8a0553d45 71 return res;
TaylorGy 0:cbe8a0553d45 72 }
TaylorGy 0:cbe8a0553d45 73 case -5: {
TaylorGy 0:cbe8a0553d45 74 pc.printf("unconnected to ap\r\n");
TaylorGy 0:cbe8a0553d45 75 return res;
TaylorGy 0:cbe8a0553d45 76 }
TaylorGy 0:cbe8a0553d45 77 default: {
TaylorGy 0:cbe8a0553d45 78 pc.printf("unknown error:%d\r\n",res);
TaylorGy 0:cbe8a0553d45 79 break;
TaylorGy 0:cbe8a0553d45 80 }
TaylorGy 0:cbe8a0553d45 81 }
TaylorGy 0:cbe8a0553d45 82 return;
TaylorGy 0:cbe8a0553d45 83 }
TaylorGy 0:cbe8a0553d45 84
TaylorGy 0:cbe8a0553d45 85
TaylorGy 0:cbe8a0553d45 86 /**
TaylorGy 0:cbe8a0553d45 87 * @brief send cJSON type data
TaylorGy 0:cbe8a0553d45 88 **/
TaylorGy 0:cbe8a0553d45 89 void sendJsonDat(char* dataName, float data)
TaylorGy 0:cbe8a0553d45 90 {
TaylorGy 0:cbe8a0553d45 91 bool ret;
TaylorGy 0:cbe8a0553d45 92 cJSON *json_data = cJSON_CreateObject(); //create a new json data
TaylorGy 0:cbe8a0553d45 93 cJSON_AddNumberToObject(json_data, dataName, data); //pack data into json package
TaylorGy 0:cbe8a0553d45 94
TaylorGy 0:cbe8a0553d45 95 send_pack = PacketSavedataJson(DEVICEID, json_data, 3); //pack send data into EDP package
TaylorGy 0:cbe8a0553d45 96 pc.printf("now sending JSON from data to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 97 ret = wifi.send(0,(const char*)send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud
TaylorGy 0:cbe8a0553d45 98 if(ret != true) { //check whether send successfully
TaylorGy 0:cbe8a0553d45 99 pc.printf("failed to send Json data\r\n");
TaylorGy 0:cbe8a0553d45 100 ret = true;
TaylorGy 0:cbe8a0553d45 101 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 102 } else {
TaylorGy 0:cbe8a0553d45 103 pc.printf("Json data sended\r\n");
TaylorGy 0:cbe8a0553d45 104 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 105 }
TaylorGy 0:cbe8a0553d45 106 cJSON_Delete(json_data); //delete json_data, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 107 wait_ms(500);
TaylorGy 0:cbe8a0553d45 108 }
TaylorGy 0:cbe8a0553d45 109
TaylorGy 0:cbe8a0553d45 110 /**
TaylorGy 0:cbe8a0553d45 111 * @brief send data to OneNet Cloud
TaylorGy 0:cbe8a0553d45 112 **/
TaylorGy 0:cbe8a0553d45 113 void sendData(char* dataName, float data)
TaylorGy 0:cbe8a0553d45 114 {
TaylorGy 0:cbe8a0553d45 115 char text[25] = {0};
TaylorGy 0:cbe8a0553d45 116 bool ret;
TaylorGy 0:cbe8a0553d45 117 memset(send_buf,0,MAX_SEND_BUF_LEN); //clean send_buf to ensure no remained buffer
TaylorGy 0:cbe8a0553d45 118
TaylorGy 0:cbe8a0553d45 119 strcat(send_buf, ",;");
TaylorGy 0:cbe8a0553d45 120 strcat(send_buf, dataName);
TaylorGy 0:cbe8a0553d45 121 strcat(send_buf, ",");
TaylorGy 0:cbe8a0553d45 122 sprintf(text,"%8.3f",data);
TaylorGy 0:cbe8a0553d45 123 strcat(send_buf, text);
TaylorGy 0:cbe8a0553d45 124 strcat(send_buf, ";");
TaylorGy 0:cbe8a0553d45 125
TaylorGy 0:cbe8a0553d45 126 pc.printf("now sending NORMAL data to OneNet\r\n");
TaylorGy 0:cbe8a0553d45 127 send_pack = PacketSavedataSimpleString(DEVICEID, send_buf); //pack data into EDP Package
TaylorGy 0:cbe8a0553d45 128 ret = wifi.send(0,(const char*)send_pack->_data,send_pack->_write_pos); //send packge to OneNet Cloud
TaylorGy 0:cbe8a0553d45 129 if(ret != true) { //check whether send successfully
TaylorGy 0:cbe8a0553d45 130 pc.printf("failed to send normal data\r\n");
TaylorGy 0:cbe8a0553d45 131 ret = true;
TaylorGy 0:cbe8a0553d45 132 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
TaylorGy 0:cbe8a0553d45 133 } else {
TaylorGy 0:cbe8a0553d45 134 pc.printf("normal data sended\r\n");
TaylorGy 0:cbe8a0553d45 135 DeleteBuffer(&send_pack); //delete send_packge, unless may cause memory leak
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 }