A secure platform for IoT devices to communicate with a cloud backend using mTLS authentication and OTA updates.
This project consists of two main components:
- IoT Device (ESP32): Securely connects to the cloud backend using mTLS, sends telemetry data, and receives commands and OTA updates.
- Cloud Backend: Provides a secure MQTT broker with mTLS authentication, REST API for device management, and OTA update functionality.
- mTLS Authentication: Mutual TLS authentication ensures both the server and device authenticate each other.
- Certificate-Based Security: Each device has its own certificate for authentication.
- Secure MQTT Communication: All data is encrypted using TLS.
- Secure OTA Updates: Over-the-air updates are verified for authenticity before installation.
/iot-device: Code for the ESP32 IoT device/iofra: Cloud backend server (REST API + MQTT broker)
- ESP32 development board
- Arduino IDE or PlatformIO
- Node.js and npm/pnpm
- MongoDB
- Open the
iot-devicefolder in Arduino IDE or PlatformIO - Configure WiFi settings and server address in
config.h - Generate device certificates (see below)
- Upload the code to your ESP32 device
- Navigate to the
iofradirectory - Install dependencies:
pnpm install
- Create a
.envfile with the following:PORT=3001 MQTT_PORT=8883 MONGODB_URI=mongodb://localhost:27017/iot-platform CERT_DIR=./certs - Generate certificates (see below)
- Start the server:
pnpm dev
For a production environment, you need to generate proper certificates. Here's a basic guide:
# Generate CA private key
openssl genrsa -out ca.key 2048
# Generate CA certificate
openssl req -x509 -new -nodes -key ca.key -sha256 -days 3650 -out ca.crt# Generate server private key
openssl genrsa -out server.key 2048
# Generate server CSR
openssl req -new -key server.key -out server.csr
# Sign server certificate with CA
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650 -sha256# Generate device private key
openssl genrsa -out device.key 2048
# Generate device CSR
openssl req -new -key device.key -out device.csr
# Sign device certificate with CA
openssl x509 -req -in device.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out device.crt -days 3650 -sha256GET /api/devices: Get all devicesGET /api/devices/:id: Get a specific devicePOST /api/devices: Create a new devicePUT /api/devices/:id: Update a deviceDELETE /api/devices/:id: Delete a device
GET /api/devices/:id/telemetry: Get device telemetry dataPOST /api/devices/:id/ota: Initiate OTA update for a devicePOST /api/devices/:id/certificates: Generate certificates for a device
devices/{deviceId}/telemetry: Device publishes telemetry datadevices/{deviceId}/commands: Device subscribes for commandsdevices/{deviceId}/ota: Device subscribes for OTA updatesdevices/status: Device publishes status updates
MIT