Ubidots example for WizFi310

Dependencies:   NetworkSocketAPI WizFi310Interface mbed

You are viewing an older revision! See the latest version

Homepage

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 + 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 "TCPSocket.h"
#include "mbed.h"
#include "WizFi310Interface.h"

#define AP_SSID     "SSID"
#define AP_PASSWORD "PASS"
#define AP_SECURITY NSAPI_SECURITY_WPA2

#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);
    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");
            
    wifi.connect(AP_SSID, AP_PASSWORD, AP_SECURITY);
    
    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");

    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.
    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);
    //printf("%s\n", http_cmd);

    socket.send((char *)http_cmd, sizeof((char *)http_cmd));
    
    socket.recv(buffer, sizeof(buffer));
    printf("%s",buffer);
    wait(5);
    
    socket.close();
    wifi.disconnect();
    
    printf("Done\r\n");

}


Caution

This example requires "Variable ID" and "TOKEN value" that can be obtained by signning up to the Ubidots. //


All wikipages