Ubidots example for WizFi310

Dependencies:   NetworkSocketAPI WizFi310Interface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 #include "TCPSocket.h"
00018 #include "mbed.h"
00019 #include "WizFi310Interface.h"
00020 
00021 #define AP_SSID     "SSID"
00022 #define AP_PASSWORD "PASS"
00023 #define AP_SECURITY NSAPI_SECURITY_WPA2
00024 
00025 #define VARID_LUX   "Ubidots Variable ID"
00026 #define TOKEN       "Ubidots Token value"
00027 
00028 #if defined(TARGET_WIZWIKI_W7500)
00029     WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200);
00030     Serial pc(USBTX,USBRX);
00031     AnalogIn myLux(PC_15);
00032 #endif
00033 
00034 int main()
00035 {
00036     int errConnect;
00037     char str[50] = "";
00038     char http_cmd[1000] = "";
00039     char buffer[2048] = "";
00040     
00041     pc.baud(115200);
00042     printf("WizFi310 NetworkSocketAPI TCP Client Ubidots Example\r\n");
00043             
00044     wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY);
00045     
00046     const char *ip = wifi.get_ip_address();
00047     const char *mac = wifi.get_mac_address();
00048     printf("IP address is: %s\r\n", ip ? ip : "No IP");
00049     printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
00050 
00051     SocketAddress addr(&wifi, "things.ubidots.com", 80);
00052     printf("things.ubidots.com resolved to: %s\r\n", addr.get_ip_address());
00053 
00054     TCPSocket socket(&wifi);
00055     socket.set_timeout(1000);   // Set Block Mode.
00056     errConnect = socket.connect("things.ubidots.com", 80);
00057     
00058     while (true) {
00059         if(errConnect!=0) {
00060             printf("\r\ncould not connect to socket : error = %d\r\n", errConnect);
00061             errConnect = socket.connect("things.ubidots.com", 80);
00062         } else {
00063             printf("socket connected\r\n");
00064             break;
00065         }
00066     }
00067             
00068     sprintf((char *)str, "{\"value\": %d}", (int)(myLux.read()*10000));
00069     printf("%s\r\n", str);
00070     int len = strlen((char *)str);
00071     
00072     sprintf((char *)http_cmd,"POST /api/v1.6/variables/%s/values HTTP/1.1\r\nContent-Type: application/json\r\nContent-Length: %d\r\nX-Auth-Token: %s\r\nHost: things.ubidots.com\r\n\r\n%s\r\n\r\n",
00073     VARID_LUX,len, TOKEN, str);
00074     //printf("%s\n", http_cmd);
00075 
00076     socket.send((char *)http_cmd, sizeof((char *)http_cmd));
00077     
00078     socket.recv(buffer, sizeof(buffer));
00079     printf("%s",buffer);
00080     wait(5);
00081     
00082     socket.close();
00083     wifi.disconnect();
00084     
00085     printf("Done\r\n");
00086 
00087 }
00088