This guide provides instructions for setting up the secure IoT system to transmit temperature data from an ESP32 sensor to a Raspberry Pi, where it is stored in InfluxDB and visualized with Grafana.
Ensure the latest versions of the following software are installed on the Raspberry Pi:
- Mosquitto (MQTT Broker)
- InfluxDB (Time-series database)
- Grafana (Data visualization)
- Nginx (Web server for OTA and firmware storage)
- OpenSSL (for generating and managing certificates)
-
Generate the CA Private Key
Generate a 2048-bit private key for the Certificate Authority (CA). -
Create the CA Certificate
Generate a self-signed CA certificate valid for 365 days.
- Generate a 2048-bit private key.
- Create a Certificate Signing Request (CSR) with the Common Name (CN) as "mosquitto".
- Sign the CSR with the CA certificate to create the server certificate.
- Generate a 2048-bit private key.
- Create a CSR with the Common Name (CN) as "influxdb".
- Sign the CSR with the CA certificate to create the
influxdb-server.crt
server certificate.
- Generate a 2048-bit private key.
- Create a CSR with the Common Name (CN) as "ESP32Client".
- Sign the CSR with the CA certificate to create the client certificate.
- Place the
ca.crt
,server.crt
, andserver.key
files in the Mosquitto configuration directory (e.g.,/etc/mosquitto/certs
). - Edit the Mosquitto configuration file (
mosquitto.conf
) to enforce mTLS, specifying paths to the certificate files. - Restart the Mosquitto service to apply changes.
- Place the
influxdb-server.crt
andinfluxdb-server.key
in the InfluxDB configuration directory (e.g.,/etc/ssl/certs
). - Update the InfluxDB service file (
influxdb.service
) to include the certificate paths:- Example configuration:
ExecStart=/usr/bin/influxd --tls-cert /etc/ssl/certs/influxdb-server.crt --tls-key /etc/ssl/private/influxdb-server.key
- Example configuration:
- Restart the InfluxDB service to enable HTTPS.
- Place the
influxdb-server.crt
as the CA certificate in the Grafana Data Source settings. - In the
grafana.ini
file:- Set
protocol = https
. - Add paths to
cert_file
andcert_key
for Grafana’s HTTPS configuration.
- Set
- Restart Grafana.
- Place the
influxdb-server.crt
andinfluxdb-server.key
in the Nginx configuration directory. - Update the Nginx configuration file to specify the certificate and key file paths for OTA updates.
- Restart Nginx to enable secure HTTPS OTA support for the ESP32.
To enable OTA (Over-the-Air) updates for the ESP32, follow these steps:
- Compile Firmware: Build the updated firmware
.bin
file in your development environment (such as PlatformIO). - Transfer Firmware to Raspberry Pi:
- Transfer the
firmware.bin
file to the Raspberry Pi's designated OTA directory:scp path/to/firmware.bin pontusbergstrom@<Raspberry_Pi_IP>:/var/www/firmware/esp32/
- Replace
path/to/firmware.bin
with the path to the compiled firmware on your local machine and<Raspberry_Pi_IP>
with your Raspberry Pi’s IP address.
- Transfer the
- Set Permissions:
- On the Raspberry Pi, set the correct permissions to allow Nginx to access the firmware file:
sudo chmod 644 /var/www/firmware/esp32/firmware.bin
- On the Raspberry Pi, set the correct permissions to allow Nginx to access the firmware file:
- Configure ESP32 for OTA:
- Ensure the ESP32 is configured to check the OTA server on the Raspberry Pi for updates.
- The ESP32 should use HTTPS to securely download firmware files, with server verification via certificates stored in LittleFS.
- Initiate OTA Update:
- Upon detecting a newer firmware version, the ESP32 will download and install the firmware update before rebooting.
Refer to this section when updating the firmware to ensure secure and reliable OTA updates in the system.
- ESP32 to Raspberry Pi Communication: Ensure the ESP32 can securely transmit data via MQTT to the Raspberry Pi.
- InfluxDB HTTPS: Verify that InfluxDB accepts HTTPS requests.
- Grafana Visualization: Check that Grafana can connect to InfluxDB and visualize data over HTTPS.
If any issues arise during setup:
- Ensure permissions on certificate files allow read access for respective services.
- Verify that all server and client certificates are correctly signed by the CA.
- Check firewall settings to ensure required ports are open.