Ubidots example for WizFi310
Dependencies: WizFi310Interface_Legacy mbed
Fork of WizFi310_Ubidots by
Prerequisite
This example is for sending data to Ubidots(Server).
WIZwiki-W7500 and WizFi310 Shield act as TCP client mode.
To implement this function, you need a Platform board and Wi-Fi board. Below are what we used.
- WIZwiki-W7500 from WIZnet (Platform board)
- WizFi310 from WIZnet (Wi-Fi board)
- ETC: PGM5537(CDS sensor) + 10k resistor
Hardware Configuration
WIZwiki-W7500 Pin map
- D0 is for RXD, D1 is for TXD
- D6 is for CTS, D7 is for RTS
- D9 is for RESET
WizFi310 Pin map
- J1 is for RXD, J3 is for TXD
- SW6-1 is connected to D6 for RTS, SW6-2 is connected to D7 for CTS
- SW5-3 is connected to D9 for RESET
Software
main.cpp
#include <stdio.h> #include "mbed.h" #include "WizFi310Interface.h" #define SECURE WizFi310::SEC_AUTO #define SSID "SSID" #define PASS "PASS" #define VARID_LUX "Ubidots Variable ID" #define TOKEN "Ubidots Token value" #if defined(TARGET_WIZwiki_W7500) WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200); Serial pc(USBTX, USBRX); AnalogIn myLux(PC_15); #endif int main() { int errConnect; char str[50] = ""; char http_cmd[1000] = ""; char buffer[2048] = ""; pc.baud(115200); printf("WizFi310 NetworkSocketAPI TCP Client Ubidots Example\r\n"); wizfi310.init(); if( wizfi310.connect(SECURE, SSID, PASS) ) return -1; const char *ip = wizfi310.getIPAddress(); const char *mac = wizfi310.getMACAddress(); //printf("IP address is: %s\r\n", ip ? ip : "No IP"); //printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); TCPSocketConnection socket; socket.set_blocking(1000); // Set Block Mode. errConnect = socket.connect("things.ubidots.com", 80); while (true) { if(errConnect!=0) { printf("\r\ncould not connect to socket : error = %d\r\n", errConnect); errConnect = socket.connect("things.ubidots.com", 80); } else { printf("socket connected\r\n"); break; } } sprintf((char *)str, "{\"value\": %d}", (int)(myLux.read()*10000)); printf("%s\r\n", str); int len = strlen((char *)str); 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", VARID_LUX,len, TOKEN, str); socket.send_all((char *)http_cmd, strlen((char *)http_cmd)); socket.receive(buffer, strlen((char *)buffer)); printf("%d\r\n",strlen((char *)buffer)); wait(5); socket.close(); wizfi310.disconnect(); printf("Done\r\n"); }
Caution
This example requires "Variable ID" and "TOKEN value" that can be obtained by signning up to the Ubidots. //
Revision 2:30ac0a804add, committed 2017-04-20
- Comitter:
- cliff1
- Date:
- Thu Apr 20 23:23:35 2017 +0000
- Parent:
- 1:62c66f73d6f4
- Commit message:
- WizFi310 Ubidots example Legacy version
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 62c66f73d6f4 -r 30ac0a804add main.cpp --- a/main.cpp Wed Apr 19 01:51:49 2017 +0000 +++ b/main.cpp Thu Apr 20 23:23:35 2017 +0000 @@ -14,45 +14,44 @@ * limitations under the License. */ -//#include "TCPSocket.h" +#include <stdio.h> #include "mbed.h" #include "WizFi310Interface.h" -#define AP_SSID "SSID" -#define AP_PASSWORD "PASS" -#define AP_SECURITY NSAPI_SECURITY_WPA2 +#define SECURE WizFi310::SEC_AUTO +#define SSID "SSID" +#define PASS "PASS" #define VARID_LUX "Ubidots Variable ID" #define TOKEN "Ubidots Token value" -#if defined(TARGET_WIZWIKI_W7500) - WizFi310Interface wifi(D1, D0, D7, D6, D9, NC, 115200); - Serial pc(USBTX,USBRX); +#if defined(TARGET_WIZwiki_W7500) + WizFi310Interface wizfi310(D1, D0, D7, D6, D9, NC, 115200); + Serial pc(USBTX, USBRX); AnalogIn myLux(PC_15); #endif + int main() { int errConnect; char str[50] = ""; char http_cmd[1000] = ""; - char buffer[2048] = ""; + char buffer[2048] = ""; pc.baud(115200); printf("WizFi310 NetworkSocketAPI TCP Client Ubidots Example\r\n"); - wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY); + wizfi310.init(); + if( wizfi310.connect(SECURE, SSID, PASS) ) return -1; - const char *ip = wifi.get_ip_address(); - const char *mac = wifi.get_mac_address(); - printf("IP address is: %s\r\n", ip ? ip : "No IP"); - printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); + const char *ip = wizfi310.getIPAddress(); + const char *mac = wizfi310.getMACAddress(); + //printf("IP address is: %s\r\n", ip ? ip : "No IP"); + //printf("MAC address is: %s\r\n", mac ? mac : "No MAC"); - SocketAddress addr(&wifi, "things.ubidots.com", 80); - printf("things.ubidots.com resolved to: %s\r\n", addr.get_ip_address()); - - TCPSocket socket(&wifi); - socket.set_timeout(1000); // Set Block Mode. + TCPSocketConnection socket; + socket.set_blocking(1000); // Set Block Mode. errConnect = socket.connect("things.ubidots.com", 80); while (true) { @@ -71,18 +70,16 @@ 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", VARID_LUX,len, TOKEN, str); - //printf("%s\n", http_cmd); - socket.send((char *)http_cmd, sizeof((char *)http_cmd)); + socket.send_all((char *)http_cmd, strlen((char *)http_cmd)); - socket.recv(buffer, sizeof(buffer)); - printf("%s",buffer); + socket.receive(buffer, strlen((char *)buffer)); + printf("%d\r\n",strlen((char *)buffer)); wait(5); socket.close(); - wifi.disconnect(); + wizfi310.disconnect(); printf("Done\r\n"); - }