Updated date:

Capture Image With Raspberry Pi and Upload It to Remote Web Server Over the Internet

Tim is currently doing research in Internet of Things (IoT). His desire to spread the concept, ideas, and experience of IoT.


This small project can be useful when you want to capture images from remote location and perform some research on those images. Using the technique described in this article you can create an Internet of Things (IoT) device that sense the environment as in form of image/picture.

I have build a sensor node to monitor a human or animal activity outdoor. Whenever any human or animal comes in the range of sensor node, it captures the image and submit it web server. TO create such sensor node following equipments are required:

  1. Raspberry Pi (3 or latest)
  2. PIR motion sensor
  3. USB Camera

First connect the USB camera to your Raspberry Pi on the any USB port available on board.

Now connect the PIR sensor to Raspberry Pi as shown in following image:

  • VCC to pin 2 (5V)
  • OUT to pin 16 (GPIO 23)
  • GND an pin 6 (ground)


Now you need to have python on your Raspberry Pi. If it is not pre-install, then you can install it as described in this link.

Write or copy paste following python script in the file called capture.py. You can give any name to your python script file.

import RPi.GPIO as GPIO
import subprocess
import time
import requests
# Use BCM GPIO references
# instead of physical pin numbers
# Define GPIO to use on Pi

# url to send image for upload
url = "http://yourserver.com/upload.php"
print "PIR Module Test (CTRL-C to exit)"
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN)      # Echo
Current_State  = 0
Previous_State = 0
  print "Waiting for PIR to settle ..."
  # Loop until PIR output is 0
  while GPIO.input(GPIO_PIR)==1:
    Current_State  = 0
  print "  Ready"
  # Loop until users quits with CTRL-C
  while True :
    # Read PIR state
    Current_State = GPIO.input(GPIO_PIR)
    if Current_State==1 and Previous_State==0:
      # PIR is triggered
      print "  Motion detected!"
      # date time string for file name
      timestr = time.strftime("%Y%m%d-%H%M%S")
      imageFileName = "img-"+timestr+".jpg"
      # capture image
      # send file to server
      files = {'file': open(imageFileName, 'rb')}
      rq = requests.post(url,files=files)
      # Record previous state
    elif Current_State==0 and Previous_State==1:
      # PIR has returned to ready state
      print "  Ready"
    # Wait for 10 milliseconds
except KeyboardInterrupt:
  print "  Quit"
  # Reset GPIO settings

The above python script checks whether the PIR sensor has detected any activity. If any activity has been detected, then the script invoke camera to capture image and then post that image to a PHP file, upload.php, located on remote server. Therefore whenever the python script POSTs image, the PHP page gets invoked at remote server and stores the image to database.

Following are the images of sensor node that I have developed.


At remote server the PHP page receives the image file and store it to database as shown in following code

After saving the python script the 'crontab' in Raspberry Pi must be configured than only the script start automatically at system startup and always keeps running in background. This is required because the script to capture image must be running all the time.

To configure 'crontab' open the terminal and type following command:

sudo crontab -e

Now add the following line at the very end of file:

@reboot sudo python /home/pi/capture.py > /home/pi/capturelog.txt

The above line run python script each time system restarts and keep it running until system is shutdown. It also adds the log to a text file.

Restart the Raspberry Pi, and the python script will automatically starts running as background process.


$dateTime = date("Y-m-d H:i:s");

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$image = $_FILES["file"]["name"];
$insert = $conn->query("INSERT into rpiimage (name, created) VALUES ('$image', '$dateTime')");


You can create a page to view uploaded image on web server and access it from anywhere. I have created a page display.php which displays all images in HTML image tag. Please check my article to display images from database. You may apply certain filters to display images for example view images on particular date.

Following code segment shows how you can display images. It displays all images captured on current day.

$result = $conn->query("SELECT * FROM rpiimage where date(created)='$cur_date' order by created DESC ");
 While($imgData = $result->fetch_assoc()){        
      echo '<div><img width="100%" src="http://yourserver.com/'.$imgData['name'].'"/>';
      echo "<br>". $imgData['created'] . "</div>" ;

This kind of project can also be used to capture images of agricultural fields, green house etc. to monitor crops such as I have done in my research 'Mobile detection of crop diseases for agricultural yield management' to perform disease analysis of crop
or you may use anywhere where IoT based monitoring via images is required. You can also use these captured images for analysis or computation by applying Computer Vision and Machine Learning or Artificial Intelligence.

© 2019 Timothy Malche