Saturday, September 10, 2016

Control Your Arduino BLE Device Using Smartphone

Bluetooth is one of the main connectivity technique used for short range connectivity in many devices.After the evolution of Bluetooth low Energy  it becomes more attractive to engineers and DIY hackers because of its low energy consumption.Many modules are available in the market which are easily interfaceable with  ‘Hackers toys’ like arduino,particle,raspberry pi,etc.Because of this high demand Arduino release their Arduino 101/Genuino 101 with inbuilt BLE module and raspberry pi3 has also got the inbuilt BLE.





This is the era of smartphones.People's daily life is highly depends on their smartphones.Almost all wearables are communicating with smartphones Via Bluetooth.There are many application of BLE in healthcare devices also which senses body sensor values like heartbeat and transmit into smartphones and then to cloud.
I have worked on classic bluetooth module HC05 as well as Adafruit Bluefruit  BLE  module.Many BLE modules are available in the market.In my research most of these BLE modules are working in peripheral mode.In this mode modules only can advertise their payload.It can not scan any other BLE modules in range.For scaning you will have to use BLE modules which supports Central mode.Usually the BLE modules in laptops and  smartphones are coming with central mode.there are some DIY friendly central mode supported bluetooth modules also available in the market.None of them are tested by me.HM10 ,BLE mini RedBear and BLE nano RedBear are such modules which can be used in central mode

Adafruit Bluefruit LE UART Friend is One of the coolest and easily interfaceable BLE modules available in the market.Since it is developed by Adafruit there are many documentation and tutorials are available in the internet.Adafruit also giving an arduino support library

The examples are  written to communicate with adafruits bluefruit LE app.There are codes available for BLE as beacons,uribeacons,eddystones,etc.Now i would like to explain how to modify this codes to our way.Ie how to control things using your samrtphone and BLE.

 Example for  arduino BLE to android communication





You can find the fritzing file here
You can find the hackster project here
  List of components
  1. Arduino Pro Mini
  2. Adafruit Bluefruit LE UART Friend 
  3. Buzzer
  4. LED
  5. Resistor 330 ohm
  6.  bluefruit LE app or any other custom designed android app
Connection
Arduino pin 9 ----Bluefruit RX
Arduino pin 10---Bluefruit TX 
Arduino pin 11---Bluefruit CTS
Arduino pin 12---Bluefruit Mode pin
Arduino GND---Bluefruit GND
Arduino Vcc---Bluefruit Vcc
Arduino pin 3---led
Arduino pin 6---Buzzer





 This example demonstrates how a device can be controlled using your smartphone.Here  i am  controlling An LED and buzzer using android App.This device will read incoming data from any other BLE device(which is working in central mode)and do actions.

    Incoming data                      Actions
character 1(ASCII49)  ----------  LED ON
character 2(ASCII50)  ---------- LED OFF
character 3(ASCII51)  ---------- will reply "Thank you for reading wiring it my way"
character 4(ASCII52)  ---------- BUZZER ON
character 5(ASCII53)  ---------- BUZZER OFF

You can use your own BLE app.Here I am Using bluefruit LE app from adafruit. in that after pairing  your device to your phone open UART mode in your app and send different characters like 1,2,3,4,5 and test your BLE.

As you know this is only the basic intro off BLE.Now try your own devices like BLE controlled toys,sensor data apps etc.please commend your BLE experiences.

 




Friday, August 5, 2016

MQTT Publish And subscribe Using RaspberryPi,Esp8266,And Particle Photon


     



  

Current trend in DIY is IoT based projects. The present professional  electronics engineering industry is also now focusing to IoT.There are many existing protocols are there for making connection with your device with Web.If we need real time data in every second or less than that,We will need a fast internet connectivity protocol.And it should use very little bandwidth.Here we have MQTT.

MQTT is an extreamly light weight connectivity protocol which is mainly used for machine to machine /IoT applications.It was formerly known as MQ Telemetry Transport.it is a publish/subscribe based messaging protocol for use on the top of TCP/IP protocol.MQTT is very helpful where network bandwidth is limited.Many IoT companies uses MQTT for their applications.


There are several MQTT brokers are available In this example we are using mosquitto broker.In MQTT there is several methods are defined like
1.Connect
2. Disconnect
3.Subscribe
4.Unsubscribe
5.Publish

As DIY hackers we also make use of MQTT in our projects .Many of the boards supporting WiFi is also supports MQTT.Here I am going to discribe MQTT broker in Raspberry Pi.And MOTT Client in Raspberry Pi,Particle Photon,esp8266.


