Vadim Tabakman

Jump to bottom

HC-SR501 Motion Detection Sensor 

HC-SR501 Motion Detection Sensor

Yet another sensor to play with.  This time a Motion Detection sensor.  This has actually taken to me a different stage of my learning.  So far, I've been using Python code that polls the sensors to get data from.  But I realized that there must be a better way, because by polling, there's a change I'm missing out on events.  The thing I found was Triggers.

The sensor looks like this :

Kind of reminds of the room sensors you see in shops you walk into or even ones you might have at home with an alarm system.

Polling

#!/usr/bin/env python

import os
import RPi.GPIO as GPIO
import time

pin = 6

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)

print "Press Ctrl+c to exit"
time.sleep(2)
print "Ready"

try:
   while True: 
      if (GPIO.input(pin)==1): 
         print "Motion Detected"
      time.sleep(1)
except KeyboardInterrupt: 
   print "Program Cancelled..." 
   GPIO.cleanup()

Trigger

#!/usr/bin/env python

 

import os
import RPi.GPIO as GPIO
import time

pin = 6

GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.IN)

print "Press Ctrl+c to exit"
time.sleep(2)
print "Ready"

def MOTION(pin):
   print "Motion Detected!"

try:
   GPIO.add_event_detect(pin, GPIO.RISING, callback=MOTION)
   while True: 
      time.sleep(100)
except KeyboardInterrupt: 
   print "Program Cancelled..." 
   GPIO.cleanup()

There's not a huge difference.  But it's all about the Callback function called MOTION.  It's registered by using the GPIO.add_event_detection function.  We are telling it that on that pin that the sensor is connected, when we get a Rising edge event, you need to call MOTION.

Not that hard right?  This is going to make for some interesting apps now that my app doesn't have to sit in a loop, constantly polling a sensor and when an event occurs, my code has to stop what it's doing to process it.  In this case, the Callback function MOTION would be called on another thread behind the scenes, and my app can continue on it's merry way.

If you want to check out the code, it's available in the download section.

Downloads

Download the HC-SR501Sensor scripts - this is zipped.  So unzip it when you want to run it.  To run it :

python HCSR501SensorPoll.py

or

python HCSR501SensorTrigger.py

Posted by Vadim Tabakman Tuesday, January 17, 2017 10:29:00 PM Categories: HC-SR501 Motion Detection Python Raspberry PI

KY017 Mercury Sensor 

KY017 Mercury Sensor

Starting to get the hang of plugging these sensors in.  I wanted to do a quick post about the KY017 Mercury Sensor.

I found that it wasn't documented well and even the sensor itself, I wasn't sure which was Gnd, which was data and power.  Given that one pin said '-', I assume that is ground (see I'm learning).  The right most pin said 'S'  But I plugged that into my GPIO13 pin.  Therefore the middle pin, is power so I plugged that into the '+' line.  

What confirmed that it worked, is that I actually plugged into my breadboard with the jumpers.  That means I can move the sensor around.  When the little ball of mercury hit the right side (closest to the board), a light light up on the sensor.

Sensor Pointing Under Horizontal

Sensor Pointing Over Horizontal

Jumpers Plugged In

Notice the Black jumper on the right and it's plugged into the '-' line.  That plugged into the '-' on the sensor.  The Yellow jumper is plugged in the middle pin on the sensor.

Python Code

#!/usr/bin/env python

import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.IN)
GPIO.setup(27, GPIO.OUT)

try:
   while True: 
      if (GPIO.input(13)==0): 
         print "Tilt"
         GPIO.output(27, True)
      else: 
         print "Level" 
         GPIO.output(27, False)
except KeyboardInterrupt: 
   print "Program Cancelled..." 
   GPIO.cleanup()

The Python code, as you can see above has something plugged into GPIO27 for output.  That's a Piezo thing I plugged on.  So basically, when the mercury sensor points down, the Piezo makes a clicking sound,.

Downloads

Download the KY017Sensor.py script - this is zipped.  So unzip it when you want to run it.  To run it :

python KY017Sensor.py

Posted by Vadim Tabakman Tuesday, January 17, 2017 1:12:00 PM Categories: GPIO KY017 Mercury Sensor Python Raspberry PI

DHT11 Temperature and Humidity Sensor 

DHT11 Temperature and Humidity Sensor

I think after doing the Hello World example with a Raspberry Pi and getting an LED to light up, the next thing to do is work on an Input sensor.  The DHT11 Temperature and Humidity sensor was the obvious target.

It has 3 pins that I plugged into my breadboard.

1. Gnd - I connected that to the - line on the breadboard

2. Data - I connected that to GPIO026

3. VCC - I connected to the + line on the breadboard

As soon as I did that, a light lit up on the sensor.  That means I've connected it correctly.. Right?? haha.

Next step was to get some Python code that can talk to the sensor.  You are probably wondering why "get" and not just write it myself.  Firstly, I'm still learning about all this sensors/GPIO stuff and at the same time, I'm learning Python.

A huge thanks goes out to Darius Juodokas (netikras) from Lithuania.  I grabbed his code from GitHub.

