Requirements
Seeed Studio’s SenseCAP K1100 Prototype Kit
An active Ubidots account
A stable Wi-Fi network
Arduino IDE installed
1. Updating SenseCAP K1100 firmware
Go to Seeed Studio’s Github page and download the file “SenseCraft-v0.2.uf2” or the most recent firmware. Keep track of where you downloaded this file for future use.
Connect the Wio Terminal to the PC using the USB-C cable.
Make the Wio Terminal go into “Bootloader Mode” by following the next procedure:
Pro Tip: You can confirm that the Wio Terminal is in Bootloader mode when the blue LED starts to breathe in a way that is different than just blinking.
Open your file explorer and check for a device labeled “Arduino”. If it is not listed, then the device is not in “Bootloader Mode”. Repeat the previous step.
Using your file explorer, copy and paste the “SenseCraft-v0.2.uf2” file to the “Arduino” device.
When the copy process is finished, the Wio Terminal will reset automatically (wait for it).
2. Setting Up the Arduino IDE
Launch the Arduino IDE.
Click on “File” → “Preferences” and copy the following URL to the “Additional Boards Manager URLs” field:
https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
Click on “Tools” → “Board” → “Board manager”, head to the search bar, and type “Wio Terminal”. Install the latest version of “Seeed SAMD Boards”.
Wait for the download to finish and then click the “Close” button.
Select your board by going to “Tools” → “Board” → “Seeed SAMD (32-bits ARM Cortex-M0+ and Cortex-M4) Boards” → “Seeeduino Wio Terminal”.
Select the port to which your board is connected. Go to “Tools” → “Port” and select your device from the available ones.
Pro Tip: On Windows OS it’s most likely that the devices be listed as “COM1”, “COM2”, etc., while on Linux-based OS the devices will be listed like “/dev/ttyACM0”, “/dev/ttyACM1”, etc.
Head to the following GitHub repositories and download the .zip files:
Go to the Arduino IDE → “Sketch” → “Include Library” → “Add .ZIP Library...”
Add the libraries you just downloaded from GitHub.
Go to “Sketch” → “Include Library” → “Manage Libraries...”, search and install the following libraries:
“Seeed Arduino rcpWIFi”
“Seeed Arduino rcpUnified”
“Seeed Arduino mbedtls”
“Seeed Arduino FS”
“Seeed Arduino SFUD”
“PubSubClient”
3. Preparing the Connection Credentials
Head over to your Ubidots account and copy your Ubidots Token for later use. In this link you can learn how to get your Ubidots Token.
Go to this website and generate a random string according to the following settings. This is going to be the MQTT client name in further steps.
Keep both of these values at hand because you will need them for the next section.
4. Uploading the Code to Wio Terminal
Go to the Arduino IDE, delete all the code, and paste the following, replacing each “#define” field accordingly:
WIFISSID: Your Wi-Fi SSID
PASSWORD: Your Wi-Fi password
TOKEN: Your Ubidots Token
VARIABLE_LABEL1..., VARIABLE_LABEL5: You can leave the default options, however, you can change them to match your needs
DEVICE_LABEL: You can leave the default option. In that case, your device will be displayed as “wio-terminal” on the Ubidots platform, however, you can change it to match your needs
MQTT_CLIENT_NAME: the random string generated in the previous section
#include <PubSubClient.h>
#include <rpcWiFi.h>
#include <TFT_eSPI.h>
#include"LIS3DHTR.h"
LIS3DHTR<TwoWire> lis;
//Required Information
#define WIFISSID "<YOUR-WIFISSD>" // Put your WifiSSID here
#define PASSWORD "<YOUR-WIFI-PASSWORD" // Put your wifi password here
#define TOKEN "<YOUR-UBIDOTS-TOKEN>" // Put your Ubidots' TOKEN
#define VARIABLE_LABEL1 "light" // Assign the variable label
#define VARIABLE_LABEL2 "IMUx"
#define VARIABLE_LABEL3 "IMUy"
#define VARIABLE_LABEL4 "IMUz"
#define VARIABLE_LABEL5 "sound"
#define DEVICE_LABEL "wio-terminal" // Assign the device label
#define MQTT_CLIENT_NAME "r6y1ax7mq8875jf" // MQTT client Name
const long interval = 100;
unsigned long previousMillis = 0;
TFT_eSPI tft;
char mqttBroker[] = "industrial.api.ubidots.com";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
//sensor values
static int lightValue = 0;
static float imuxValue = 0;
static float imuyValue = 0;
static float imuzValue = 0;
static int soundValue = 0;
// Space to store values to send
static char str_light[6];
static char str_imux[6];
static char str_imuy[6];
static char str_imuz[6];
static char str_sound[6];
char payload[700];
char topic[150];
void callback(char* topic, byte* payload, unsigned int length){
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) {
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
//Reading built-in sensor values
void read_builtin()
{
lightValue = analogRead(WIO_LIGHT);
Serial.print("Light = ");
Serial.println(lightValue);
imuxValue = lis.getAccelerationX();
Serial.print("IMU_x = ");
Serial.println(imuxValue);
imuyValue = lis.getAccelerationY();
Serial.print("IMU_y = ");
Serial.println(imuyValue);
imuzValue = lis.getAccelerationZ();
Serial.print("IMU_z = ");
Serial.println(imuzValue);
soundValue = analogRead(WIO_MIC);
Serial.print("Sound = ");
Serial.println(soundValue);
}
//Sending data to Ubidots
void send_data()
{
dtostrf(lightValue, 4, 0, str_light);
dtostrf(imuxValue, 4, 3, str_imux);
dtostrf(imuyValue, 4, 3, str_imuy);
dtostrf(imuzValue, 4, 3, str_imuz);
dtostrf(soundValue, 4, 0, str_sound);
if (!client.connected()) {
reconnect();
}
// Builds the topic
sprintf(topic, "%s", ""); // Cleans the topic content
sprintf(topic, "%s%s", "/v2.0/devices/", DEVICE_LABEL);
//Builds the payload
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label
sprintf(payload, "%s%s", payload, str_light); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label
sprintf(payload, "%s%s", payload, str_imux); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL3); // Adds the variable label
sprintf(payload, "%s%s", payload, str_imuy); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL4); // Adds the variable label
sprintf(payload, "%s%s", payload, str_imuz); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\":", VARIABLE_LABEL5); // Adds the variable label
sprintf(payload, "%s%s", payload, str_sound); // Adds the value
sprintf(payload, "%s}", payload); // Closes the dictionary brackets
client.publish(topic, payload);
delay(500);
client.loop();
}
void setup() {
Serial.begin(115200);
lis.begin(Wire1);
pinMode(WIO_MIC, INPUT);
pinMode(WIO_LIGHT, INPUT);
tft.begin();
tft.setRotation(3);
tft.setTextSize(2);
tft.fillScreen(TFT_BLACK);
lis.setOutputDataRate(LIS3DHTR_DATARATE_25HZ); //Data output rate
lis.setFullScaleRange(LIS3DHTR_RANGE_2G);
// while(!Serial);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
tft.drawString("Connecting to WiFi...",20,120);
WiFi.begin(WIFISSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
WiFi.begin(WIFISSID, PASSWORD);
}
tft.fillScreen(TFT_BLACK);
tft.drawString("Connected to the WiFi",20,120);
delay(1000);
client.setServer(mqttBroker, 1883);
client.setCallback(callback);
}
void loop() {
read_builtin(); //Reading buile-in sensor values
send_data(); //Sending data to Ubidots
delay(5000);
}
Upload the code to the “Wio Terminal” device by hitting the “Upload” button on the Arduino IDE:
5. Visualize the Data on Ubidots
Go to your Ubidots account → “Devices” and you’ll be able to see a new device labeled “wio-terminal”.