0

I have made a smart doorbell and subscribed to a website that does Machine Learning so that I can predict an image when sent to the server.

However, while testing the code using POSTMAN API everything seems to be working very fast and whenever I send the POST I get a request very fast. But, when I come to my Pi 3 and Breadboard, whenever I press a button and fire the POST code, it takes lots of time and there is sometimes huge latency for the reply to come. I am pretty new to GPIO and Python programming and I am learning things as they come. In this scenario I suspect that there is a blocking call that is preventing the reply to come as fast as possible.

What I have is a buzzer, a led and a button. Whenever I press the button, my camera takes a snapshot, the led turns red and the buzzer horns until I remove my finger from the button. Then the image that was saved on the desktop is sent via the PHP file that resides on my server.

If any image or other detail is needed I can provide it.

Below is my python code:

#buttonInput.py

#When the user press the button, this python code runs the buzzer as well as the led light and
#then saves an image via the camera and sends it using a post via PHP to a webpage

import picamera                   
import RPi.GPIO as GPIO
from time import sleep
import time
import os
import requests

#set the broadcom mode
GPIO.setmode(GPIO.BCM)

#the time to sleep after a certain task happens
sleepTime = 0.1

#GPIO pin components
lightBuzzerPin = 4
buttonPin = 17
camera = picamera.PiCamera()

#tie the pins to each ones behavior
GPIO.setup(lightBuzzerPin, GPIO.OUT)
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#set the lightBuzzerPin initially to false so that it does not light up initially
GPIO.output(lightBuzzerPin, False)

try:
    while True:
        while True:
            print("waiting for the press")
            press = GPIO.input(buttonPin) 
            if (press == 1): 
                break 
            print("Light the led and horn the buzzer")
            GPIO.output(lightBuzzerPin, not GPIO.input(buttonPin))
            print("Capturing image of the visitor") 
            camera.vflip = True #set the camera details 
            camera.resolution = (300, 300) #set the image resolution
            camera.capture('/home/pi/Desktop/image.png')
            print("Image captioned and saved")
            files = {'image': open('/home/pi/Desktop/image.png', 'rb')} 
            print("Turning off led and buzzer")
            GPIO.output(lightBuzzerPin, False) 
            print("sending image to server for analysis") #print a message
            sleep(sleepTime) #sleep a bit and give time for ventilation
            r = requests.post('http://mysite.com/smart_door_bell/api/v1/upload_file.php', files=files)
            print("wait for request and print the answer")
            print(r.text) #show the results of the request
        sleep(sleepTime)
finally:
    GPIO.output(lightBuzzerPin, False)
    GPIO.cleanup()
  • 1
    Where is the time being spent? You need to instrument your code to show the timings. At its simplest add a started = time.time() after the break and then print something like time to here is time.time() - started at various points. – joan Dec 12 '17 at 16:33
  • @joan I did as advised, and it turned out that this line ` r = requests.post('mysite.com/smart_door_bell/api/v1/upload_file.php', files=files)` is taking too much time (around 15 seconds). However, if I fire the same web location using POSTMAN (a rest api tool) with the same image, the return is very instant. – tony9099 Dec 12 '17 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.