Archives | Botshop
Control with the Arduino bluetooth module

Control with the Arduino bluetooth module

We found this incredible tutorial on how to use the HM-10 BLE Arduino Bluetooth module by Hammad Tariq

connections_hn10_arduinoIn this tutorial, you will learn about controlling a LED using HM-10 BLE Arduino bluetooth module, Arduino and Evothings Studio.

Last Summer, I wrote a tutorial about controlling the lights of your home using Arduino and HC-05 bluetooth module. While, HC-05, HC-06 and HC-09 are still famous and available everywhere, they are essentially based on Bluetooth 2.0 technology. On the other hand, many new smartphones support only BLE (Bluetooth Low Energy or Bluetooth 4.0) instead of Bluetooth 2.0 or Bluetooth 1.0. The iPhone is most prominent of those smartphones as it’s supporting BLE since iPhone 4S, which was released nearly 5 years ago!

As I sat down to explore what options we have for prototyping a BLE enabled IoT device, the HM-10 came up as a prominent module in this space as it’s inexpensive and available everywhere. The module is also based on already familiar TI’s CC2541 BLE SoC. The module also has a few clones; one is called BT-05, another is called AT09, yet another is known as the SPP-CA HC-05/HC-06 or BT06, yet some clones are based on ZS-040 breakout boards just like HC-05.

There is an excellent post by Martyn Currey if you want to identify the module you have or want to explore the differences between them. Essentially, if your module is based on CC2541, that is BLE and you should be able to use this tutorial with the exception of UUID of the module, that I will explain later in the tutorial. Also, HM-10 and all other clones use AT commands for configuration, you can read the datasheets for reference but this tutorial or mobile app does not need you to use any AT command.

In addition to the HM-10 and Arduino, I will be using Evothings Studio to develop our mobile app. Evothings Studio is ideal for developing IoT mobile apps as it’s easy to use, gets you started in minutes even if you have “some” knowledge of JavaScript and HTML. Also, it has useful pre-built libraries and plugins, such as, for this example, the Evothings Studio already has necessary libraries to work with BLE, all you need is to write down a few lines of code to connect and send commands to your BLE module.

Step1: What you will need.

 

 

 

1 x HM-10 or anyother similar Arduino bluetooth module
1 x Arduino Uno
1 x LED
1 x 220 ohm resistor

 

Step 2: Connect the circuit

 

 

Connect the Arduino and Bluetooth module pins as shown below.

  1. Connect 3.3V of Arduino to the VCC of HM-10
  2. Connect GND of Arduino to the GND of HM-10
  3. Connect D8 of Arduino to RX of HM-10
  4. Connect D7 of Arduino to TX of HM-10
  5. Connect D2 of Arduino to the long leg of LED along with a 220 ohm resistor
  6. Connect the short leg of LED with the GND of Arduino

 

HM-10 and Arduino Wiring Diagram

HM-10 AND ARDUINO WIRING DIAGRAM

 

 

 

Step 3: Upload the Arduino Sketch

 

 

Go to my Github repository and download/copy-paste the Arduino sketch to your Arduino IDE. Upload the sketch to your Arduino.

Step 4: Download the Evothings Studio

You can skip to next step if you already have Evothings Studio and are familiar with it’s working.

Follow these steps:

  1. Download and install Evothings Workbench on your computer. Generate an anonymous Cloud Key further down on the download page, paste it into the Workbench software.
  2. Download Evothings Viewer app from an appstore (iOS, Android)
  3. Open Evothings Workbench and click on “Get Key” button
  4. Open Evothings Viewer app, provide your connection key and tap the “connect” button
  5. Once the connection is successful, go to “Examples” tab and click the “Run” button for the “Hello World” example

You should see the “Hello World” app loaded into the Evothings Viewer; that is how the whole development suite works together, whatever changes you will make in your app code, the Evothings Workbench will reload it in the Evothings Viewer, allowing you to preview your changes in real time!

Step 5: Developing the Mobile App

On Evothings Workbench, click “Run” for “BLE Scan” example and note down the name of your BLE module.

Clone or download this Github repository on your computer. Open “hm10-arduino-ble” example, go to “app folder”, drag & drop the “index.html” to “My Apps” tab of Evothings Workbench.

Now open “index.html” in your favourite code editor, check “app.connect” function to confirm if your module has the same name as written in the code, else change it with your module’s name.

Now click the “Run” for your new project entry in “My Apps” tab, the app should load in Evothings Viewer, press the “Connect” button. Once connected, use the buttons to switch your LED On/Off.

Tip: If you experience any difficulty in connecting with your module, first identify which module do you have and then search for it’s UUID (universal unique identifier) online. Correct UUID should be given in the app.ledOn and app.ledOff functions of index.html.

Code Explanation

