The WEMOS D1 Mini ESP8266 is a development board similar to Arduino and developed especially for cost effective Internet of Things applications and solutions. Also compatible with Expressif's ESP32, the WEMOS series is perfect for educational and hobby IoT projects. With a 32 bit architecture (more powerful than the Arduino Due) and WiFi connectivity, you can choose between the Arduino and Lua languages for your application's development.

In this tutorial we will demonstrate how to setup and program the WEMOS D1 mini  with the Arduino IDE to display your data with Ubidots.

Requirements  

Step-by-Step

  1. Setting up Arduino IDE
  2. Sending (POST) Data to Ubidots
  3. Summary

1. Setting up Arduino IDE

1. To begin working with the Wemos D1 mini ESP8266 platform in the Arduino IDE, you will need to install the ESP8266 board using the preconfigured Arduino Board Manager. If you are not familiar with adding a board with the Arduino IDE, refer to this article for additional guidance.


2. With the ESP8266 platform installed, select the ESP8266 device you are working with. In the case, we are working with a “WEMOS D1 min”. To select your board from the Arduino IDE, select Tools > Board WEMOS D1 min”.

2. Sending (POST) Data to Ubidots

With the following sample code you will be able to post readings taken from the ANALOG pin A0 of the WEMOS D1 mini to your Ubidots account.

1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN, SSID (WiFi Name) and the Password of the available network. 

/********************************
 * Libraries included
 *******************************/
#include <ESP8266WiFi.h>

/********************************
 * Constants and objects
 *******************************/

const char* SSID_NAME = "....."; // Put here your SSID name
const char* SSID_PASS = "....."; // Put here your password
const char* TOKEN = "....."; // Put here your TOKEN
const char* DEVICE_LABEL = "wemos-d1"; // Your device label
const char* VARIABLE_LABEL = "temperature"; // Your variable label
const char* USER_AGENT = "ESP8266";
const char* VERSION = "1.0";
const char* HTTPSERVER = "industrial.api.ubidots.com";
int HTTPPORT = 80;

WiFiClient clientUbi;

/********************************
 * Auxiliar Functions
 *******************************/
/**
 * Gets the length of the body
 * @arg variable the body of type char
 * @return dataLen the length of the variable
 */
int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable[i] != '\0') {
      dataLen++;
    } else {
      break;
    }
  }
  return dataLen;
}

/********************************
 * Main Functions
 *******************************/

void setup() {
  Serial.begin(115200);
 
  /* Connects to AP */
  WiFi.begin(SSID_NAME, SSID_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    }

  WiFi.setAutoReconnect(true);
  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

}

void loop() {

  char* body = (char *) malloc(sizeof(char) * 150);
  char* data = (char *) malloc(sizeof(char) * 300);
  /* Space to store values to send */
  char str_val[10];

  /* Read the sensor from the analog input*/
  float sensor_value = analogRead(A0);

  /*---- Transforms the values of the sensors to char type -----*/
  /* 4 is mininum width, 2 is precision; float value is copied onto str_val*/
  dtostrf(sensor_value, 4, 2, str_val);

  /* Builds the body to be send into the request*/
  sprintf(body, "{\"%s\":%s}", VARIABLE_LABEL, str_val);
 
  /* Builds the HTTP request to be POST */
  sprintf(data, "POST /api/v1.6/devices/%s", DEVICE_LABEL);
  sprintf(data, "%s HTTP/1.1\r\n", data);
  sprintf(data, "%sHost: things.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);

  /* Initial connection */
  clientUbi.connect(HTTPSERVER, HTTPPORT);

  /* Verify the client connection */
  if (clientUbi.connect(HTTPSERVER, HTTPPORT)) {
        Serial.println(F("Posting your variables: "));
        Serial.println(data);
        /* Send the HTTP Request */
        clientUbi.print(data);
  }

  /* While the client is available read the response of the server */
  while (clientUbi.available()) {
        char c = clientUbi.read();
        Serial.write(c);
  }
   /* Free memory */
  free(data);
  free(body);
  /* Stop the client */
  clientUbi.stop();
  /* Five second delay */
  delay(5000);
}

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

3. Upload the code into your “WEMOS D1 mini”. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen, try unplugging the WEMOS D1 mini device and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code 115200.

5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, located the device called "wemos-d1".

3. Summary

With this simple tutorial you are able to POST data to Ubidots with the ease of the Arduino IDE and an WEMOS D1 mini

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Hacking! :) 

Other readers have also found useful...

  

Did this answer your question?