This is an old revision of the document!
* Student: Zoican Denis-Alexandru
The goal of this project is to alert the system's owner when an object approaches the system. When this happens, a photograph of the object is taken, and the user can view the image from any network-connected device.
The project was developed on 2 different boards: ESP32 and Raspberry Pi 4.
The first part of the code is the setup part where I define the variables, constants, used libraries and the logic in the setup function.
#include "WiFi.h" #include <Arduino.h> #include <HTTPClient.h> const int buzzerPin = 12; const int trigPin = 5; const int echoPin = 18; //define sound speed in cm/uS #define SOUND_SPEED 0.034 #define CM_TO_INCH 0.393701 #define WIFI_NETWORK "MERCUSYS_DCEE" #define WIFI_PASSWORD "74745814" #define WIFI_TIMEOUT 20000 #define API_URL "http://ip-:5000/upload_photo" long duration; float distanceCm; float distanceInch; void setup() { Serial.begin(115200); // Starts the serial communication pinMode(buzzerPin, OUTPUT); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input connectToWifi(); }
The code is divided in functions for a better understanding of the logic.
The following code demonstrates how the boards connect to WiFi in order to communicate with the Raspberry Pi 4. The board's mode will be set to WIFI_STA, the board being able to connect to an WIFI network. The authentication procedure begins with the use of the network's name and password. If the authentication process does not succeed after 20 seconds (WIFI_TIMEOUT), an error message will be shown. Otherwise, a success message will be shown beside the IP address.
void connectToWifi(){ Serial.println("Connecting.. status: " + String(WiFi.status())); WiFi.mode(WIFI_STA); WiFi.begin(WIFI_NETWORK,WIFI_PASSWORD); unsigned long startAttemptTime = millis(); while(WiFi.status() != WL_CONNECTED && millis()-startAttemptTime < WIFI_TIMEOUT){ Serial.print("."); delay(100); } if(WiFi.status() != WL_CONNECTED){ Serial.println("Failed to connect"); } else { Serial.println("Connected"); Serial.println(WiFi.localIP()); }
The next function's role is to send a POST request to the raspberry pi board's upload_photo endpoint. The endpoint's url is specified in the API_URL constant.
void makeRequest(){ HTTPClient http; Serial.println("Making HTTP request..."); const char* payload = ""; http.begin(API_URL); int httpCode = http.POST(payload); if (httpCode > 0) { String payload = http.getString(); Serial.println("Response:"); Serial.println(payload); } else { Serial.println("Error making HTTP request"); } http.end(); }
The HC-SR04 sensor determines distance by generating sound waves for a set amount of time and then calculating how long it takes for the sound waves to return to the sensor. In order to get the distance received from the HC-SR04 sensor, the project use the following function. The trigger pin is set to HIGH for 10 microseconds, and then we calculate the distance by multiplying the echo pin's value with the sound of speed.
long getDistance(){ // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); return duration * SOUND_SPEED/2; }
The loop function contains the main logic of the project. We will calculate the distance every 1 second and if the distance is smaller than 50 cm, the buzzer will generate a high pitched sound for 5 seconds and the esp32 board will make a POST request to the raspberry pi 4 board. After this, a photo will be taken and will be saved in the database.
void loop() { long distance = getDistance(); if(distance < 50){ tone(buzzerPin, 1000); makeRequest(); // Wait for 1 second delay(5000); noTone(buzzerPin); } delay(1000); }
The code that is used on this board is written in Python. This board will play the role of a server that will store the images in a database. The web server is implemented using the Flask framework and the database is implemented using SQLlite. In order to create the database, I used the following python code where I defined a table with id, name and the image content.
import sqlite3 import os database_path = "./database.db" conn = sqlite3.connect(database_path) cursor = conn.cursor() cursor.execute(""" CREATE TABLE IF NOT EXISTS images ( id INTEGER PRIMARY KEY, name TEXT, image_data BLOB ) """) conn.commit() conn.close()