Ubidots example for WizFi310

Dependencies:   WizFi310Interface_Legacy mbed

Fork of WizFi310_Ubidots by WIZnet

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

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

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. //

Files at this revision

API Documentation at this revision

Comitter:
cliff1
Date:
Wed Apr 19 01:51:49 2017 +0000
Parent:
0:1a8e6ca12976
Child:
2:30ac0a804add
Commit message:
WizFi310 Ubidots Legacy version

Changed in this revision

NetworkSocketAPI.lib Show diff for this revision Revisions of this file
WizFi310Interface.lib Show diff for this revision Revisions of this file
WizFi310Interface_Legacy.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/NetworkSocketAPI.lib	Wed Apr 12 01:31:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/teams/NetworkSocketAPI/code/NetworkSocketAPI/#ea3a618e0818
--- a/WizFi310Interface.lib	Wed Apr 12 01:31:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/WIZnet/code/WizFi310Interface/#04c8d61984a3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WizFi310Interface_Legacy.lib	Wed Apr 19 01:51:49 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WizFi310Interface_Legacy/#774ff1e8b26b
--- a/main.cpp	Wed Apr 12 01:31:32 2017 +0000
+++ b/main.cpp	Wed Apr 19 01:51:49 2017 +0000
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
  
-#include "TCPSocket.h"
+//#include "TCPSocket.h"
 #include "mbed.h"
 #include "WizFi310Interface.h"
 
--- a/mbed.bld	Wed Apr 12 01:31:32 2017 +0000
+++ b/mbed.bld	Wed Apr 19 01:51:49 2017 +0000
@@ -1,1 +1,1 @@
-https://mbed.org/users/mbed_official/code/mbed/builds/856d2700e60b
\ No newline at end of file
+https://mbed.org/users/mbed_official/code/mbed/builds/97feb9bacc10
\ No newline at end of file