A secure IoT messaging system that combines LoRa wireless communication with blockchain-based device authentication and message logging.
This project implements a distributed messaging system where:
- ESP32 devices communicate via LoRa radio
- Blockchain smart contract handles device registration and message logging
- Flask server acts as a bridge between devices and blockchain
- Authentication system ensures only registered devices can send trusted messages
┌─────────────┐ LoRa ┌─────────────┐
│ Sender │─────────────│ Receiver │
│ ESP32 │ │ ESP32 │
└─────────────┘ └─────────────┘
│ │
│ HTTP/WiFi │ HTTP/WiFi
│ │
└─────────────┬─────────────┘
│
┌─────────────┐
│ Flask │
│ Server │
└─────────────┘
│
│ Web3
│
┌─────────────┐
│ Blockchain │
│ (Hardhat) │
└─────────────┘
- Device Registration: Manages authorized devices
- Message Logging: Stores messages with timestamps on blockchain
- Access Control: Only registered devices can log messages
- Events: Emits events for device registration and message logging
- API Endpoints:
POST /log: Log messages to blockchainPOST /auth: Check device authentication status
- Blockchain Integration: Interacts with smart contract via Web3
- Auto-registration: Automatically registers new devices
- Sends messages via LoRa radio
- Posts messages to Flask server for blockchain logging
- LCD display for status updates
- WiFi connectivity for HTTP requests
- Receives LoRa messages
- Authenticates sender devices via Flask server
- Displays messages only from authenticated devices
- LCD display for message output
- ESP32 development board
- LoRa module (SX1276/SX1278)
- I2C LCD display (16x2)
- Connecting wires and breadboard
ESP32 Pin → LoRa Module
5 (NSS) → NSS
15 (RESET) → RESET
2 (DIO0) → DIO0
23 (MOSI) → MOSI
19 (MISO) → MISO
18 (SCK) → SCK
3.3V → VCC
GND → GND
ESP32 Pin → LCD (I2C)
21 (SDA) → SDA
22 (SCL) → SCL
5V → VCC
GND → GND
- Node.js (for Hardhat blockchain)
- Python 3.7+ (for Flask server)
- Arduino IDE (for ESP32 programming)
- Required Python packages:
flask,web3 - Required Arduino libraries:
LoRa,WiFi,HTTPClient,LiquidCrystal_I2C
# Install Hardhat (if not already installed)
npm install --save-dev hardhat
# Start local blockchain
npx hardhat node
# Deploy smart contract
npx hardhat run scripts/deploy.js --network localhost# Install dependencies
pip install flask web3
# Update contract address in server.py
# (Use the address from contract deployment)
# Start server
python pyscript/server.py- Create
keys.hfile in bothsender/andreceiver/directories:
// keys.h
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* serverUrl = "http://YOUR_FLASK_SERVER_IP:5000/log";-
Install required Arduino libraries:
- LoRa by Sandeep Mistry
- LiquidCrystal I2C
-
Upload
sender.inoto sender ESP32 -
Upload
receiver.inoto receiver ESP32
- Start local blockchain:
npx hardhat node - Deploy smart contract and note the contract address
- Update
server.pywith the contract address - Start Flask server:
python pyscript/server.py - Power on ESP32 devices
- Sender ESP32: Sends "Hello0" through "Hello9" messages
- LoRa Communication: Messages transmitted via 433MHz LoRa
- Blockchain Logging: Messages logged to blockchain via Flask server
- Device Authentication: Receiver checks sender authentication
- Message Display: Only authenticated messages shown on receiver LCD
Log a message to the blockchain
{
"device_id": "esp32_sender",
"message": "Hello World"
}Check device authentication
{
"device_id": "esp32_sender"
}Returns: "yes" or "no"
registerDevice(string deviceId): Register a new devicelogMessage(string deviceId, string content): Log message from registered devicegetMessage(uint256 index): Retrieve message by indexgetMessageCount(): Get total number of messagesregisteredDevices(string deviceId): Check if device is registered
- Frequency: 433MHz
- Pins: NSS=5, RESET=15, DIO0=2
- WiFi: Configure in
keys.h - Server: Default port 5000
- Blockchain: Local Hardhat network (port 8545)
- LoRa not working: Check wiring and frequency settings
- WiFi connection failed: Verify credentials in
keys.h - Blockchain connection error: Ensure Hardhat node is running
- Contract interaction fails: Verify contract address in
server.py
- Monitor Serial output for both ESP32 devices
- Check Flask server logs for HTTP requests
- Verify blockchain transactions in Hardhat console
- Device Registration: Only registered devices can log messages
- Blockchain Immutability: Messages stored permanently on blockchain
- Authentication: Receiver verifies sender before displaying messages
- Timestamp Verification: All messages include blockchain timestamps
- Add message encryption for LoRa communication
- Implement device revocation mechanism
- Add message acknowledgment system
- Support for multiple LoRa channels
- Web dashboard for message monitoring
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request