Skip to main content

reCamera connects to XIAO via HTTP

This document demonstrates how to enable communication between the reCamera and XIAO via HTTP, transmitting data to XIAO so that you can integrate the reCamera into your own projects.

Preparation

reCamera

HTTP API and Network connection

Firstly, prepare your reCamera's C++ supervisor project and the running environment.

Secondly, reCamera and XIAO must be on the same 2.4GHz local area network (LAN) to communicate. ReCamera has an internal wireless network card that can connect to WiFi. Typically, you can connect to WiFi through the camera's Node-RED web. However, when running a custom C++ project, you must disable Node-RED and restart reCamera.(Reference: Seeed Studio Wiki). Therefore, we need to establish the WiFi connection via the Linux terminal.

cd /etc/
ls

You can see the wpa_supplicant.conf :

You can also view it in the left file directory:

Drag the file(s) to the desktop and open it with Notepad.Add the network as shown in the image, and change it to your SSID and password.

Delete the original file in reCamera terminal: /etc/.

sudo rm wpa_supplicant.conf

Open your PC windows powershell and copy the file to the reCamera terminal : /home/recamera/:

scp "C:\Users\{your username}\Desktop\wpa_supplicant.conf" [email protected]:/home/recamera

Copy the file from /home/recamera/ to : /etc/:

sudo scp wpa_supplicant.conf /etc/

Restart the reCamera.Then, run the program.

sudo ./Supervisor_add_detection_http

Ensure your reCamera is connected to your WiFi network.

XIAO

Network connection and send HTTP requests

It is necessary to download Arduino to flash the program for XIAO (ESP32-C3).

Step 1. Download and Install the latest version of Arduino IDE according to your operating system.Launch the Arduino application.

Step 2. Add ESP32 board package to your Arduino IDE Navigate to File > Preferences, and fill "Additional Boards Manager URLs" with the url below: https://jihulab.com/esp-mirror/espressif/arduino-esp32.git

Navigate to Tools > Board > Boards Manager..., type the keyword "esp32" in the search box, select the latest version of esp32, and install it.

Step 3. Select your board and port

Board Navigate to Tools > Board > ESP32 Arduino and select "XIAO_ESP32C3". The list of board is a little long and you need to roll to the buttom to reach it.

Port Navigate to Tools > Port and select the serial port name of the connected XIAO ESP32C3. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports).

Step 4. Write the following program and click the upload button to compile and upload. The reCamera and XIAO must be on the same 2.4GHz local area network (LAN) to communicate.

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "{Your wifi name}";
const char* password = "{Your wifi password}";
const char* apiUrl = "http://{Your reCamera wlan0 IP}/modeldetector"; //You can replace "modeldetector" with your project interface.
//You can check the reCamera IP address of wlan0 using 'ifconfig' in the terminal.

void setup() {
Serial.begin(115200);
//network connection
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}

void loop() {
if (WiFi.status() == WL_CONNECTED) {
//send HTTP requests
HTTPClient http;
http.begin(apiUrl);
http.addHeader("Content-Type", "application/json");

int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("HTTP Response:");
// Serial.println(payload);


DynamicJsonDocument doc(1024); // Adjust the buffer size according to the actual JSON size.
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("JSON Deserialization fail: ");
Serial.println(error.c_str());
}
// Adjust the json key according to the actual JSON content.
Serial.print("Code: ");
Serial.println(doc["Code"].as<String>());
Serial.print("Msg: ");
Serial.println(doc["Msg"].as<String>());
Serial.print("Target: ");
Serial.println(doc["Target"].as<String>());
Serial.print("Score: ");
Serial.println(doc["Score"].as<String>());
Serial.print("Release_duration: ");
Serial.print(doc["Release_duration"].as<String>());
Serial.println("ms");
Serial.print("Capture_duration: ");
Serial.print(doc["Capture_duration"].as<String>());
Serial.println("ms");
Serial.print("Image_preprocessing_duration: ");
Serial.print(doc["Image_preprocessing_duration"].as<String>());
Serial.println("ms");
Serial.print("Detection_duration: ");
Serial.print(doc["Detection_duration"].as<String>());
Serial.println("ms");
Serial.print("Total Duration: ");
Serial.print(doc["Duration"].as<String>());
Serial.println("ms");


} else {
Serial.print("HTTP Get fail: ");
Serial.println(httpCode);
}
http.end();
} else {
Serial.println("WiFi disconnected");
}

delay(5000);
}

Note: Modify to your WiFi name (SSID), password, and reCamera wlan0 IP address.

Wait for the upload to complete,and you can see XIAO has successfully connected to WiFi in the serial monitor.

Running

Ensure your reCamera is running the Supervisor project and has successfully connected to WiFi.

sudo ./Supervisor_add_detection_http

You can see the Json results of HTTP service in the serial monitor of XIAO.

Tech Support & Product Discussion

Thank you for choosing our products! We are here to provide you with different support to ensure that your experience with our products is as smooth as possible. We offer several communication channels to cater to different preferences and needs.

Loading Comments...