I have no plan to explain the MQTT protocol ,but i would like to explain how it can be implemented  in our DIY project.In MQTT protocol we will have broker and clients.clients are those which can publish and subscribe to some topics created in broker.Broker is responsible for all fitering and all.ie broker will receive all messages and it will send those messages to corresponding subscribed clients.MQTT client libraries are easly available for many devices and in many coding languages.


It is very easy to set up a MQTT broker in linux system.As this blog is mainly explaining DIY projects I would like to Explain how to install MQTT broker in Raspberry Pi,which is one of the best toy of an electronics hobbyists.And I will be sharing how to set up MQTT clients(publish/subscribe) in Raspberry pi, Particle photon,esp8266,which are main tools in IoT hardware prototyping.All these are inspired from community.So I would like to thank all bloggers in community.



First we can Setup a MQTT broker in our Raspberry pi.For installing Mosquitto broker follow the steps below.






How to install MQTT broker in Raspberry pi

First we have to import repository package for mosquitto into our raspberry pi.For that  open the Raspberry pi terminal and run below commands.I used a Raspberry Pi 3 in jessie with 16GB microSD card 




sudo apt-key add mosquitto-repo.gpg.key
Now we have to make available this repository to apt by using below command


cd /etc/apt/sources.list.d/


Now follow below commands


sudo -i
for updating pi
sudo apt-get update
apt-get update
After updating we can install mosquitto MQTT broker running below command
apt-get install mosquitto


How to install MQTT Client in Raspberry pi

So  now we successfully installed MQTT broker in our Raspberry Pi.Now we can install MQTT client by below command
apt-get install mosquitto-clients
Now the client library for MQTT is installed



So we successfully installed MQTT.Now we can start publishing and subscribing to some topics.






How to use MQTT client as Publisher
To publish a data “wiring_it _my _way ” to  a topic named “color”we can use the below commands


If the broker is installed in the same Raspberry pi
mosquitto_pub -h localhost -t color -m wiring_it_my_way



If the broker is installed in different Raspberry pi
mosquitto_pub -h 192.168.0.114 -t value -m wiring_it_my_way


How to use MQTT client as Subscriber
If the broker is installed in a different system then for subscribing a topic named “color” just run the below command


mosquitto_sub -h 192.168.0.114 -t color             
Here the IP in the command is lP of broker  raspberry pi/system
if the client is running in the same raspberry pi where broker is installed then use the below command for subscribing a topic named “color”
mosquitto_sub -h localhost -t color

ESP8266 as MQTT client

   As we know  ESP8266 WiFi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any micro controller access to our WiFi network.Also we can program the ESP8266 directly from our arduino IDE.There many libraries available for ESP8266 in the community.I am sharing two code snippets .One is for publishing using ESP8266 and the other is for publishing and subscription.


Here our broker is set upped in raspberry pi so we have to note the IP of the raspberry pi and include in our ESP8266 arduino code.
Here given code is only basic code please make your own variety with sensors and actuators have fun.And please share in comments  I used an ESP8266-12






 

 

 

 

 

 Particle Photon as MQTT client

In one of my previous post we have discussed about the particle module,which is one of the best choice for IoT based prototyping.Particle web IDE contains MQTT libraries in it.By including those libraries we can easly subscribe or publish to a MQTT topic.Here also we have to give IP address of our pi in Particle code.
.

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

// This #include statement was automatically added by the Particle IDE.
#include "MQTT/MQTT.h"

#include "MQTT/MQTT.h"
String sensor;
byte server[] = { 192,168,0,115 };//the IP of broker
void callback(char* topic, byte* payload, unsigned int length);
MQTT client(server, 1883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))   
        RGB.color(255, 0, 0);
       
    else if (message.equals("GREEN"))   
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))   
        RGB.color(0, 0, 255);
    else if (message.equals("wiring_it_my_way"))   
        {RGB.color(255, 0, 0);
        delay(1000);
        RGB.color(0, 255, 0);
        delay(1000);
        RGB.color(0, 0, 255);  
        delay(1000);}
    else   
        RGB.color(255, 255, 255);
    delay(1000);
}

void setup() {
    pinMode(A0,INPUT);
    RGB.control(true);
    client.connect(System.deviceID());
    if (client.isConnected()) {
        client.subscribe("color");//color is the topic that photon is subscribed
        client.publish("fun", "hello");//publishing a data "hello" to the topic "fun"
       
    }
}

void loop() {
    int sensor_value =analogRead(A0);
    sensor =String (sensor_value);
    if (client.isConnected())
        client.loop();
         client.publish("value", sensor);//publishing a sensor data  to the topic "value"
        
   
     
}   


