Monday, September 30, 2013

arduino-bluetooth switch


`

                                           Bluetooth Switch

moinakhtar J.D
frugal-labs tech solution PVT. LTD

1. Bluetooth module

AT commands can found here(u.com/files/ElecFreaks/HC-0305 serail module AT commamd set 201104 revised.pdf)
According to datasheet and referring to schematics of HC-05 used in this bluetooth module
we can see 6 pin are provoided for external use,
Hold module such that you get antenna at the top and external pin's facing earth,as shown in below fig.

if we consider the first at you left is first pin of board then we have
pin 1 -- Not connected
pin 2 – KEY
pin 3 – RX
pin 4 – TX
pin 5 – 5v
pin 6 – GND

Note 1 :: when we verify RX and TX pin of board with actual RX and TX of module(CHIP) using multimeter in contunity mode, we found that RX on board actulally TX of CHIP and vice versa.

2.MICROCONTROLLER
we have used arduino board

as show in the image we have RX and TX for serial communication with arduino

3.SWITCH

for this project an SSR is used HFD27/005 s
datasheet can be found here(http://www.datasheetdir.com/HFD27-005-S+download)
remember pin below 1A mark is pin-16
by seeing data-sheet we have this following diagram

look at the last image,
when SSR is not triggered 4 and 6 are connected and 13 and 11 are connected together leaving 8 and 9 unconnected respectively
and here when a 5v is applied at 1 and 16 ,pin 4 and 8 will be connected together and 13 and 9 are connected together ,allowing current to pass through them,and leaving 6 and 11 unconnected respectively,
we gone use this phenomenon,for switching AC current

NOTE:during this project i only had HFD27/005 SSR,any other SSR which can controlled using 5v can be used,just make sure where to apply voltage how switches work

4.BLUB
we have used a normally available 60 watt bulb

put it in a plug and connect to switch and check if it is working properly
then turn off the main power and take out plug from AC socket ,

NOTE 2:: you are dealing with AC power so be safe and take required remedies before proceeding from here

CONNECTION

step 1 :: connection between bulb and AC

you will see two wires, select any one wire out of two,and cut in middle, i chose +ve or red coloured wire,
note :: doesn’t matter which wire you choose because AC is available for both negative and positive cycle.

for reference purpose ,i will call the these new terminal as 1 and 2

step 2 :: connection between SSR and BULB
connect terminal 1 to pin 4 on SSR ,and terminal 2 to pin 8 on SSR,

step 3 :: connection between Bluetooth module and Arduino

bluetooth pin ---- Arduino pin
pin3 / RX                     RX
pin4 / TX                      TX
pin5 /5V                       5V
pin6 / GND                 GND

if you have followed some other post then you might get confused about my connection,because usually we connect RX to TX and vice versa for communication,
but recall(Note 1 )
i said
when we verify RX and TX pin of board with actual RX and TX of module(CHIP) using multimeter in continuity mode, we found that RX on board actually TX of CHIP and vice versa.

.hurrah.... we are actually connecting RX to TX and TX to RX,so my connection are pretty good and universly correct
,
connect bluetooth module with system's bluetooth and activate SPP ,protocol
more details on configuring(http://www.instructables.com/id/Cheap-2-Way-Bluetooth-Connection-Between-Arduino-a/)

or just download Bluetooth SPP application(https://play.google.com/store/apps/details?id=mobi.dzs.android.BluetoothSPP&hl=en) for android and follow simple steps,and you will be connected with module Serially

step 4 :: connection between SSR and arduino

SSR ----------- Arduino
pin 1                pin 10
pin 16              pin GND

you may ask ,why we are connecting pin 1 of SSR to pin 10 of Arduino,and not to VCC?
Ans: if we connect pin 1 directly to the VCC of arduino,the SSR will be triggerd ,just by powering Arduino,and light will glow without any data through bluetooth,but we want to make a switch ,i.e wheever i recive perticular number from bluetooth i will turn bulb ON or OFF ,corresponding to number,so programicaly activate SSR i am connecting it to the pin 10.
ALL done with connection and now its time to program

PROGRAM

concept here is ,turn the bulb ON ,if we recieve 1 and Turn bulb OFF if we recive 0 via bluetooth

//initialize relay_pin to 10
int relay_pin=10;
//initializing INBYTE as Intiger
int INBYTE;

void setup(){
//I configured my HC-05 to communicate at 38400 baud rate and hence i am using
  Serial.begin(38400);
//make pin 10 as OUTPUT
  pinMode(relay_pin,OUTPUT);
}
void loop(){
//print Press 1 to ON or 0 to turn OFF LIGHT: on all serial monitor connected to bluetooth
  Serial.println("Press 1 to ON or 0 to turn OFF LIGHT:");
// stay here so long as COM port is empty
  while (!Serial.available());
// read next available byte
  INBYTE = Serial.read();
// if it's a 0 (zero) dont trigger SSR
  if( INBYTE == '0' ) digitalWrite(relay_pin, LOW);
// if it's a 1 (one) trigger SSR
  if( INBYTE == '1' ) digitalWrite(relay_pin, HIGH);
  delay(50);
}


that it plug you AC plug to its socket and press turn on button,bulb will not glow.
Now from device you have connected to bluetooth module,and send 1 to turn ON bulb and ) to turn OFF bulb
that's it enjoy your bluetooth switch and
why we have dummy switches.......??????
wait for next post..to know more
till then enjoy your BT-switch 

hi freind sorry for delay ..here is 

SOME THING NEW FROM HERE

why we are using demo switch?

Assume condition when you dont know ,where you have kept you mobile or the mobile's battery is pretty low.then you cant connect to bluetooth and hence cant use your switch

To over come this problem .we have used demo switch so that you can controll your bluetooth switch even by mechanical switch

first of all we need to make connection
connect one pin to GND and other to resistor we call this pin as switch pin , then connect the other pin to VCC ,
after this connect switch pin to any one pin of M-controller.in my case i connected it to pin 8.
NOTE:
we need this connection beacouse when i change the state of switch button ,my controller will read either 0,or 1 due to establishment /demolishing of connection on between VCC and GND
dont forget to use resistor between switch pin and VCC .

Similarly we change our program to update the components used

int sys = 0;
int ab = '0';
//int ab1 = INBYTE;
 
int as = LOW;
//int as1 = sbyte;
int INBYTE='0';
int sbyte;
int relay_pin=3;
int s = 8;

void setup(){
  Serial.begin(9600);
  pinMode(relay_pin,OUTPUT);
  pinMode(s ,INPUT);
}
void loop(){
  Serial.println("Press 0 to turn OFF or 1 to turn ON LIGHT:");
  //delay(1000);
  sbyte = digitalRead(s);
  //int sys;
  if(Serial.available()){
    INBYTE = Serial.read();
    Serial.println(INBYTE);
  }
  //if (INBYTE == ab) sys = sys;
  //if (sbyte == as) sys = sys;
  if (INBYTE != ab){
    sys =!sys;
    ab=INBYTE;
  }
  if (sbyte != as) {
    sys =!sys;
    as=sbyte;
  }
  //delay(1000);
  if ( sys == 1) digitalWrite(relay_pin, HIGH);
  //delay(1000);
  if ( sys == 0) digitalWrite(relay_pin, LOW);
  //delay(1000);
 
}

Thursday, September 5, 2013

HCITOOL LESCAN using PYTHON

HI ALL,
from past two week ,i was jumbling my self to how to do hcitool lescan using python and store it's real time data directly to a file(temporary file),
if you are the one looking for the same ,
then yeah,this post can solve your all question ,
so let me just give you code.if you dont want to understand whats happening in the code ,and worried only about out put,then just go ahead and paste the below code on python

#working fine
import os
import signal
import subprocess
import threading
import tempfile
import sys
import time
from tempfile import TemporaryFile
import commands
t = TemporaryFile()
print commands.getoutput("hcitool dev")
commands.getoutput('hciconfig hci0 down')
commands.getoutput('hciconfig hci0 up')
print commands.getoutput("hcitool dev")
commands.getoutput('killall hcitool')
p = subprocess.Popen('hcitool lescan', bufsize = 0,shell = True, stdout =subprocess.PIPE,stderr = subprocess.STDOUT)
time.sleep(10)
#os.kill(p.pid,signal.SIGTERM)
for i in range(0,30,1):
    print 'for'
    inchar = p.stdout.readline()
    i+=1
    if inchar:
        print 'loop num:',i
        print str(inchar)
        t.write(str(inchar))
print 'out of loop'
t.seek(0)
print t.read()

and to understand the code read below,ok 
first 9 lines are used call library's,
import os
import signal
import subprocess
import threading
import tempfile
import sys
import time
from tempfile import TemporaryFile
import commands


then go further to create a tempoary file,temporary file is used to create a virtual memory space to store the data,the advantage of this temp file is,that it will vanish once we are out of our script ,i.e you dont have to delete it manually,it will delete itself,
t = TemporaryFile()
now 't' is our tempfile,

going to next line,
print commands.getoutput("hcitool dev")

this line will print the available blue-tooth device on system
out of displayed devices find ,which one reefer’s to BLE and make a note of it ,

Wednesday, August 28, 2013

Beagle Bone Black ,setting up ehternet,bluetooth and wifi


This blog is about to tell you how to settup bluetooth and wifi on BBB  

TASK :1- Entering into beagle bone's angstrom terminal from ubuntu's terminal

 step-1: connect BBB to PC,through USB

 step-2: open terminal by pressing 'cntr+alt+T'

 step-3:to check for usb port available ,issue this command in terminal
  $ ls /dev
  this will give a list of availaible vertual port, check for ttyACM0 then

 step-4: issue following command on ubuntu's terminal

                 $ sudo screen /dev/ttyACM0 115200

  you are successfully entered into beagle-bone's angstrom terminal
 
 step-5:Press Enter and you should see the Ångström login screen:

         ---O---. 

         |       |                  .-.           o o 

         |   |   |-----.-----.-----.| |   .----..-----.-----.

         |       |     | __  |  ---'| '--.|  .-'|     |     |

         |   |   |  |  |     |---  ||  --'|  |  |  '  | | | |

         '---'---'--'--'--.  |-----''----''--'  '-----'-'-'-'

         -'  |

         '---'

 The Angstrom Distribution beaglebone ttyO0

 Angstrom v2012.05 - Kernel 3.2.18

 beaglebone login:

 Log in as root. At Password: press Enter. You should see the prompt:

 root@beaglebone:~#

 step-6: If you want to close communications with the 'bone, press Ctrl+A, k and answer “y” to Really kill this window (y/n)?

 Once you’ve logged in, if you close the screen, reopen it, and press Enter, the prompt will appear. 
       You don’t have to log in again until you reboot the 'bone. 
       this terminal will be called as beagle bone's terminal(remember),
in future case if we use word like beagle bone's terminal then you should come back on this terminal and perform the requierd action 
  
2] Enabling Ethernet Communications on BBB

 step-1: To view the PC’s existing Ethernet connections, on ubuntu terminal, enter

  $ ifconfig

  You’ll likely see entries for eth0, the PC’s Ethernet interface, and lo, 
 desktop loopback.
 step-2: connect ethernet wier to BBB's ethernet port
 
 step-3: Testing the Ethernet Connection on ubuntu's terminal, enter

  $ ifconfig 

  You should see a new connection that looks something like this:

                eth2      Link encap:Ethernet  HWaddr 00:18:31:8d:99:93 

          inet addr:192.168.7.1  Bcast:192.168.7.3  Mask:255.255.255.252
 
          inet6 addr: fe80::218:31ff:fe8d:9993/64 Scope:Link
 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:30 errors:0 dropped:0 overruns:0 frame:0

                       TX packets:44 errors:0 dropped:0 overruns:0 carrier:0

                        collisions:0 txqueuelen:1000 

                        RX bytes:6377 (6.3 KB)  TX bytes:9308 (9.3 KB)

  inet addr is the IP address the PC uses to communicate with the 'bone. 
In the example above, the address is 192.168.7.1.

  To find the IP address the BBB is using, on the beaglebone's terminal, enter

  ifconfig

  If the 'bone is using 192.168.7.2, at the desktop terminal, enter:

  ping 192.168.7.2 

  The 'bone should respond with data packets:

          PING 192.168.7.2 (192.168.7.2) 56(84) bytes of data.

          64 bytes from 192.168.7.2: icmp_req=1 ttl=64 time=2.19 ms

          64 bytes from 192.168.7.2: icmp_req=2 ttl=64 time=0.152 ms

  Press Ctrl+C to quit.

  To test in the reverse direction, in the beaglebone terminal, enter:

  ping 192.168.7.1
  
  great now we have a working ethernet on board.
now we can pass update command to update our BBB's  operating sys
  
  $ opkg update 
  

3]configure and enable bluetooth device 
 
step1: make sure your board is updated to bluetooth stack(bluez 4.0 or above) 
  (this step neccarry to communicate with BLE's,if you re not worried about BLE's right now.then skip step 1)
 
       step 2:The first step in enabling Bluetooth is to inform your BeagleBone Black that you would like to be ableto use this feature. 
To do this you will just need to make a quick edit to a configuration file.
   On beagle bone terminal ,If you perform:
    
  $cat /var/lib/connman/settings
  You should see something like:
   [global]
   OfflineMode=false

   [Wired]
   Enable=true

 step3:now,here if on your window bluetooth and wifi are listed,it's good.if not then its great..so edit the configuration file,
 we are using VIM editor(inbuilt editor),
  to do this type following command on BBB's terminal 
 
               $vim /var/lib/connman/settings 
         
       step 4: once a new open ups, press i ,to insert values to file,and add following two lines 
  
  [Bluetooth]
  Enable=true
  then press esc.

 step 5: once both lines are added, press q: this will bring command line of vim,
then type exit and press enter to come back to the BBB's main terminal  
  
      step6: enter command reboot to restart beagle bone
 
 step7: enter into beagle bone's terminal again .

        Step8: run hci commands to check if the bluetooth is enabled or not
           ex::   $hcitool dev:
                 devices = xx:xx:xx:xx:xx
                   the BID of connected bluetooth device is printed
 
                congradulation  now you have ethernet and bluetooth,working on your BBB
note:how to enable wifi ob BBB will soon be posted 

Thursday, February 28, 2013

PIR sensor


General Description
The PIR (Passive Infra-Red) Sensor is a pyroelectric device that detects motion by measuring changes in
the infrared levels emitted by surrounding objects. This motion can be detected by checking for a high
signal on a single I/O pin.

Calibration
The PIR Sensor requires a ‘warm-up’ time in order to function properly. This is due to the settling time
involved in ‘learning’ its environment. This could be anywhere from 10-60 seconds. During this time
there should be as little motion as possible in the sensors field of view.

This is the explanation of program written in pir_test.ino
PIN connection
Vcc(PIR)--3.3V(Arduino)
GND(PIR)--GND(Arduino)
OUT(PIR)--pin7(arduino)
by:moin
creaed :@date 28-02-2013 @time 6:22


Since PIR, sends 'HIGH' signal for 3sec(vary with devices), just after sending a LOW signal for 2sec(vary with devices),
to check motion, this phenomenon of PIR is interpreted as positive motion lots of time by the compiler.
And hence cause wrong results many times. to over come this problem ,we need to specify a time condition,
so that if and only if that condition is satisfied, our compiler should consider that signal as a positive motion

to this we first need to calculate time at which PIR is sending 'LOW’ signal,
to do this I have written a if loop as shown below ,
this program calculates the time at which PIR is LOW, and prints the value to serial com .
                                   


                                    if(digitalRead(pir)==LOW){
                                      offtime=millis();//stores time at which pir pin turned low
                                      digitalWrite(led,LOW);//pir status is 
                                      Serial.print("\n offtime");
                                      Serial.println(offtime);
                                      delay(100);
                                    }


to this we first need to calculate time at which PIR is sending 'HIGH’ signal,
to do this I have written a if loop as shown below ,
this program calculates the time at which PIR is 'HIGH’, and prints the value to serial com .

                                    else if(digitalRead(pir)==HIGH){
                                      ontime=millis()-offtime;//store's time at which pir was high**
                                     digitalWrite(led,HIGH);//pir status on led
                                      Serial.print("\n ontime:");
                                      Serial.println(ontime);
                                      delay(200);
                                        }
imp:: ontime=millis()-offtime;
      here int above statement ontime is calculated by [millis()-offtime] formula,
            because millis(),gives the total time taken by compiler since the program is started.
            so if you can see in the first if loop , we are using [offtime=millis();]millis();direct to store the time at which
            PIR signaled LOW, so by applying simple math’s we can understand that the above formula can give us ontime.


Now after calculating time for which PIR is HIGH, we need check if PIR is high for minimum 5sec to confirm motion,

                                     if(ontime>condition){
                                      Serial.print("\n motion sensed");
                                      delay(1000);
                                    }
                                   
                                    if(ontime<condition){
                                      Serial.print("\n motion not sensed");
                                      delay(1000);
                                     }
Condition is initialized to 5000;
                                    long condition=5000;
this program is working as expected. and here is the program copy and paste it to arduino platform
//@dat 28-feb-2013
//by moin
*************//working sketch for pir*************************************************
#define pir 7
#define led 12
//int pir_state =LOW;
long offtime=0;//make sure it is initialized to zero**
unsigned long ontime;
long condition=5000;//should saty high to confirmmotion**
void setup(){
  pinMode(pir,INPUT);//make pir pin as input to micro controller
  pinMode(led,OUTPUT);//make led to show pir status
  Serial.begin(9600);
  //time requierd by PIR to callibrate
  Serial.print("calibrating sensor\n ");
    for(int i = 0; i < 30; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE\n");
    delay(50);
}
void loop(){
  //check if pir pin is low and store the time at which it is low

  if(digitalRead(pir)==LOW){
      offtime=millis();//stores time at which pir pin turned low
      digitalWrite(led,LOW);//pir status is  
     // Serial.print("\n offtime");
      //Serial.println(offtime);
      delay(100);
    }
    
    //check if pir pin is high and store time 
    else if(digitalRead(pir)==HIGH){
    ontime=millis()-offtime;//store's time at which pir was high**
    digitalWrite(led,HIGH);//pir status on led
    //Serial.print("\n ontime:");
   // Serial.println(ontime);
    delay(200);
  }
  //check if led was high for 5 sec**
  if(ontime>condition){
    Serial.print("\n motion sensed");
    digitalWrite(led,HIGH);
    delay(1000);
  }
   if(ontime<condition){
    Serial.print("\n motion not sensed");
    delay(1000);
  }
}
*********************************************************************************  
    


Thank you for Reading.

Sunday, February 10, 2013

configuring cc2500 to transmitt and recieve data to atmegaxx

 CC2500 Serial Transreceiver Wireless Module is designed to meet the requirement for the low cost, low power wireless device to transmit and receive serial data. The module operates on 2.4 GHz frequency band. The module can also be used as Wireless Sensor Network (WSN) node.

Hey folks this is about how to configure a cc2500 RF .to recieve and transmitt data to and from a atmega micro.
1.Download this smart rf softwere to get start with it.
http://focus.ti.com/docs/toolsw/folders/print/smartrftm-studio.html


2.Install and run the program .
it looks something like this

3.Now click on 2.4 ghz.you will find a list of components select cc2500 from it.


4.Now check on the right most part of the tool you will see a list of register of cc2500,
and here is the the thing we need to configure these registers to configure  our cc2500 to act as a transreciever.
so lets just beging with conguration now.
NOTE: To enter a value for perticular register just click on the value given infront of that register
here the list of the register and their corresponding value in the table given below
IMPORTANT :Setting the register bit PKTCTRL0.PKT_FORMAT=1 in the CC2500 radios enables
serial synchronous mode.
for my cc2500 i hve used GDO2 as clock and GDO0 as output.refrence value is given in the table below
 

//To check what particular register is doing ,simply double click on the register about which you want to know//
5. save your work by clicking on file-save-cnfig  we need this file in future so keep it safe.

6
  To know weather we are sending or recieving data.we got status resisters ,here is the list of status available in cc2500
 Status byte summary

NOTE:
Manchester encoding disabled, the frequency of the clock on the clock pin is equal to the programmed bit-rate (MDMCFG4.DRATE_E and MDMCFG3.DRATE_M register fields) in the radio.
For example, if the programmed data rate is 10 kbps, the frequency of the clock coming out of the clock pin will be 10 kHz.
Enabling Manchester encoding (available for 2-FSK, GFSK, ASK/OOK modulations) makes
the transmit/receive data rate half the programmed data rate.i.e it transmitts data for positive clock only and remains shut for the other.

here is the program for cc2500 and atmega


spi control register SPCR