Before we get started with the Project, it is necessary to know the basic usage of a Bolt IoT module and Arduino programming. Slowly, we'll move ahead and get you started with the integration with all required instructions.
Bolt Module - A microcontroller board, built on the ESP8266-12E/12S commits the purpose to build IoT Projects easily, in only a few minutes. After the set-up, it directly connects to the Bolt Cloud account with the same email as logged in on the Bolt App.
A brief Hardware Description of the Module can be found in the Documentation
Arduino/Boltduino - The Boltduino is Bolt's take on the open-source electronics platform, Arduino. Read more about Arduino here. While they believe that we can build some really awesome projects around the Arduino UNO, making these projects IoT-ready can be quite tedious.
Bolt IoT has designed the Boltduino such that you can instantly port all Arduino projects to it, and make these projects IoT ready. They did this by re-designing the Arduino UNO R2 to have 2 key features required for IoT.
- Internet Connectivity
- OTA update
A brief Hardware Description of the Boltduino can be found in the Documentation
Install Arduino IDE on PC1. Download the software for your version of OS from - https://www.arduino.cc/en/software
2. Install the Software and Connect your Boltduino to the USB port.
3. Open the Arduino IDE and verify if the board is connected.
Go to Tools > Board > Arduino AVR Boards > Arduino UNONow, in Tools > Port > Select the COM port with (Arduino UNO) beside the name.
Now you know that the port is connected properly.
Basic Arduino Programs1.Blink the in-built LED (blink.ino)
We'll write a program to blink the in-built LED of the Arduino which is connected with pin - 13. For ease, we'll directly use the in-program variable, i.e. "LED_BUILTIN".
Code -
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Compile and Upload the Code -
Boltduino
Arduino Nano
2. Display Analog pin values on Serial Monitor
We'll write a program to print the A0, A1 pin inputs on the Serial Monitor. In our case, we don't need any sensors for testing. As the analog pins generate random values when no interface is present.
Code -
void setup() {
Serial.begin(9600);
}
void loop() {
int A0 = analogRead(A0);
int A1 = analogRead(A1);
Serial.print("Values are ");
Serial.print(A0);
Serial.print(" and ");
Serial.println(A1);
delay(1000);
}
Compile and Upload the Code. Then go to Tools > Serial Monitor (Ctrl + Shift + M)You'll see the values getting displayed on the Monitor -
Bolt IoT has provided a library 'boltiot-arduino-helper' that lets us perform UART communication with the Bolt IoT module and Bolt Cloud in very easy steps.
It includes a series of functions that are able to perform Serial Write/Read depending upon the situation.
For example, to plot data of Arduino's analog pins on the Bolt Cloud, we need to send the data to the Bolt Module, which in turn sends the data to the Cloud.
To simulate the process simultaneously along with the Bolt Cloud, the functions are really helpful. We'll look into every function and steps as mentioned in the 'boltiot-arduino-helper' documentation.
1.Install the boltiot-arduino-helper library
As mentioned in the documentation, once we click on the download link, we need to add the library to our Arduino IDE. As per instructions, on the Arduino IDE, go to Sketch > select Include Library > select Add.Zip library
Now browse to your Downloads (wherever you downloaded the library ZIP file), and select the ZIP file you just downloaded. The Library will be installed once you RESTART the Arduino IDE.
To use the library we use the below method for calling the header file, as Arduino IDE uses C language.
Now, that we have called the header file, we can use the functions that the library has provided us with.
1. beginSimilar to Serial.begin, the 'boltiot.begin()' function initializes the communication between the Arduino and Bolt module with a baudrate of 9600.
In here, we have a choice of either a Hardware Serial (pin 0 and 1) or a Software Serial (any other digital pin) on Arduino, where the pins will be used to set this communication with the Bolt IoT's RX and TX pin.
Hardware Serial -
boltiot.begin(Serial); //Connect RX (Arduino)-->TX (Bolt) | TX (Arduino)-->RX (Bolt)
Software Serial -
boltiot.begin(RX,TX); //Connect RX (Arduino)-->TX (Bolt) | TX (Arduino)-->RX (Bolt)
We'll test both the functions along with the next function.
2. processPushDataCommandThe bolt Cloud has plotchart and graphs that display the data sent by the Bolt IoT module. Commonly used GPIO, can send only 1 analog data, as there is only 1 analog pin. When we connect an Arduino with the Bolt Module, we'll use the UART communication to display the Data sent by an external device through RX/TX pins to the Bolt Module.
Function Code -
Single Data
boltiot.processPushDataCommand(2);
Multiple Data
int sensorData1=20;
float sensorData2=20.6;
boltiot.processPushDataCommand(sensorData1,sensorData2);
Array Data -
int integerSensorData[3]={20,24,26};
float floatSensorData[3]={20.6,-10.5,30.4};
boltiot.processPushDataCommand(integerSensorData[0],floatSensorData[2],floatSensorData[1],integerSensorData[2],integerSensorData[1],floatSensorData[0]);
Circuit Diagram
Sample Code -
HardwareSerial
#include <BoltIoT-Arduino-Helper.h>
void setup(){
boltiot.begin(Serial);
}
void loop(){
boltiot.processPushDataCommand(analogRead(A0));
}
OR
SoftwareSerial
#include <BoltIoT-Arduino-Helper.h>
void setup(){
boltiot.begin(6,7);
}
void loop(){
boltiot.processPushDataCommand(analogRead(A0), analogRead(A1);
}
Just so you know, you can send multiple values using both hardware and software serial. It is only to show the method and similarity between both the modes.
Now, that we have the code ready, let us set up the Bolt Cloud.
During creating a New Product, select UART as the mode to collect data.
In Hardware Config, make the below configurations -
In code, you can write the below code (extension - js) to view Multiple Graphs -
var lineGraph = new boltGraph();
lineGraph.setChartType("lineGraph");
lineGraph.setAxisName('Time','Analog 1');
lineGraph.plotChart('time_stamp','val1');
var barGraph = new boltGraph();
barGraph.setChartType("barGraph");
barGraph.setAxisName('Time','Analog 2');
barGraph.plotChart('time_stamp','val2');
Now, let's Run the Code,
Boltduino (Hardware Serial) -
On Boltduino, when we attach the Bolt WiFi module, the hardware Serial pins (0 and 1) of Boltduino are by default connected with the TX and RX pin of Bolt Module.
Hence, we see the values directly. This is what the Bolt IoT Product Graph looks like after a few minutes.
Meanwhile, the Serial Monitor printed the values of A0 and A1 pin.
You'll see the data being plotted according to your data collection rate, and also when you click on "Push Data to Cloud". How does that happen?
Well, in the boltiot-arduino-helper documentation, it is mentioned, that the processPushDataCommand() function waits for an 'RD\r' from the Bolt WiFI module, and when received, it sends the data inside it.
Bolt Cloud has a customized API command that controls the Serial monitor to send 'RD' with a carriage return that is received by the Boltduino.
Arduino(Software Serial) -
To connect with any other Arduino, we can either do the Hardware Serial (pin 0 and 1) or Software Serial (any 2 digital pins). Let's do one program with the SoftwareSerial now.
We'll use pin 6 and 7 of the Arduino, and connect it with TX and RX of Bolt Module.
Circuit Diagram -
This is what the connection with Arduino Nano looks like -
As you can see, after a few minutes, the graph takes a good form.
When using Software serial, we won't see any value printed on the serial monitor, as the serial monitor only displays communication in the hardware serial (pin 0 and 1).
3. setCommandString & handleCommandThis function waits for a SerialWrite with a String command from the Bolt Cloud (either with serialWrite API, or dashboard). It has a couple of parameters, depending on which, it gets executed.
This function is placed in the void setup(), and is executed infinitely in the void loop() because of the handleCommand(). We'll understand it even more with examples.
Syntax -
setCommandString ( <A>, <B>, <C>, <D>);
A - String from Bolt Cloud waiting to be received.
B - Function to execute when A is received in the Serial Monitor.
C - How many more extra arguments will be sent along with the same data from Serial Write. (by default - 0)
D - The arguments will be separated with the following special character. (by default - ' ')
Do not worry, below I have provided sample programs for different scenarios.
Function Code -
Single Argument Data
String turnON(String *data) {
digitalWrite(13,HIGH);
return "Pin 13 was turned ON";
}
boltiot.setCommandString("ON",turnON);//by default 3rd arguement 0 & 4th argument ' '
https://cloud.boltiot.com/remote/<API_KEY>/serialWrite?deviceName=<Bolt_Device_ID>&data=ON
Double Argument Data
String setDigitalPin(String *data){
int pinNumber=data.toInt();
digitalWrite(pinNumber,HIGH);
return "Pin "+String(pinNumber)+" set to Digital value HIGH";
}
boltiot.setCommandString("SetPin",setPin,1); //by default 4th argument ''(null)
https://cloud.boltiot.com/remote/<API_KEY>/serialWrite?deviceName=<Bolt_Device_ID>&data=SetPin 3
Triple Argument Data
String runPwmCommand(String *data){
int pwmChannel=data[0].toInt();
int pwmValue=data[1].toInt();
analogWrite(pwmChannel,pwmValue);
return "Pin "+String(pwmChannel)+" set to PWM value "+String(pwmValue);
}
boltiot.setCommandString("SetPWM",runPwmCommand,2,'@');
https://cloud.boltiot.com/remote/<API_KEY>/serialWrite?deviceName=<Bolt_Device_ID>&data=SetPWM@3@125@
Sample Code -
#include <BoltIoT-Arduino-Helper.h>
String LEDon(String *data) {
digitalWrite(13,HIGH);
return "LED was turned ON";
}
String LEDoff(String *data) {
digitalWrite(13,LOW);
return "LED was turned OFF";
}
String setPin(String data){
int pinNumber=data.toInt();
digitalWrite(pinNumber,HIGH);
return "Pin "+String(pinNumber)+" set to Digital value HIGH";
}
String runPwmCommand(String *data){
int pwmChannel=data[0].toInt();
int pwmValue=data[1].toInt();
analogWrite(pwmChannel,pwmValue);
return "Pin "+String(pwmChannel)+" set to PWM value "+String(pwmValue);
}
void setup() {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
digitalWrite(13, LOW);
boltiot.begin(Serial);
boltiot.setCommandString("ON",LEDon);
boltiot.setCommandString("OFF",LEDoff);
boltiot.setCommandString("SetPin",setPin,1);
boltiot.setCommandString("SetPWM",runPwmCommand,2,'@');
Serial.println("Bolt-IoT - Arduino is READY !!");
}
void loop() {
boltiot.handleCommand();
}
Now, this code can be directly compiled and uploaded to the Arduino/Boltduino.
TESTING -
Buttons on Bolt Cloud -
<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT Platform</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
<script>
setKey('{{ApiKey}}','{{Name}}');
</script>
</head>
<body>
<center>
<button onclick="serialWrite('ON');">Pin 13 ON</button>
<br>
<br>
<button onclick="serialWrite('OFF');">Pin 13 OFF</button>
<br>
<br>
<button onclick="serialWrite('SetPin 9');">Turn ON Pin 9</button>
<br>
<br>
<button onclick="serialWrite('SetPWM@9@20@');">SET Pin 9 to Value 20</button>
</center>
</body>
</html>
Since we have already learned how to use the library, let us recreate perform the same tasks with fewer functionalities (of course).
We'll perform the Serial Communication in between Arduino/Boltduino and Bolt WiFi Module.
1. Plot Data on Bolt Cloud
In our case, from Arduino the data printed on the Serial Interface is somewhat like 'val1, val2\n'. Where val1 and val2 are the analog data from the Arduino Board. We can attach any kind of sensor on pins A0 and A1 to send multiple analog data to the Bolt Cloud from the Pico.
To send single data, serial.write 'val\n' on the monitor.
Arduino Code -
void setup()
{
Serial.begin(9600);
while(!Serial);
Serial.println("Plot Graph on receiving RD\r");
}
void loop()
{
if (Serial.available())
{
String RD = Serial.readStringUntil('\r'
if (RD == "RD")
{
int a0 = analogRead(A0);
int a1 = analogRead(A1);
Serial.print(a0);
Serial.print(",");
Serial.print(a1);
Serial.println("\n");
RD = "";
}
}
}
Bolt Product Code (UART) -
var lineGraph = new boltGraph();
lineGraph.setChartType("lineGraph");
lineGraph.setAxisName('Time','Analog - 1');
lineGraph.plotChart('time_stamp','var1');
var barGraph = new boltGraph();
barGraph.setChartType("barGraph");
barGraph.setAxisName('Time','Analog - 2');
barGraph.plotChart('time_stamp','var2');
Graph -
2. ControlBoltduino/Arduino from Cloud
ArduinoCode -
void setup()
{
pinMode(13,OUTPUT);
Serial.begin(9600);
while(!Serial);
Serial.println("INPUT --- 1 TO TURN ON - 2 TO TURN OFF");
}
void loop()
{
if (Serial.available())
{
String pin = Serial.readStringUntil('\n');
Serial.print("Received Data = ");
Serial.println(pin);
if (pin == "ON")
{
digitalWrite(13,HIGH);
Serial.println(" TURNED ON");
Serial.println(".");
Serial.println(".");
}
if (pin == "OFF")
{
digitalWrite(13,LOW);
Serial.println(" TURNED OFF");
Serial.println(".");
Serial.println(".");
}
}
}
BoltIoT API (SerialWrite) -
Bolt Product Code (HTMLpage) -
<!DOCTYPE html>
<html>
<head>
<title>Bolt IoT Platform</title>
<script type="text/javascript" src="https://cloud.boltiot.com/static/js/boltCommands.js"></script>
<script>
setKey('{{ApiKey}}','{{Name}}');
</script>
</head>
<body>
<center>
<button onclick="serialWrite('ON');">Pin 13 ON</button>
<br>
<br>
<button onclick="serialWrite('OFF');">Pin 13 OFF</button>
</center>
</body>
</html>
Connections for regular will remain the same as explained in Software Serial (any 2 digital pins) and Hardware Serial (pin 0 and 1).
Here's the view of the serial monitor while I use the Bolt IoT APIs to control the Module.
You can even use the Bolt CLoud to send customised Serial Commands using below method (JS Extension)
singleButton({name:"Led On", action:"serialWrite", arguments: ["ON"]})
singleButton({name:"Led Off", action:"serialWrite", arguments: ["OFF"]})
'actions' lets you use any of the functions that existed in the hosted page, and based on 'arguments', you can customize the function we use inside. Even the inbuilt commands -
singleButton({name:"Alert", action:"alert", arguments: ["This is an Alert"]})
SAME AS
alert("This is an Alert");
For more information on Bolt IoT and its APIs, visit THIS PROJECT.
Now you have a brief Idea on How to use Arduino/Boltduino to send and get data from Bolt IoT Module. Indirectly making your Arduino part of an IoT System.
Comments