Learn how to make HTTP POST requests using the ESP32-CAM board with Arduino IDE to send photos to a server. We’ll show how to post a JPG/JPEG image to a local server (Raspberry Pi LAMP server) or to a cloud server (that you can access from anywhere). The photos will be displayed in a gallery where you can view or delete the photos. To save the images in the server and create the gallery, we’ll use PHP scripts. Show
To build this project, you need to follow the next steps. Follow the LAMP Server or the Hosting Server instructions depending if you want to access the photos locally or from anywhere.
1. Hosting Your PHP ApplicationThe goal of this project is to have a local or cloud server to store and access your ESP32-CAM photos. 1. Raspberry Pi local server: With a Raspberry Pi LAMP server, you can access your images locally (as illustrated below).
Setup Local RPi LAMP Server » 2. Cloud server (Bluehost hosting solution) You also can visualize the ESP32-CAM photos from anywhere in the world by accessing your own server + domain. Here’s a high level overview on how it works:
Get Hosting and Domain Name with Bluehost » When buying a hosting account, you’ll also have to purchase a domain name. This is what makes this project interesting: you’ll be able to go your domain name (http://example.com) and see your ESP32-CAM photos. If you like our projects, you might consider signing up to Bluehost, because you’ll be supporting our work. HTTP POST Request MethodThe Hypertext Transfer Protocol (HTTP) works as a request-response protocol between a client and server. Here’s an example:
HTTP POST is used to send data to a server to create/update a resource. For example, publish an image to a server.
2.1. Preparing Your .php Files and uploads Folder (Raspberry Pi LAMP Server)This section prepares your .php files and uploads folder for your Raspberry Pi LAMP Server. If you’re using your own server + domain name, skip to the next section. Having a Raspberry Pi running Apache and PHP, in the Raspberry Pi board terminal window navigate to the /var/www/html/ directory:
Create a new folder called uploads:
At the moment, /var/www/html is owned by root, use the next commands to change to the pi user and give it all permissions so that you can save photos using a PHP script later on.
Finally, create a new upload.php file:
This PHP script is responsible for receiving incoming images from the ESP32-CAM, rename the images with a timestamp and store them in the uploads folder. Edit the newly created file (upload.php) and copy the following snippet:
View raw code Your upload.php file should look like this. Save your file and exit (Ctrl+X, Y, and Enter key): Then, create a new gallery.php file:
Edit the newly created file (gallery.php) and copy the following snippet:
View raw code This PHP script is responsible for displaying the images on the gallery. Your gallery.php file should look like this. Save your file and exit (Ctrl+X, Y, and Enter key): 2.2. Preparing Your .php Files and uploads Folder (Hosting Service)If you prefer to run your server remotely and access the photos from anywhere, you need a hosting account. After signing up for a hosting account and setting up a domain name, you can login to your cPanel or similar dashboard. After that, open the File Manager. Open the “Advanced” tab and select “File Manager“: Then, select the public_html option. Press the “+ File” button to create a new upload.php file and a new gallery.php file. Then, click the “+Folder” button to create the Uploads folder. With the three items created, edit the upload.php file: This PHP script is responsible for receiving incoming images from the ESP32-CAM, rename the images with a timestamp and store them in the uploads folder. Edit the newly created file (upload.php) and copy the following snippet:
View raw code Save your file and exit. Then, edit the gallery.php file and copy the following snippet. This is responsible for displaying the images in the gallery.
View raw code Save your file and exit. That’s it! Your server is ready. Now that you have your server ready (Raspberry Pi LAMP server or cloud server), it’s time to prepare the ESP32-CAM with the code to publish a new image to your server every 30 seconds. Before proceeding with this tutorial, make sure you complete the following prerequisites. Parts RequiredTo follow this tutorial you need the following components:
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price! Arduino IDEWe’ll program the ESP32-CAM using Arduino IDE, so make sure you have the ESP32 add-on installed.
Check the PHP URLYou should try to open the Raspberry Pi local IP address or your external example.com domain name, followed by /upload.php that should return:
If you see that message save your URL/domain name and path, your server should be ready and you can continue with this guide. Additionally, try to access the /gallery.php path. You should get something as shown below: ESP32-CAM CodeThe next sketch posts the image to a server using HTTP POST. Copy the code below to your Arduino IDE.
View raw code Before uploading the code, you need to insert your network credentials in the following variables:
Make sure you select the right camera module. In this case, we’re using the AI-THINKER Model. If you’re using another camera model, you can read this Guide ESP32-CAM Camera Boards: Pin and GPIOs Assignment. Add your Raspberry Pi IP address or use the server domain name:
Upload Code to ESP32-CAMNow you can upload the code to your ESP32-CAM board. Connect the ESP32-CAM board to your computer using an FTDI programmer. Follow the next schematic diagram: Many FTDI programmers have a jumper that allows you to select 3.3V or 5V. Make sure the jumper is in the right place to select 5V. Important: GPIO 0 needs to be connected to GND so that you’re able to upload code.
To upload the code, follow the next steps:
After a few seconds, the code should be successfully uploaded to your board. If you have troubles uploading the code, read our ESP32-CAM Troubleshooting Guide. How the Code WorksHere’s a quick explanation on how the code works:
The sendPhoto() function is the part that actually takes a photo and sends it to your server. You can use that function in other of your projects that require taking and publishing a photo to a server. 4. Testing and Final DemonstrationAfter uploading the code to your board, open the Arduino IDE Serial Monitor and you should see a similar message being printed every 30 seconds:
If you go to your local server URL http://IP-Address/uploads, or to your cloud server URL http://example.com/uploads you should have a folder with all your stored photos. You can open each link to open a new page with the full image: Now, if you go to your local server URL http://IP-Address/gallery.php, or to your cloud server URL http://example.com/gallery.php, you can access the gallery page, where you can view and delete the photos. To delete any photo, just click on the “Delete file” link next to each image. Wrapping UpThat’s it! Now, you can send your ESP32-CAM photos to any server using HTTP POST. Modify this project to best suit your needs, for example take a photo and send to a server when motion is detected. Other ESP32 tutorials you might be interested in:
Learn more about the ESP32-CAM:
Thanks for reading. How do you send an image over HTTP in Python?import socket.. import time.. HOST = 'data.pr4e.org'. PORT = 80.. mysock = socket. socket(socket. AF_INET, socket. SOCK_STREAM). mysock. connect((HOST, PORT)). mysock. sendall(b'GET http://data.pr4e.org/cover3.jpg HTTP/1.0\r\n\r\n'). Does HTTP transfer images?Yes. That is how you can transmit any file. Everything is composed of bytes, and every protocol (including HTTP) is essentially an elaborate way to transmit, receive, and interpret bytes.
How do I pass an image into API?Send an Image to an API. Add an Image Element to a page in your app. ... . To send the image as a Base 64 encoded string, your app will need to encode the image and store it in a variable to send with the API request. ... . Add the API request that receives the image.. How do you send a photo in a post request?Option 1: Direct File Upload , From this method you can select form-data and set the type to file. Then select an image file by clicking on the button shown in the value column. The content type is automatically detect by postman but if you want you can set it with a relevant MIME type.
|