WIZnet / Mbed 2 deprecated WizFi310_Legacy_Ubidots

Dependencies:   WizFi310Interface_Legacy mbed

Fork of WizFi310_Ubidots by WIZnet

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 <stdio.h>
00018 #include "mbed.h"
00019 #include "WizFi310Interface.h"
00020 
00021 #define SECURE WizFi310::SEC_AUTO
00022 #define SSID        "SSID"
00023 #define PASS        "PASS"
00024 
00025 #define VARID_LUX   "Ubidots Variable ID"
00026 #define TOKEN       "Ubidots Token value"
00027 
00028 #if defined(TARGET_WIZwiki_W7500)
00029     WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200);
00030     Serial pc(USBTX, USBRX);
00031     AnalogIn myLux(PC_15);
00032 #endif
00033 
00034 
00035 int main()
00036 {
00037     int errConnect;
00038     char str[50] = "";
00039     char http_cmd[1000] = "";
00040     char buffer[2048] = "";  
00041     
00042     pc.baud(115200);
00043     printf("WizFi310 NetworkSocketAPI TCP Client Ubidots Example\r\n");
00044             
00045     wizfi310.init();
00046     if( wizfi310.connect(SECURE, SSID, PASS) )    return -1;
00047     
00048     const char *ip = wizfi310.getIPAddress();
00049     const char *mac = wizfi310.getMACAddress();
00050     //printf("IP address is: %s\r\n", ip ? ip : "No IP");
00051     //printf("MAC address is: %s\r\n", mac ? mac : "No MAC");
00052 
00053     TCPSocketConnection socket;
00054     socket.set_blocking(1000);   // Set Block Mode.
00055     errConnect = socket.connect("things.ubidots.com", 80);
00056     
00057     while (true) {
00058         if(errConnect!=0) {
00059             printf("\r\ncould not connect to socket : error = %d\r\n", errConnect);
00060             errConnect = socket.connect("things.ubidots.com", 80);
00061         } else {
00062             printf("socket connected\r\n");
00063             break;
00064         }
00065     }
00066             
00067     sprintf((char *)str, "{\"value\": %d}", (int)(myLux.read()*10000));
00068     printf("%s\r\n", str);
00069     int len = strlen((char *)str);
00070     
00071     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",
00072     VARID_LUX,len, TOKEN, str);
00073 
00074     socket.send_all((char *)http_cmd, strlen((char *)http_cmd));
00075     
00076     socket.receive(buffer, strlen((char *)buffer));
00077     printf("%d\r\n",strlen((char *)buffer));
00078     wait(5);
00079     
00080     socket.close();
00081     wizfi310.disconnect();
00082     
00083     printf("Done\r\n");
00084 }
00085