As explained earlier, the Evothings Studio comes bundled with all necessary libraries to connect and get you started with BLE. Our example is making use of arduinoble and easyble libraries which are located in app/libs/evothings directory. See this tutorial on more details about them. As, for now, we don’t need to go in detail of how these libraries are working, we can just focus on the code in the “index.html” file.

Following block of code is used to connect to the BLE module:


app.connect = function()
{
evothings.arduinoble.close();
evothings.arduinoble.connect(
'BT05', // Name of the module.
function(device)
{
app.device = device;
app.showMessage('Connected! Touch buttons to turn LED on/off.');
},
function(errorCode)
{
app.showMessage('Connect error: '   errorCode   '.');
});
};

In this block of code, we are providing the name of the module to the library function of evothings.arduinoble.connect, upon success, we show a success message.

Similarly, analyze following block of code:


// Turn on LED.
app.ledOn = function()
{
app.device && app.device.writeDataArray(new Uint8Array([1]), '0000ffe1-0000-1000-8000-00805f9b34fb');
}
// Turn off LED.
app.ledOff = function()
{
app.device && app.device.writeDataArray(new Uint8Array([0]), '0000ffe1-0000-1000-8000-00805f9b34fb');
}

In this block of code, we are calling library function app.device.writeDataArray to write 0 and 1 to the BLE module along with the UUID of HM-10 module.

In the Arduino sketch, analyze the following block of code:


void loop() {
int c;
if (mySerial.available()) {
c = mySerial.read();
Serial.println("Got input:");
if (c != 0)
{
// Non-zero input means "turn on LED".
Serial.println("  on");
digitalWrite(LED_PIN, HIGH);
}
else
{
// Input value zero means "turn off LED".
Serial.println("  off");
digitalWrite(LED_PIN, LOW);
}
}
}

In this block of code, we are reading the software serial of Arduino. We simply switch the LED on if we receive anything other than 0 on the software serial, similarly, if it’s 0, we turn the LED off.

You can also check the serial monitor of Arduino IDE to see what you are receiving on Arduino’s software serial.

IOT devices NodeMCU with ESP8266 part 2

IOT devices NodeMCU with ESP8266 part 2

IOT devices Part 2

Part 1 can be read here.
In this tutorial we will control electrical devices like fans, lights, etc., using esp8266 from a web browser.  To do that we need to get familiar with IOT devices. The Arduino sketch below shows how you can add HTML code (web page code) to the NodeMcu board so it will give you a web page when connecting to it. The web page the NodeMcu shows you will have buttons you can click on to control relays that in turn will control lights and so on.
What makes this cool is that you can connect to your NodeMcu from your wireless network from any device that got a web browser, including your phone.
Note: Make sure your browser enabled devices and the NodeMcu board is on the same wireless network. Depending on your networking knowledge you can take this a step further and configure your wireless router to do port forwarding and dynamic DNS to access your NodeMcu from anywhere on the Internet. Another good idea is to setup your router to always assign the same IP to your NodeMcu board in your router’s DHCP settings.
The components that you will need for completing this project are very simple, you need to have a NodeMcu module and a relay, make sure you buy a 5v relay, which are very easy to use with esp chips, it doesn’t require an external power supply.
We tested and sell this NodeMCU board.
You can also use this board with the easy and powerful LAU scripting language, more info can be found on the LAU website.

For this example project, I have used a 2 relay circuit. The actual program is written to connect four relays to IOT devices.

You can watch the above video on how it works and how to connect your browser to the IP address returned from esp, all the details are included in this video. The MCU IoT devices make it really easy these days.

Copy the below Arduino code and paste it into your Arduino IDE, then upload the program to your nodemcu or any other esp devices that you are using, make sure to choose the correct port and board. Also, don’t forget to change the SSID and password to your Wi-fi settings.
This program for the esp8266 also will return the status of the device, which will. in turn notify you when it’s connected to the specified network and it will also tell you what IP it received from the router.

#include <ESP8266WiFi.h>

const char* ssid = “Your Network Name”;
const char* password = “Your Password”;

; //
WiFiServer server(80);