I had to change it a little to display the temp in Fahrenheit, but I was pleasantly surprised at the fact that it just worked.

Results

When you run it, it goes and gets the data from the sensor around 1,000 times, then calculates averages.  This should get rid of any anomalous readings.

The output looks like this :

H: 18%

T: 69.8F

Now you know what I have my office set to.

note:  there is also some text "Hello World" that pops up.  That's just for me.. feel free to get rid of that.

Downloads

Download the DHT11Sensor.py script - this is zipped.  So unzip it when you want to run it.  To run it :

python DHT11Sensor.py

Posted by Vadim Tabakman Tuesday, January 17, 2017 12:07:00 PM Categories: DHT11 Humidity Python Raspberry PI Temperature

Raspberry Pi visible to Windows 

Raspberry Pi visible to Windows

I had a bunch of copy and pasting I wanted to do to my Pi from my Windows pc.  Obviously, by default, the Pi is locked down and isn't visible on the network.  To get it be visible, I had to install Samba and tweak the config and also set a password.

Here are the steps I took:

sudo apt-get install samba

sudo apt-get install samba-common-bin

sudo nano /etc/samba/smb.conf

Make some changes to this conf file.

workgroup = WORKGROUP

wins support = yes

add the following to the bottom of the conf file.

[PiShare]

   comment = Pi Home

   path=/home/pi

   browseable=Yes

   writeable=Yes

   only guest=no

   create mask=0777

   directory mask=0777

   public=no

Finally, run this command and enter the password.

sudo smbpasswd -a pi

Posted by Vadim Tabakman Tuesday, January 17, 2017 11:58:00 AM Categories: apt-get Raspberry PI Samba

Python to REST Nintex Workflow Cloud External 

Python to REST Nintex Workflow Cloud External

Although this post is specifically about the Raspberry Pi, I'm putting it in here, because this is going to be my start into writing some Python code to make a REST call.  Now it doesn't haven't to be Nintex Workflow Cloud, but since I do a lot of work around Nintex, this was the easiest thing for me to do.  Since I didn't have to go and build a REST service.  Instead, I build a workflow in Nintex Workflow Cloud that had an external start (REST end point).

I also wanted to see if I could write the Python script in the simplest possible way so that it's easy for everyone to understand.

Nintex Workflow Cloud

I know some of you may not know what Nintex or Nintex Workflow Cloud is.  Check it out.  It's quite amazing in letting you automate business processes with an easy to use drag and drop designer.  In this example, I created a simple workflow and configured it have an External Start with two parameters.

This is what the External Start settings look like :

Notice the two parameters, EventName and EventAmount.  These are going to be two JSON parameters we will be able to set from the Python script.

Next, we have a simple Send an Email action:

Finally, once you publish this workflow, you get all the REST information you need to then plug into the Python script.

 

Here are the minimum requirements I had.

  • REST Url
  • JSON data (payload)
  • Headers

# This is what the Python looks like.

import requests

url = 'https://........'

data = '{\

   "startData": {\

      "se_event_amount1": 20.4,\

      "se_event_name1": "Intruder alert"\

   },\

   "options": {\

      "callbackUrl": ""\

   }\

}'

headers = {'content-type': 'application/json'}

response = requests.post(url, data=data, headers=header)

print response.text

Now I saved this file as "nwcStart.py" and I was able to easily run this to make sure it works, using "python nwcStart.py".

Conclusion

This is just the beginning.  I wanted to see how easy it was to make a REST call from Python, since the aim will be for this type of script to be run when a sensor on the Raspberry Pi, senses something we are interested in.

I hope this helps.  If you have any questions, let me know.

Posted by Vadim Tabakman Wednesday, January 4, 2017 10:56:00 AM Categories: Nintex Nintex Workflow Cloud Python Raspberry PI REST

Getting an LED to Light Up 

Getting an LED to Light Up

I'll be the first to admit that it's been many many many years since I've done anything to do with electronics and I can't read the simplest electronics diagram if my life depended on it.  This is actually one of the reasons I'm messing around with the Raspberry Pi.  I want to keep my brain active since learning another language (spoken.. not programming) is beyond my limitations.  "Limitations are the shackles we bind to ourselves".  Who said that??? - I'm showing my grandpa'dness.

Last night I was googling around all over the place to get my head around how to get a simple LED to light up using the Raspberry Pi.  It doesn't sound hard, and it's probably the first thing you should do.  It's like the Hello World of electronics.

Firstly, I should tell you what my set up is.

  • Raspberry Pi 2
  • Breadboard
  • T-Type GPIO expansion board
  • Ribbon cable to connect the expansion board to the Raspberry Pi.
  • Red LED
  • 470ohm Resistor
  • 3 jumper leads (red, white an black)

