-
Notifications
You must be signed in to change notification settings - Fork 0
Software Development
All firmware developments used in this project is done on the Arduino IDE.
The Arduino IDE (Integrated Development Environment) is a user-friendly software platform used for writing, compiling, and uploading code to Arduino microcontroller boards. It supports C and C++ programming languages and includes a vast library of pre-written code and functions, making it accessible for beginners and powerful enough for advanced users. The IDE provides a simple interface for code development, debugging, and serial communication with the board. It's widely used in educational, hobbyist, and professional projects, enabling quick prototyping and development of electronics and embedded systems.
To program the Arduino Pro Mini and ESP32 Dev Kit V1, the following board packages need to be installed via the Board Manager in the Arduino IDE:
-
Arduino AVR Boards by Arduino
-
Esp32 by Espressif Systems
This project requires the following libraries:
-
DHT sensor library for Arduino by Adafruit
-
Adafruit Unified Sensor by Adafruit
-
BH1750 by Christopher Laws
-
LowPower by Rocket Scream Electronics
-
LoRa by Sandeep Mistry
Ensure these are installed in your Arduino IDE before compiling the code.
- DHT11 sensor: Pin 8
- Soil moisture sensor data: Pin A0
- Soil moisture sensor power: Pin 7
- Battery voltage measurement: Pin A1
- BH1750 sensor: I2C pins
- LoRa module: SPI pins
- The device initializes all sensors and the LoRa module.
- It generates or retrieves a unique ID for the node.
- BH1750 sensor is initiated in
ONE_TIME_HIGH_RES_MODE
for it to work in the lowest power consumption setting. - Soil moisture sensor is disconnected from power via a transistor to prevent unnecessary power draw.
- It then enters a loop of:
- Waking up and reinitializing LoRa
- Reading sensor data
- Transmitting data via LoRa
- Entering a low-power sleep mode
The LoRa message is formatted as follows:
##NETWORK_ID@ENVSENSOR@NODE_ID:SEQUENCE:TIMESTAMP@TEMP;HUMIDITY;LIGHT;SOIL@BATTERY@CHECKSUM$$
-
##
: Start delimiter -
NETWORK_ID
: Identifier for the network (e.g., "WD") -
ENVSENSOR
: Message type identifier -
NODE_ID
: Unique identifier for this sensor node -
SEQUENCE
: Message sequence number -
TIMESTAMP
: Current timestamp (milliseconds since boot) -
TEMP
: Temperature reading (°C) -
HUMIDITY
: Humidity reading (%) -
LIGHT
: Light intensity (lux) -
SOIL
: Soil moisture reading (V) -
BATTERY
: Battery voltage (V) -
CHECKSUM
: Hexadecimal checksum of the message -
$$
: End delimiter
Example:
##WD@ENVSENSOR@0002:10:[email protected];53.20;216.33;[email protected]@a08$$
To parse this message:
- Split the message by
@
characters - Further split the third part (after ENVSENSOR) by
:
to get node info - Split the fourth part by
;
to get environmental sensor readings - The fifth part contains the battery voltage
- Everything between
##
and$$
should be used to verify the checksum
Identify Calibration Points:
-
dryValue
: This represents the analog value output by the sensor when the soil is completely dry. The default dry value is set to 690. -
wetValue
: This represents the analog value output when the soil is completely saturated with water. The default wet value is set to 380.
Collect Calibration Data:
- Place the soil moisture sensor in a sample of completely dry soil (or leave it exposed to air) and record the analog value. This is the dry value.
- Submerge the sensor in a sample of water-saturated soil and record the analog value. This is the wet value.
Adjusting Calibration Values:
The default calibration values (dryValue = 690
and wetValue = 380
) may need to be adjusted depending on,
- Sensor Variability: Different sensors might have slight variations in their readings.
- Environmental Conditions: Soil type, salinity, and temperature can affect the sensor's readings.
The Network ID is a crucial parameter that MUST be customized for your specific implementation. It serves as a filter for the receiver device to process only messages from sensors within its group.
- You MUST change the
NETWORK_ID
in the code to a unique, short identifier for your sensor network. - Keep the Network ID as short as possible (e.g., "A1", "B2") to minimize transmission length.
- Ensure all sensors in your network and the corresponding receiver use the same
Network_ID
.
Failure to change the Network_ID
may result in:
- Interference with other sensor networks
- Inability to properly filter and process your sensor data
- Connect all sensors and the LoRa module to the Arduino board according to the pin configuration.
- Install all required libraries in the Arduino IDE.
- Copy the provided code into the Arduino IDE.
- Adjust any necessary parameters (e.g.,
NETWORK_ID
,LORA_FREQUENCY
). - Upload the code to the Arduino board.
The device generates a unique 5-digit ID for each node, which is stored in EEPROM:
- On the first run, it generates a random ID based on sensor readings and analog noise.
- The ID is stored in EEPROM and reused on subsequent boots.
- This ensures each node has a persistent, unique identifier.
The device uses low-power sleep modes to conserve energy. It sleeps for approximately 15 minutes between readings (adjustable via SLEEP_CYCLES
).
- Build the circuit according to the given schematic.
- Install all required libraries in the Arduino IDE.
- Download and open the provided code on the Arduino IDE.
- Adjust any necessary parameters (e.g.,
NETWORK_ID
,LORA_FREQUENCY
). - Connect the TTL converter to the Arduino Pro Mini.
- Upload the code to the Arduino Pro Mini.
- Compilation errors might be caused by not installing the correct libraries.
- If sensors fail to initialize, the built-in LED will blink rapidly.
- Enable
DEBUG
mode for verbose serial output to diagnose issues. - Ensure all connections are secure and the power supply is adequate.
- Verify that the LoRa frequency matches your regional regulations.
-
Connect the TTL Converter to the Arduino Pro Mini:
- GND on the TTL converter to GND on the Pro Mini.
- VCC on the TTL converter to VCC (3.3V) on the Pro Mini.
- TX on the TTL converter to RX on the Pro Mini.
- RX on the TTL converter to TX on the Pro Mini.
- DTR (if available) on the TTL converter to the DTR pin on the Pro Mini for auto-reset during upload.
-
Set Up the Arduino IDE:
- Open the Arduino IDE.
- Go to Tools > Board and select Arduino Pro or Pro Mini.
- Go to Tools > Processor and select ATmega328P (3.3V, 8 MHz).
- Go to Tools > Port and select the COM port corresponding to your TTL converter.
-
Upload the Sketch:
- Open the sensor module firmware Arduino sketch.
- Click on the Upload button in the IDE.
- The firmware will be uploaded to the Arduino Pro Mini. If the DTR is connected, the upload should start automatically.
-
Troubleshooting:
- If the upload fails, check the connections and ensure the correct COM port and board settings are selected.
-
SPI library - inbuilt library
-
FS library - inbuilt library
-
RTCLib library by Adafruit
-
LoRa library by Sandeep Mistry
-
SPIFFS library - inbuilt library
-
BLE Libraries (BLEDevice.h, BLEServer.h, BLEUtils.h, BLE2902.h) - inbuilt library
- LoRa module: SPI pins (ss - 5, rst - 14, dio0 - 4)
- RTC module: I2C pins
The receiver device software is responsible for initializing the hardware components, handling incoming data, and providing a command-line interface (CLI) for interaction.
-
Initialization: The device initializes the RTC, BLE and LoRa module.
-
BLE Initialization:
- A BLE service is created.
- Under this service:
- A read-only characteristic is created for reading the latest file name in the /data folder.
- A read/write characteristic is created to allow a connected BLE device to write the file name it wants to read and read that file line by line.
-
Task Management: FreeRTOS tasks are created for BLE services and LoRa services to run concurrently without task congestion.
-
Command Line Interface (CLI): The CLI provides the following functionalities:
- List all files and directories in the current directory
- Read specific files (data files and log files)
- Delete specific files
- List all files in a specific directory
- Show total and used capacity of SPI flash
- Show used capacity in bytes and as a percentage
- Set the RTC time
- Set the RTC date
- Display the current date and time
- Show the command catalog
-
Receiving and Processing Data (LoRa Services):
- Upon receiving a packet, the message is read character by character and stored in the receivedMessage string.
- Specific parts of the message, such as the network ID and checksum, are extracted and validated.
-
Message Validation:
- The code looks for specific delimiters (
##
and@
) to locate the network ID. - If the network ID matches the expected
NETWORK_ID
, the message is processed; otherwise, it is discarded.
- The code looks for specific delimiters (
-
Checksum Validation:
- If the network ID matches, the code extracts the message portion for checksum calculation.
- The calculated checksum is compared with the checksum in the received message. If they match, the message is valid; otherwise, it is discarded.
-
Parsing the Message (parseLoRaMessage):
- The function extracts various data fields:
- Network ID
- Node ID
- Sequence Number
- Timestamp (wake time of the sensor module)
- Sensor Readings (temperature, humidity, light, soil moisture)
- Battery Voltage
- Checksum
- The function extracts various data fields:
-
Data Storage:
- Valid messages are processed and stored in SPIFFS (SPI Flash File System) storage.
- Data is stored in the format:
nodeID,batteryVoltage,temperature,humidity,light,soilMoisture,unixtime
(received time). - The file path is determined by the UNIX timestamp of the start of the current day.
-
Logging:
- Events such as message saving, checksum mismatch, or unmatched network ID are logged.
- Logs include details like
Node ID, Sequence Number, wake time, RSSI
.
-
Checksum Calculation (calculateChecksum):
- The function computes a checksum by summing the ASCII values of all characters in the message.
- This ensures the integrity of the received message.
To ensure that the flash chip doesn't run out of space, a cyclic recording feature is used.
This feature will automatically check the storage usage and automatically delete the oldest files if the usage goes above 75%
Ensure the Network ID on the receiver device matches the Network ID on the sensor module.
Incorrect Network ID configuration can result in:
- Interference with other sensor networks.
- Inability to properly filter and process your sensor data.
-
Connect the ESP32 Devkit V1 to Your Computer:
- Use a micro USB cable to connect the ESP32 Devkit V1 to your computer. Ensure the cable is a data cable, not just a charging cable.
-
Select the Board and Port:
- Go to Tools > Board and select ESP32 Dev Module.
- Go to Tools > Port and select the COM port corresponding to your ESP32.
-
Upload Your Sketch:
- Open the Receiver Device Arduino sketch on the Arduino IDE.
- Click the Upload button (right arrow icon).
- The IDE will compile the sketch, and you should see "Connecting..." in the output window.
-
Press the BOOT Button (if needed):
- If the upload process does not start automatically, press and hold the BOOT button on the ESP32 until you see the upload progress bar start moving, then release the button.
-
Verify the Upload:
- Once the upload is complete, you should see the message "Done uploading." in the IDE.
-
Troubleshooting:
- If the upload fails, double-check your COM port, ensure the correct board is selected, and try holding the BOOT button during the upload process.
- Make sure the ESP32 drivers are correctly installed on your computer.
- Connect the RTC and LoRa module to the esp32 dev kit v1.
- Install all required libraries in the Arduino IDE.
- Download and open the provided code on the Arduino IDE.
- Adjust any necessary parameters (e.g., NETWORK_ID, LORA_FREQUENCY).
- Upload the code to the esp32 devkit v1.
- Check the RTC time by typing
DATETIME
on the serial monitor - If the date and time is off, adjust it using the
SETDATE <YYYY-MM-DD>
andSETTIME <HH:MM:SS>
commands
- Compilation errors might be caused by not installing the correct libraries.
- If peripherals fail to initialize, the built-in LED will blink rapidly.
- Enable
LOGGING
mode for system to record logs. - Ensure all connections are secure and the power supply is adequate.
- Verify that the LoRa frequency matches your regional regulations.
The mobile app is built using flutter and it's designed for managing and monitoring garden sensors through Bluetooth connectivity. It allows users to connect to receiver devices, scan for and add sensor nodes, view sensor data, and analyze sensor readings over time.
- Welcomes users to the Garden app
- Provides a button to connect a receiver device
- Displays a list of connected receiver devices
- Allows users to edit receiver names
- Implements a refresh mechanism to update the list
- Scans for available Bluetooth devices
- Filters devices based on a specific service UUID
- Allows users to add new devices to the system
- Shows a list of connected sensor nodes
- Implements a dropdown to select the current receiver
- Displays a sync card (used to sync data from a receiver device)
- Provides detailed information about a specific sensor node
- Displays graphs of sensor data over time
- Allows users to edit sensor name and description
- Implements time range selection for data viewing (24h, 7d, 30d, custom)
- Bluetooth device scanning and connection
- CRUD operations for receivers and sensor nodes
- Data visualization through graphs
- Time-based data filtering
- Editable device information
- Uses DAO (Data Access Object) pattern for data operations
- Implements separate DAOs for receivers, nodes, and sensor data
- Utilizes SQLite database (implied by DAO structure)
- Custom widgets like
SensorCard
,GraphCard
, andSyncCard
. - Implements a side menu for navigation.
- Uses Material Design components and custom styling.
- Utilizes the
flutter_blue_plus
package for BLE operations. - Implements scanning, connecting, and service discovery for BLE devices.
- Uses custom
GraphData
andGraphInterval
models for data representation. - Implements dynamic graph generation based on selected time ranges.
This is a project by Watchdog Sri Lanka (team-watchdog). Watchdog is a multidisciplinary team of journalists, researchers and software engineers, operating under the Appendix umbrella. We hunt misinformation, investigate matters of public welfare, and build software tools.
We began in April 2019, days after the Easter Sunday bombings in Sri Lanka, as a group of concerned citizens trying to counter misinformation. We did this amidst government crackdowns on freedom of expression and mass hysteria caused by fake news and rumors, building a mobile app that was used by over 200,000 people to verify information and counteract rumors in their own networks.
We've been called an “open-source intelligence research collective”, and that's fairly accurate. We use a lot of OSINT techniques - a combination of data scraping, analysis of publicly available documents and datasets, paired with old-school boots-on-the-ground journalism.