Ubidots Community

People Counter Raspberry Pi Ubidots 3G Communication PIR Sensor

Hello, I’m working on a people counter bot and I am having trouble with the communication between the Raspberry PI, PIR sensors, and the dashboard platform Ubidots. I am attempting to communicate with the Ubidots platform using 3G communication however I cannot seem to get the Raspberry Pi or Ubidots to communicate via this method? Can anyone help with this?

Greetings, my advice to you is to make sure that your raspberry pi can reach the cloud using the 3G module that you already have, maybe just doing a ping test to www.google.com.

Once you are sure that your device is connected to the cloud, follow the steps at this guide to send values to Ubidots.

All the best

Thanks for your advice however we have not been able to get the sensor to work.

The steps below is as far we can get, can you please help us solve this problem?

Many thanks, Leo

Here are the steps we followed:

  1. This is the code we used for
    https://www.instructables.com/id/How-to-build-a-People-Counter-with-Raspberry-Pi-an/
  2. This code was for Raspberry Pi Type B and we are using Raspberry Pi 3B. This required realigning the PIR input cable GPIO 7 to GPIO 4.
  3. We ran step 1 and the Raspberry Pi response was “Couldn’t connect to the API, check your Internet connection”.

Please note we do have a Internet connection, it seems to be an API issue.

Greetings, it is not an issue of our REST API, if it was, trust me, you would have found a lot of post messages with bug reports. Said this, I see that you are getting a message that suggests that your problem is related with your internet connection rather than with Ubidots. Additionally, the tutorial that you are following is pretty old and outdated, so please follow this updated article that at my side works properly.

All the best

We have been able to connect the Raspberry Pi to Ubidots using the code on the Ubidots site.

However, when we tried to use the code from the article you linked above, there were various errors in the code which made it unable t be run. We instead decided to combine the connection code with the one from the article into the program below. Can you see if there are any mistakes below?

import requests
import platform
import RPi.GPIO as GPIO
import time

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

DEVICE_LABEL = "PCB"  # Replace by your desired device label
VARIABLE_LABEL = "counter"  # Replace by your desired counter variable label
UBIDOTS_TOKEN = ""  # Add here your Ubidots token


def build_counter(payload):
    counter = 0
    people_count = 0
    while True:
        # Verifies if the sensor is activated by a movement
        presence = GPIO.input(7)
        if(presence):
            print("presence detected")
            people_count += 1
            presence = 0
            time.sleep(1.5)
            counter += 1
        time.sleep(1)
        if(counter == 10):
            print("counter value: {people_count}, sending data ...")
            req = send_data_to_ubidots(UBIDOTS_TOKEN, DEVICE_LABEL,
                                       VARIABLE_LABEL, people_count)
            print("requests result:\n{req.text}")
            counter = 0
            people_count = 0

return counter

def post_counter(payload):
    # Creates the headers for the HTTP requests
    url = "http://industrial.api.ubidots.com"
    url = "{}/api/v1.6/devices/{}".format(url, DEVICE_LABEL)
    headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}

    # Makes the HTTP requests
    status = 400
    attempts = 0
    while status >= 400 and attempts <= 5:
        req = requests.post(url=url, headers=headers, json=payload)
        status = req.status_code
        attempts += 1
        time.sleep(1)

    # Processes results
    if status >= 400:
        print("[ERROR] Could not send data after 5 attempts, please check \
            your token credentials and internet connection")
        return False

    print("[INFO] request made properly, your device is updated")
    return True


def main():
    payload = build_payload (VARIABLE_LABEL)

    print("[INFO] Attemping to send data")
    post_request(payload)
    print("[INFO] finished")


if __name__ == '__main__':
    while (True):
        main()
        time.sleep(1)

Please make sure that you are copying and pasting properly the example code from the article, at my side it works properly:

All the best