Those are the minimum that I found was needed to get this to work.  I'm going to try to explain why it plugged in the way it is, but I want you to take note that I'm learning here.  So it's possible I'm doing something wrong (hopefully I won't electrocute myself).  If you see I'm doing something wrong or don't get it, let me know.  I'm keen to understand this stuff better.

Making sure there's Power and Ground to the Breadboard

I'm making this the first part of this post, because this is what I didn't know and I woke up this morning with an epiphany.

Feel free to click on the above image, to get a closer look.

Firstly, when you plug in the T-type expansion board into the breadboard (with the ribbon), I was under the wrong impression that somehow the left and right side of the board now had power and ground all connected up, under the covers.  That was a wrong assumption.

As you can see, I have a White jumper connected to Pin 1 (3v3 power) and it goes to the + (red) column.  That sends power down that column, so you can plug things into it now.

I also have a Red jumper that connects the Gnd (ground) pin to the - (blue) column. This is what I was missing as I thought I didn't need to do this.

LED

The LED you can pretty much put anywhere, as long as it's not on the same row.  The rows, being the number set of holes.  What I mean, is that your LED can't take up pins D35 and E35.  One row needs to be for the positive (anode) and the other for the negative(cathode).I've put my Red LED with the anode on E33 and the cathode on E34.  So now my row 34 is the negative (or ground) and row 33 is the positive (power).

For those who are learning, like me, the anode (+) is the long leg of an LED and the cathode (-) is the shorter leg.

Resistor

I've read a bunch about how you shouldn't plug the LED directly to the power with a resistor, because it will try to drag in as much power as possible and burn itself out.  I actually did connect it up without a resistor and it worked, but I don't recommend this as I did it while I had no idea what I was doing.  Which I still feel is the case :).

I took the 470 ohm resistor that came with my kit, but you can use a smaller one since we are just dealing with an LED.  One of the ends of the resistor is plugged in to the cathode row.  In the picture, it's plugged into A34.  The other end of the resistor is plugged into the - line (to ground it).

GPIO17 pin

The final electronics piece of the puzzle, is connecting the LED to one of the GPIO pins, because I want to write some code to talk to it.

In the image above, I have a Black Jumper connected to the anode part of the LED (pin C33.. it could also be A33, B3 or D33)  The other end of the jumper is connected to B6 which is the GPIO17 pin.

Code to turn the LED on and off

Since I'm running Raspbian (linux) on the Raspberry Pi, the simplest way to communicate with the pins is using Python.  Now I'm not a Python guru.  I'm learning it as I go, so it's not going to be pretty. 

Turning on the LED

import RPi.GPIO as GPIO

pinNum = 17

GPIO.setmode(GPIO.BCM)

GPIO.setwarning(False)

GPIO.setup(pinNum, GPIO.OUT)

GPIO.output(pinNum, GPIO.HIGH)

Turning off the LED

import RPi.GPIO as GPIO

pinNum = 17

GPIO.setmode(GPIO.BCM)

GPIO.setwarning(False)

GPIO.setup(pinNum, GPIO.OUT)

GPIO.output(pinNum, GPIO.LOW)

Conclusion

I don't think this is too complicated, but like I said, I'm still learning and I could be doing something wrong or describing it incorrectly.  Please let me know if either is true as I want to understand this stuff and I also want to put it in a way for other beginners/newbies like myself to learn it easily.

A little addon to this. I installed Ninja-IDE to write my Python.  It seems pretty good for my needs.  That way, I can write the Python code and it gives me an idea if I totally messed something up, like syntax.  Then I save the files, eg ledON.py and ledOFF.py and I can run them with something like this:

python ledON.py

and

python ledOFF.py

Posted by Vadim Tabakman Wednesday, January 4, 2017 7:58:00 AM Categories: Breadboard GPIO GPIO17 Jumpers LED Ninja-IDE Python Raspberry PI Resistor T-Type Expansion

Updating your Raspberry Pi 

Updating your Raspberry Pi

It's been a while since my Unix days, so I feel like I'm learning all over again.

Updating the Raspberry Pi is quite simple.

sudo apt-get update && sudo apt-get upgrade

That goes and downloads all the updates for what you have installed.  In some cases, it will stop and ask if you want to continue.  You just need to press Y and then <enter>

Posted by Vadim Tabakman Tuesday, January 3, 2017 8:16:00 PM Categories: apt-get Raspberry PI

Minimizing Hardware - Remote Desktop 

Minimizing Hardware - Remote Desktop

Keeping this a short and sweet blog post.  Mostly as a reminder to me for later so I don't forget and have to keep looking.

One of the important things to me around the Raspberry Pi, is that it's tiny and it doesn't take up a lot of real estate on my desk or in my office.  Once I have Raspbian installed on an SD card, the first thing I did was to get Remote Desktop working.

Why??  Because that means I don't need to worry about having a monitor, a keyboard and a mouse connected it.  It can be sitting in a corner somewhere and as long as it has power, it'll do what I tell it to do.

I looking online on how to get RDP (remote desktop) working, and installing xrdp almost worked, but I was having some issues after successful login.  Turns out I needed something else.

So here are the commands to run:

sudo apt-get install tightvncserver
sudo apt-get remove xrdp
sudo apt-get install xrdp

Notice that it removes xrdp and installs it again.  That assume you already installed it and are having issues.

This seemed to work for me and hopefully it helps others.

Posted by Vadim Tabakman Tuesday, January 3, 2017 8:09:00 AM Categories: Raspberry PI RDP Tightvncserver XRDP