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

no such method: tip