when i heard the name MQTT for the first time i thought it will be very difficult for a hardware geek like me.But the broker set up was very easy and straight forward.It makes MQTT very easy.Regarding quality there are methods in MQTT to retain messages,confirmation etc.I don't have a plan to explain those in this post.Now make your own project in MQTT in whichever platform you like and please let me know your ideas in comments.  


 you can find the hackster project here









Monday, July 25, 2016

Ambient Light Sensor For Microcontroller Based Projects








TEMT6000 ambient light sensor is just another analog sensor.It is compatible with micro-controllers which supports analog data input.This sensor is working like a transistor.As light falling on sensor increases the analog output(SIGNAL)will increase.
                                                 If the light is very low then the SIG will be also very low as light increases the SIG will also goes high.

       We can useTEMT6000 ambient light sensor from spark fun electronics in various applications that includes ambient light sensing.Like if you want to control your bulb according to ambient light then TEMT6000 ambient light sensor  is best choice over simple LDRs.This one is more reliable that LDRs

       TEMT6000 is a silicon NPN epitaxial planar photo transistor .The device is sensitive to the visible spectrum.TEMT6000 is used as ambient light sensor for display back light dimming in mobile phones,Notebook computers ,cameras etc.
      The below circuit and code is for demonstrating basic working of TEMT6000 Ambient light sensor.Here we are  using Arduino Pro Mini as our microcontroller.You can use either a battery or FTDI+USB for powering the board.Anyway if we want to see the data on serial monitor we need to use FTDI and USB.In the below code i am just reading the data and printing it into percentage of Ambient light.And arduino inbuilt LED will ON if the analog value from the sensor goes beyond certain value.
CONNECTION
ARDUINO PRO MINI                  SENSOR
VCC                                               VCC
GND                                              GND
A0                                                   SIG


You can find  fritzing files here
you can find the hackster project here
Arduino Code

Purchase links
Sparkfun electronics 
Rhydolabz 


Tuesday, June 7, 2016

Playing With HC-SR04 Ultrasonic Ranging Module

 Introduction

 HC-SR04 is a ultrasonic  ranging module which is working like bats.ie it uses sonar technique for ranging.This module is widely used in between arduino hackers and diy enthusiast.The main attraction of this module is it is one of the simplest module and can be recommend to any newbie
 

 

 Applications

HC-SR04 is a module which can be used in various applications in our DIY projects.
  • As obstacle detector in obstacle avoiding robot.
  • In walking assistant system for visually impaired people.
  • As parking sensor in vehicles. 
  • As sensor in bugler alarm type projects.

 

 

Example project

Arduino based ultrasonic obstacle detector 

components needed

  1.  Arduino ProMini x 1
  2. HC-SR04 x1
  3. Buzzer x 1
  4. LED x 1
  5. 220 ohm resistor x1

 Connections 

Arduino                                                 HC-SR04

pin 9                                                         trig
  pin8                                                          echo
  vcc                                                           vcc
  gnd                                                           gnd 



arduino pin 6 is connected to LED.
arduino pin A3 is connected to a buzzer.
arduino can be power via usb/ftdi or a 5v battery

working 

 In this demo project HC-SR04 is used as simple obstacle detector.it detects objects in front and will reply how far the obstacle is from the sensor.If an obstacle is detected it will alarm with a buzzer.If the obstacle is very close the led will ON.

Arduino code

//http://anasdalintakam.blogspot.com/2016/06/playing-with-hc-sr04-ultrasonic-ranging.html
//this is a simple project to demo working of HC-SR04
//connctions
//pin9-hcsr04 trig
//pin8-hcsr04 echo
//vcc-hcsr04 vcc
//gnd-hcsr04 gnd
//pin6-led
//pin A3-buzzer



int trig=9;
int echo=8;
int duration;       
float distance;
int val; 
float meter;      
void setup()
{
  Serial.begin(9600);
  pinMode(trig, OUTPUT);//trigger to hcsr04
  pinMode(echo, INPUT);//echo from hcsr04
  pinMode(6,OUTPUT);// led pin
  pinMode(A3,OUTPUT);// buzzer pin
  digitalWrite(trig, LOW);
  delayMicroseconds(2);
 
  delay(6000);
  Serial.println("Distance:");
}
void loop()

  digitalWrite(trig, HIGH);   
  delayMicroseconds(10);              
  digitalWrite(trig, LOW);   

  duration = pulseIn(echo, HIGH);

  if(duration>=38000){
      Serial.print("Out range"); 
      }    

  else{
      distance = duration/58;
      val=200-distance;  
      Serial.print("value:");
      Serial.print(val);
      Serial.print("\n");
      Serial.print(distance);   
      Serial.print("cm");
      meter=distance/100;
      Serial.print("\t");
      Serial.print(meter);
      Serial.println("m");
   if (distance >10){
   digitalWrite(6,LOW);
    }
    else {
   digitalWrite(6,HIGH);
      }
analogWrite(A3,val);
     
      }
  //   delay(1000);  
}





