Skip to main content
All CollectionsConnect your Devices
Connect an Arduino UNO + WIZnet WizFi310 to Ubidots over HTTP
Connect an Arduino UNO + WIZnet WizFi310 to Ubidots over HTTP

Learn to setup and connect the WizFi310 shield with Ubidots IoT Application Development Platform over HTTP.

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

The WizFi310 Shield is a built-in WizFi310 WiFi module of WIZnet hardware, and comes setup with Pin compatible with Arduino. For most environment monitoring Apps, this hardware is ideal as has comes with temperature, humidity, and CDS sensors built right in.

For additional details to the Specs of the WizFi310, check out the WizWiki official documentaton.

In the following tutorial, Ubidots will demonstrate how to setup and program the WizFi310 using the Arduino IDE to display your data in efficiently using the Ubidots IoT Application Development Platfrom.

Requirements

Step-by-Step 

  1. Hardware Setup

  2. Setting up the Arduino IDE

  3. Sending (POST) Data to Ubidots 

  4. Summary

1. Hardware Setup 

The WizFi310 Shield will be initialized by AT commands sent through Serial communication. It's important to mention that the WizFi310 Shield is Arduino Pinout compatible, but for the purpose of this tutorial we will communicate with the Arduino MEGA via serial and therefore unable to debug any messages because if we try, the connection will be disrupted. We manually wire the shield and Arduino to take advantage of TX1 and RX1 PINs, if you attempt to block the boards together these PINs will not align and we will not be able to see our data response during the provisioning process. 

Please follow the steps below carefully to ensure the right functionality between the WizFi310 Shield and the Arduino:

NOTE: We decided to used the Arduino MEGA because it lets us visualize any debug message. Please chose your hardware based on the environment and computational requirements of your integration. 

1. First, make sure the DIP Switch Configurations are located in the right place: 

  • SW1: 1. OFF - 2. OFF

  • SW5: 1. ON - 2. OFF - 3. OFF 

  • SW6: 1. OFF - 2. OFF

  • UART Selector Jumper: D0 - TXD  |  D1 - RXD

Reference the image below for a visual review: 

2. Now with the correct configurations, use male-female wires to establish the connections between the shield and the Arduino. Please, reference the table below to establish the proper wiring of the PINs:

3. With everything configured, power ON the WizFi shield and the Arduino.

2. Setting up the Arduino IDE 

1. Download the latest version of the Arduino IDE if you do not already have it.

2. Go to the library repository to download the WizFi310 Arduino library. To download the library clicking the green button called "Clone or download" and select "Download ZIP".  

3. Now back in the Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library.

4. Select the .ZIP file of WizFi310_arduino_library and then “Accept” or “Choose”. If you are not familiar with the workings and operations of board and library management in the Arduino IDE, be sure to check out this Ubidots User Guide. 

5. Paste the sample code below into the Arduino IDE. Once you have pasted the code, be sure to assign the required parameters: Wi-Fi name and password, plus your individual, unique Ubidots TOKEN. 


#include <Arduino.h>
#include "WizFi310.h"

namespace {
  char ssid[] = "xxxxxxxxxx"; // Assign your Network SSID
  char pass[] = "xxxxxxxxxx";  // Assign your Network password
  char SERVER[] = "industrial.api.ubidots.com"; // Ubidots Server
  const char * TOKEN = "xxxxxxxxxx"; // Assign your Ubidots TOKEN
  const char * DEVICE_LABEL = "wiznet-shield"; // Ubidots Device Label
  const char * USER_AGENT = "wiznet";
  const char * VERSION = "1.0";
  const int PORT = 80;
  int  status = WL_IDLE_STATUS; // the Wifi radio's status  
}

WiFiClient client;

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
 
  delay(6000);
  Serial.print("Starting...");
  Serial.println("WiFi initiallize");
  WiFi.init(&Serial1);
 
  if (WiFi.status() == 0) {
    Serial.println("Failed to configure WiFi");
    // don't continue
    while(true)
    ;
  }
  //attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("[WiFi] Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    //Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
    Serial.println("Connected!");
  }
 
  delay(1000);
}

void loop() {
  int sensor_value = analogRead(A0);
  Serial.println("Sending sensor value...");
  sendValue("light", sensor_value);
  delay(10000);
}

void sendValue(const char * variable_label, int value) {
 
  /* Build the body to be POST */
  char* body = (char *) malloc(sizeof(char) * 100);
  sprintf(body, "{\"%s\":%d}", variable_label, value);

  /* Builds the HTTP request to be POST */
  char* data = (char *) malloc(sizeof(char) * 300);
  sprintf(data, "POST /api/v1.6/devices/%s", DEVICE_LABEL);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: industrial.api.ubidots.com\r\n", data);
  sprintf(data, "%sUser-Agent: %s/%s\r\n", data, USER_AGENT, VERSION);
  sprintf(data, "%sX-Auth-Token: %s\r\n", data, TOKEN);
  sprintf(data, "%sConnection: close\r\n", data);
  sprintf(data, "%sContent-Type: application/json\r\n", data);
  sprintf(data, "%sContent-Length: %d\r\n\r\n", data, dataLen(body));
  sprintf(data, "%s%s\r\n\r\n", data, body);

  if (client.connect(SERVER, PORT)) {
    client.print(data);
  } else {
    Serial.println("connection failed");
  }
 
  while (!client.available());
    //Serial1.println("Reading..");
  while (client.available()) {
    char c = client.read();
    Serial.print(c); // Response Monitoring
  }
  client.flush();
  client.stop();
  free(body);
  free(data);
}

int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

6. Next, select Arduino MEGA 2560 from Tools –> Board menu.

7. Then Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon press it to verify your code. 

8. Upload the code into your Arduino MEGA. To do this, choose the "right-arrow" icon beside the check mark icon from point 7 above. 

9. To verify the connectivity of the device, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of our Arduino IDE. As you will note below, the request is sent to Ubidots' server and responded with 200 OK - meaning a valid request was sent. 

3. Data visualization 

1. Enter your Ubidots account to confirm the data has been received. In the device section, locate the device called "wiznet-shield" and visualize your data. 

2. The variable received is "light" which is the reading of the CDS sensor:

4. Summary

With this simple tutorial we are able to POST data to Ubidots with the ease of the Arduino IDE and a WizFi310. Now you can modify the example code provided to include temperature and humidity readings too and launch your IoT solution with ease and WIZnet. :) 

Other readers have also found useful...

Did this answer your question?