void setup() {
Serial.begin(9600);
delay(10);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(0, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(0, LOW);
digitalWrite(13, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);

// Start the server
server.begin();
Serial.println(“Server started”);

// Print the IP address
Serial.print(“Use this URL to connect: “);
Serial.print(“http://”);
Serial.print(WiFi.localIP());
Serial.println(“/”);

}

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println(“new client”);
while(!client.available()){
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil(‘\r’);
Serial.println(request);
client.flush();

// Match the request

if (request.indexOf(“/light1on”) > 0) {
digitalWrite(5, HIGH);

}
if (request.indexOf(“/light1off”) >0) {
digitalWrite(5, LOW);

}

if (request.indexOf(“/light2on”) > 0) {
digitalWrite(4, HIGH);

}
if (request.indexOf(“/light2off”) >0) {
digitalWrite(4, LOW);

}
if (request.indexOf(“/light3on”) >0) {
digitalWrite(0, HIGH);

}
if (request.indexOf(“/light3off”) > 0) {
digitalWrite(0, LOW);

}
if (request.indexOf(“/light4on”) > 0) {
digitalWrite(13, HIGH);

}
if (request.indexOf(“/light4off”) > 0) {
digitalWrite(13, LOW);

}
// Set ledPin according to the request
//digitalWrite(ledPin, value);

// Return the response
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“”); // do not forget this one
client.println(“<!DOCTYPE HTML>”);
client.println(“<html>”);
client.println(“<head>”);
client.println(“<meta name=’apple-mobile-web-app-capable’ content=’yes’ />”);
client.println(“<meta name=’apple-mobile-web-app-status-bar-style’ content=’black-translucent’ />”);
client.println(“</head>”);
client.println(“<body bgcolor = \”#f7e6ec\”>”);
client.println(“<hr/><hr>”);
client.println(“<h4><center> Esp8266 Electrical Device Control </center></h4>”);
client.println(“<hr/><hr>”);
client.println(“<br><br>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 1”);
client.println(“<a href=\”/light1on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light1off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 2”);
client.println(“<a href=\”/light2on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light2off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 3”);
client.println(“<a href=\”/light3on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light3off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“Device 4”);
client.println(“<a href=\”/light4on\”\”><button>Turn On </button></a>”);
client.println(“<a href=\”/light4off\”\”><button>Turn Off </button></a><br />”);
client.println(“</center>”);
client.println(“<br><br>”);
client.println(“<center>”);
client.println(“<table border=\”5\”>”);
client.println(“<tr>”);
if (digitalRead(5))
{
client.print(“<td>Light 1 is ON</td>”);

}
else
{
client.print(“<td>Light 1 is OFF</td>”);

}

client.println(“<br />”);

if (digitalRead(4))
{
client.print(“<td>Light 2 is ON</td>”);

}
else
{

client.print(“<td>Light 2 is OFF</td>”);

}
client.println(“</tr>”);
client.println(“<tr>”);

if (digitalRead(0))

{
client.print(“<td>Light 3 is ON</td>”);

}

else

{
client.print(“<td>Light 3 is OFF</td>”);
}
if (digitalRead(13))
{
client.print(“<td>Light 4 is ON</td>”);

}
else
{
client.print(“<td>Light 4 is OFF</td>”);
}

client.println(“</tr>”);
client.println(“</table>”);

client.println(“</center>”);
client.println(“</html>”);
delay(1);
Serial.println(“Client disonnected”);
Serial.println(“”);

}

IOT modules with NodeMCU with ESP8266 WiFI part 1

IOT modules with NodeMCU with ESP8266 WiFI part 1

The ESP8266 WiFi chip

The first thing you need to know is that the ESP8266 WiFi chip is much more than just a WiFi chip. It also includes its own microprocessor, 16 input/output pins, 64 KiB of instruction RAM and 96 KiB of data RAM.

The ESP8266 wifi chip can me run as a serial device from an Arduino but it can also be used as a stand-alone device!

You can use this chip on a breakout board with a couple of components to create a stand-alone Internet Of Things device. This is much more cost effective than an Arduino with a WiFi shield. IOT esp8266 WifI devices became very popular these days because of the amazing benefits included in the ESP chip.

Thanks to the guys who ported the Esp8266 into Arduino IDE and helping all the Arduino users to easily program the ESP8266.

Most of this NobeMCU part 1 post discusses how to install the Esp8266 support for the Arduino and then a short script on how to blink a LED (the hello world in the electronics).

Firstly open the Arduino IDE and then go to the file menu and click on the preference sub menu in the Arduino IDE

Copy the below link into the Additional boards Manager URLs

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Click OK to continue.

After completing the above steps , go to Tools -> board, and then select Boards Manager

Search for ESP8266 in the search bar and install “ESP8266 by ESP8266 Community”.

Once all the above are completed we are ready to program our esp8266 with Arduino IDE.

For this example I have used NodeMCU esp8266, if you are using any other vendor wifi chips or generic wifi module please check with the esp8266 Pin mapping which is very essential to make things works.

The reason why I used D7 pin for this example is , I uploaded the basic blink program that comes with the examples program in the arduino IDE which is connected with 13 pin of arduino. The 13th pin is mapped into D7 pin of NodeMCU.

Go to the tools menu and then the board sub menu and select the type of esp8266 you are using and select the correct COM port to run the program on your esp8266 device. For the Node MCU, select NodeMCU 1.0 (ESP-12E Module)

 

void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Part 2 of this block post can be found here.