Control Devices through Arduino and Bolt IoT Module
Here we take an example of a Led to control it through Arduino and Bolt IoT Module
Component required
- Arduino Uno
- Bolt IoT Module
- Led
- Jumper Wire
Arduino Code
#include <BoltIoT-Arduino-Helper.h> //Include Bolt-Arduino-Helper Library
#define led 6 //Pin declaration for led
String command = ""; //variable for storing command
// function for Controlling Led
String on(String *data) {
command = "ON";
led_on();
return "Led:ON";
Serial.println("Led is now turned on");
}
// function for Controlling Led
String off(String *data) {
command = "OFF";
led_off();
return "Led:OFF";
Serial.println("Led is now turned off");
}
void setup() {
// put your setup code here
pinMode(led,OUTPUT); // pinMode decide input or output type
boltiot.begin(Serial); //here hardware serial is used
Serial.begin(9600); //baud rate for transsmit and receive data
boltiot.setCommandString("ON",on);
boltiot.setCommandString("OFF",off);
}
void loop() {
// put your main code here, to run repeatedly:
boltiot.handleCommand(); //Bolt command controll
}
void led_on(){
digitalWrite(led, HIGH);
}
void led_off(){
digitalWrite(led,LOW);
}
Bolt IoT Cloud Code
<!DOCTYPE html>
<head>
<title>
| Bolt Led Control |
</title>
<script>
var http = new XMLHttpRequest();
var apiKey, deviceId;// variable to store apiKey and devicId
apiKey = ""; // Enter your apiKey
deviceId = ""; // Enter your deviceId
if(apiKey == "" || deviceId == ""){
alert("Please enter an API KEY and a Device ID"); // it display alert
}
else{
alert("You are ready to play");
}
// function for turn onn the led
function onn(){
http.open("GET","https://cloud.boltiot.com/remote/"+apiKey+"/serialWrite?data=ON&deviceName="+deviceId,true);
http.send();
}
function off(){
http.open("GET","https://cloud.boltiot.com/remote/"+apiKey+"/serialWrite?data=OFF&deviceName="+deviceId,true);
http.send();
}
</script>
<style>
.button {
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
.button1 {background-color: #4CAF50;} /* Green */
.button2 {background-color: #008CBA;} /* Blue */
</style>
</head>
<body>
<h1>Bolt Led Control</h1>
<button class="button button1" onclick="onn()">Turn on the Led</button>
<button class="button button2" onclick="off()">Turn off the Led</button>
</body>
</html>
No comments: