Software

Ubidots

Using your laptop, set up an ubidots account and create a widget with a device and variable. Here is a video showing how https://youtu.be/jlfhIFsJUCk. Make sure to write down your variable id and your API token (which is listed under API Credentials under your username). Leave your ubidots account open on your laptop in a browser window because you will come back to it later.

Code

Create a folder on the desktop of the Raspberry Pi called PeopleCounter. This will make the code easier to find later if you need to troubleshoot or update it.

Create a file called peoplecounter.py and copy/paste in the code from the following file

from ubidots import ApiClient
from RPi import GPIO
import time
GPIO.setmode(GPIO.BOARD)
#Make sure this value matches the pin number on the raspi
pir = 26
GPIO.setup(pir, GPIO.IN)

try:
  #Replace the xxxx with your actual API token
  api = ApiClient(token='XXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
  #Replace the XXX with your actual variable ID
  people = api.get_variable("XXXXXXXXXXXXXXXXXXXXXXXXX")
except:
  print("Couldn't connect to the API, please check your Internet connection.")

peoplecount=0

time.sleep(2)

while(True):
  if (GPIO.input(pir)):
    peoplecount = peoplecount+1
    time.sleep(2)
    response = people.save_value({"value":peoplecount/2})
    print response

time.sleep(0.1)

Remember that indentation is extremely important with python code. You will want to double check all your indentation after copy/pasting to make sure that nothing got out of alignment.

After you create your peoplecounter.py script in your PeopleCounter folder on the desktop of your Raspberry Pi, you will need to open peoplecounter.py and replace the XXXs with your Ubidots API token token and variable id. These are available in your Ubidots account. If you need help finding your variable id and token, re-watch the video from the Ubidots section of the instructions.

Test the Ubidots connection

Open a terminal window on the raspberry pi and walk through the setup instructions here https://ubidots.com/docs/devices/raspberrypi.html#setup. Honestly, these Ubidots tests don't always work for me. So complete the installations, then try the tests. If you're not seeing any activity on your ubidots account, move on to the next step. If you're still not seeing any activity on your Ubidots account in the browser window on your laptop, try double checking the indentation on the python script and the variable numbers. If even the installations don't work, make sure you are connected to the Internet.

Test the code and sensor work together

Open a terminal window on the raspberry pi and type the following command to change directories into your PeopleCounter folder (don't type the dollar sign $, it's just there to show the beginning of the command line interface, aka CLI.)

$ cd Desktop/PeopleCounter

Now that you are working within the PeopleCounter folder, type the following into the CLI to test that your sensor and code work together (once again, don't type the dollar sign $, it's just a standard way to indicate in instructions that we are typing CLI commands.)

$ python peoplecounter.py

This command will start the peoplecounter script running. Let that continue to run and position the PIR sensor so that it is somewhat hidden from accidental triggering. Test the sensor by waving your hand in front of it (some brands of PIR sensors will light up red to indicate when they are active, others remain unlit even when actively taking readings). Now, wait a few seconds while you watch your ubidots account in the browser on your laptop. If everything is working, you should see some activity on your account! Here is a video demonstrating what the ubidots dashboard will display when activity is happening https://youtu.be/KlHLQQomxuc

Enable startup script with rc.local

Up until now we have been plugging in a keyboard and typing commands to run our script peoplecounter.py . However, when the oshiot:: Door Counter is attached to a door in a library somewhere, we want our motion sensor program to start automatically everytime it gets plugged in. In order to get the peoplecounter.py script to run automatically on startup, we will follow these instructions to add the name of the script to the rc.local file on the raspberry pi. Since rc.local runs on startup, it will call peoplecounter.py each time the device powers up. Be sure to bifurcate the command by adding an ampersand symbol as stated in the following instructions https://www.raspberrypi.org/documentation/linux/usage/rc-local.md

After you complete editing the rc.local file in nano, hit CRTL+x to exit, then hit Yto confirm you are saving your changes, then hit ENTERto leave the nano editor. If you get an error stating

Error Writing etc/rc.local : No such file or directory

then you need to try saving again, but this time after you hit Y, use the left arrow key to place the cursor before the contents the file path and add a forward slash at the beginning!

Test the startup script by rebooting the raspberry pi and seeing if the peoplecounter.py script begins automatically upon startup.

Enable startup script with lxsession

The rc.local method worked fine for me on my OSHIOT Door Counters made with Raspberry Pi 2. A colleague of mine, Dan Brower, noticed a bug where this method didn't work on door counters build with Raspberry Pi 3. I repeated his steps and confirmed the bug happened to me also when using a Raspberry Pi 3 to build an OSHIOT Door Counter. The following is the way to enable your script to run on startup if the above rc.local method does not work for you!

After you confirm that your code and sensor work together to create activity on your ubidots account, open a terminal window and type

$ sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

(Remember do not type the $, it's just to indicate you are typing in the terminal).

This should open a file in edit mode. Use your keyboard's down arrow to get to the bottom of the file. Add the following line

@sudo python /home/pi/Desktop/PeopleCounter/peoplecounter.py

Save the changes and close the file by hitting CTRL x then Y then enter .

Now reboot by typing $ rebootin the terminal window and watch your ubidots account closely. It should begin showing activity as the Raspberry Pi starts up again.

results matching ""

    No results matching ""