Monday, April 11, 2016

PARTICLE CORE the best hardware choice for IoT based prototyping




Future world is the world where everything is connected.Nowadays electronics engineers researches mainly on IoT-internet of things.we need internet of everything.There are many boards and modules in market which helps hobbyists to develop their prototypes and play with connected things.Arduino is already providing Ethernet shield ,WiFi shield for connect the device into internet.Raspberry Pi is also came up with their WiFi and LAN.especially their latest Raspberry Pi3 has inbuilt Bluetooth and WiFi
But in this blog post  i am going to explain my experience with a cool microcontroller board other than Arduino and Raspberry Pi.That is Particle core - a microcontroller board with inbuilt WiFi module and cloud connectivity.(https://www.particle.io/)

Particle core

Particle is core is one of the amazing and cool stuff for electronic engineers,hobbyists and prototype makers.This board can be easily connected to internet as easily as we connect our smartphones to internet.

  • It uses ARM 32-bit Cortex™-M3 CPU Core as its brain
  • The WiFi module is CC3000

Some of its features makes this board extra cool.

1.This board can be coded remotely.

    Yes you can code this board far away from your hardware.Because Particle uses a
web IDE,the code is debugging from their cloud and uploading it to Particle core via internet.All you need is set up your account in particles web IDE(https://build.particle.io/build/) you will have a  unique id for your particle.if you keep connected the particle with WiFi you can code it from anywhere in the world.

2.Arduino like language.

     No need to study a new language for programming your particle.The coding language used for Particle is very much similar to arduino C.So any arduino hobbyist can easily play with particle.

3. Particle variables

    Particle variable is another cool thing.you can send particle variable data into cloud.it can easily see using URLs which is based upon your devices unique id.It have its own API.Also if you are using
Particle Dev(a desktop application)  in your computer,you can easily watch the current value of particle variables.All you need to do  is just declare your variable as particle variable by simple command,then the data will be in the cloud.No need of any long coding (https://docs.particle.io/reference/firmware/core/)


4.Particle Functions

   These are cloud functions.You can easily trigger value from cloud and controll your device according to that function. This is also working using particles own rest API (https://docs.particle.io/reference/firmware/core/)



5.Tinker the mobile App

Tinker is a mobile app which can use in your smartphones.if you upload the corresponding code for tinker in your particle core you can control all pins of your device using your phone.you can make a pin HIGH or LOW or can write and read analog values.

6.connection with IFTTT

Particle is a verified channel in IFTTT services. You can write conditional statement actions using this.
For eg;if you want a mail notification/sms/push notification when your particle variable value is below or above(like your room temperature is x ) that is so easy with IFTTT and particle

7 .Arduino like pins and Protocols

Particles pins are similar to arduino.but its voltage output is 3.3V max.Particle supports protocols like UART,SPI,I2C,

Sunday, April 10, 2016

Arduino my buddy

I was one of those engineering students, who was hatred programming always.I used to say that there is no place for programming in an electronics engineers life.I have done a number of electronics projects before but never tried to code a microcontroller.So I always find happiness in doing wiring simple 555 multivibrator stuff.I always  by-hearted codes for exams.I didn't even try to understand, because I always believed that I can't never do programming myself.this is not my job.
      
                Then one day I got an arduino. Things changed after that.Even I too programmed a microcontroller from my heart not by-heart.I can't explain my feeling when the LED started blinking when I programmed the arduino first time.The most interesting thing which I felt was there is no need of knowing registers and all in arduino.I will call it as programming the pins.

                Even after completing my exam on microcontrollers and microprocessors I don't understood what they mean by i2c? ,what is uart?,what is spi?,actually I managed to pass those papers like all other students by simply reading text books and write those exact words in my answer sheet.when I started playing with arduino I started to learn things in different way other than text books way.
               Even now after completing by engineering course I am not happy with the way that curriculum running.Most of the students manages to get good GPA ,but majority goes to IT field or to banks.The cause is that even after completing their engineering in electronics they are not able to apply it,because they don't know what it is.
           If I don't get my buddy arduino,now I may be working as some IT professional.I am always thankful to arduino for giving me a chance to live as an electronics engineer after my engineering.