Skip to content

Commit bae4364

Browse files
authored
[LORA] Add water counter sensor with example (1technophile#1941)
* Add water counter example
1 parent b5e0223 commit bae4364

File tree

5 files changed

+427
-2
lines changed

5 files changed

+427
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
#include <LoRa.h>
2+
#include <SPI.h>
3+
#include <WiFi.h>
4+
#include <Wire.h>
5+
#include <stdio.h>
6+
#include <Adafruit_Sensor.h>
7+
#include <DHT.h>
8+
#include <DHT_U.h>
9+
10+
#include "SSD1306.h"
11+
12+
//LoRa pins
13+
#define SCK 5 // GPIO5 -- SX1278's SCK
14+
#define MISO 19 // GPIO19 -- SX1278's MISnO
15+
#define MOSI 27 // GPIO27 -- SX1278's MOSI
16+
#define SS 18 // GPIO18 -- SX1278's CS
17+
#define RST 23 // GPIO14 -- SX1278's RESET
18+
#define DI0 26 // GPIO26 -- SX1278's IRQ(Interrupt Request)
19+
#define BAND 433E6
20+
21+
//OLED pins
22+
#define OLED_SDA 21
23+
#define OLED_SCL 22
24+
//#define OLED_RST 16
25+
26+
#define LED 25
27+
28+
#define DHT_PIN 14
29+
#define DHT_TYPE DHT22
30+
31+
#define CNT_PIN 34
32+
33+
#define BAT_PIN 35
34+
35+
const int MAX_ANALOG_VAL = 4095;
36+
const float MAX_BATTERY_VOLTAGE = 4.2;
37+
38+
int cntpkt = 0;
39+
unsigned long counter = 0;
40+
unsigned long last_interrupt_time = 0;
41+
int pulse_seen = 0; // pulse detected by interrupt function
42+
int bounce_delay_ms = 100;
43+
44+
SSD1306 display(0x3c, OLED_SDA, OLED_SCL);
45+
String rssi = "RSSI --";
46+
String packSize = "--";
47+
String packet;
48+
49+
DHT_Unified dht(DHT_PIN, DHT_TYPE); //Inizializza oggetto chiamato "dht", parametri: pin a cui è connesso il sensore, tipo di dht 11/22
50+
51+
void ICACHE_RAM_ATTR bounceCheck ();
52+
53+
void setup() {
54+
pinMode(LED, OUTPUT);
55+
56+
//pinMode(OLED_RST, OUTPUT);
57+
//digitalWrite(OLED_RST, LOW); // set GPIO16 low to reset OLED
58+
//delay(50);
59+
//digitalWrite(OLED_RST, HIGH); // while OLED is running, must set GPIO16 in high
60+
61+
Serial.begin(115200);
62+
while (!Serial);
63+
Serial.println();
64+
Serial.println("LoRa Sender Test");
65+
66+
SPI.begin(SCK, MISO, MOSI, SS);
67+
LoRa.setPins(SS, RST, DI0);
68+
if (!LoRa.begin(BAND)) {
69+
Serial.println("Starting LoRa failed!");
70+
while (1);
71+
}
72+
73+
Serial.println("init ok");
74+
display.init();
75+
display.flipScreenVertically();
76+
display.setFont(ArialMT_Plain_10);
77+
78+
pinMode(BAT_PIN, INPUT);
79+
pinMode(CNT_PIN, INPUT_PULLUP);
80+
81+
attachInterrupt (CNT_PIN, bounceCheck, RISING);
82+
83+
dht.begin();
84+
sensor_t sensor;
85+
dht.temperature().getSensor(&sensor);
86+
Serial.println(F("------------------------------------"));
87+
Serial.println(F("Temperature Sensor"));
88+
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
89+
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
90+
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
91+
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
92+
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
93+
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
94+
Serial.println(F("------------------------------------"));
95+
// Print humidity sensor details.
96+
dht.humidity().getSensor(&sensor);
97+
Serial.println(F("Humidity Sensor"));
98+
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
99+
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
100+
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
101+
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
102+
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
103+
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
104+
Serial.println(F("------------------------------------"));
105+
106+
delay(1500);
107+
}
108+
109+
void loop() {
110+
sensors_event_t event1, event2;
111+
112+
display.clear();
113+
display.setTextAlignment(TEXT_ALIGN_LEFT);
114+
display.setFont(ArialMT_Plain_10);
115+
116+
display.drawString(0, 0, "Sending packet: ");
117+
display.drawString(90, 0, String(cntpkt));
118+
119+
String NodeId = WiFi.macAddress();
120+
// float temp = dht.readTemperature();
121+
// float hum = dht.readHumidity();
122+
float battery = getBattery();
123+
124+
dht.temperature().getEvent(&event1);
125+
if (isnan(event1.temperature)) {
126+
Serial.println(F("Error reading temperature!"));
127+
}
128+
else {
129+
Serial.print(F("Temperature: "));
130+
Serial.print(event1.temperature);
131+
Serial.println(F("°C"));
132+
}
133+
// Get humidity event and print its value.
134+
dht.humidity().getEvent(&event2);
135+
if (isnan(event2.relative_humidity)) {
136+
Serial.println(F("Error reading humidity!"));
137+
}
138+
else {
139+
Serial.print(F("Humidity: "));
140+
Serial.print(event2.relative_humidity);
141+
Serial.println(F("%"));
142+
}
143+
144+
// send packet
145+
LoRa.beginPacket();
146+
147+
String msg = "{\"model\":\"ESP32CNT\",\"id\":\"" + NodeId + "\",\"count\":\"" + String(counter) + "\",\"tempc\":\"" + String(event1.temperature) + "\",\"hum\":\"" + String(event2.relative_humidity) + "\",\"batt\":\"" + String(battery) + "\"}";
148+
// Send json string
149+
LoRa.print(msg);
150+
LoRa.endPacket();
151+
152+
Serial.println(msg);
153+
154+
display.drawString(0, 15, String(NodeId));
155+
display.drawString(0, 30, "count: " + String(counter));
156+
display.drawString(0, 45, "battery: " + String(battery)+ " %");
157+
display.display();
158+
159+
delay(5000);
160+
161+
cntpkt++;
162+
163+
digitalWrite(LED, HIGH); // turn the LED on (HIGH is the voltage level)
164+
delay(1000); // wait for a second
165+
digitalWrite(LED, LOW); // turn the LED off by making the voltage LOW
166+
//delay(60000); // wait for 60 seconds
167+
delay(6000);
168+
}
169+
170+
void ICACHE_RAM_ATTR bounceCheck (){
171+
unsigned long interrupt_time = millis();
172+
if (interrupt_time - last_interrupt_time > bounce_delay_ms) counter++; // void loop() then notes pulse == 1 and takes action
173+
last_interrupt_time = interrupt_time;
174+
}
175+
176+
float getBattery(void) {
177+
int rawValue = analogRead(BAT_PIN);
178+
179+
float voltageLevel = (rawValue / 4095.0) * 2 * 1.1 * 3.3; // calculate voltage level
180+
float perc = voltageLevel / MAX_BATTERY_VOLTAGE * 100;
181+
182+
return(perc);
183+
}
184+

examples/LoraWaterCounter/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# OpenMQTTGateway LoRa Node Example
2+
This repository contains an example of a LoRa node program designed for the ESP32 platform.
3+
The program reads the DHT22 temperature and humidity, the battery level and water consumption via reed switch, packages the data into a JSON format, and sends it over LoRa.
4+
5+
## Features:
6+
* Uses an SX12XX LoRa module.
7+
* Displays packet sending status, counter and battery level data on an SSD1306 OLED display.
8+
* Sends the ESP32's MAC address as the node ID.
9+
* Sends temperature data in Celsius.
10+
11+
## Hardware Requirements:
12+
* ESP32 development board.
13+
* SX12XX LoRa module.
14+
* SSD1306 OLED display.
15+
16+
## Pin Configuration: (Lilygo LoRa32 V2.1_1.6)
17+
* SCK - GPIO5
18+
* MISO - GPIO19
19+
* MOSI - GPIO27
20+
* SS - GPIO18
21+
* RST - GPIO23
22+
* DI0 - GPIO26
23+
24+
## Setup:
25+
### Hardware Setup:
26+
27+
Connect the SX1278 LoRa module and the SSD1306 OLED display to the ESP32 according to the pin configuration.
28+
Ensure that the OLED display is powered correctly.
29+
30+
### Software Setup:
31+
32+
* Clone this repository.
33+
* Open the provided node program with PlatformIO
34+
* Upload the program to your ESP32.
35+
36+
## Usage:
37+
Power on the ESP32.
38+
The OLED display will show the status of the packet being sent and the current temperature reading.
39+
The built-in LED on the ESP32 will blink once every time a packet is sent.
40+
Monitor the serial output (at 115200 baud rate) to see the JSON formatted data being sent.
41+
42+
If you use arduino load ino file.
43+
44+
## Data Format:
45+
The data is sent in the following JSON format:
46+
47+
```json
48+
{
49+
"model": "ESP32CNT",
50+
"id": "ESP32_MAC_ADDRESS",
51+
"count": "COUNTER_IN_LITER",
52+
"tempc": "TEMPERATURE_IN_CELSIUS",
53+
"hum": "HUMIDITY_IN_PERCENTAGE",
54+
"batt": "BATTERY_IN_PERCENTAGE"
55+
}
56+
```
57+
58+
## Troubleshooting:
59+
LoRa Initialization Failed: Ensure that the SX1278 LoRa module is connected correctly and powered on.
60+
OLED Display Not Working: Check the connections and ensure that the display is powered correctly.
61+
No Temperature Data: Ensure that the ESP32's internal temperature sensor is functional.
62+
Contributing:
63+
Feel free to contribute to this example by opening issues or submitting pull requests. Any feedback or improvements are welcome!
64+
65+
I hope this README helps users understand and use your program! Adjustments can be made as necessary to fit any additional details or changes.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:ttgo-lora32-v1]
12+
platform = espressif32
13+
board = ttgo-lora32-v1
14+
framework = arduino
15+
lib_deps =
16+
https://github.com/sandeepmistry/arduino-LoRa.git#f4a1d27
17+
https://github.com/ThingPulse/esp8266-oled-ssd1306.git#f96fd6a
18+
monitor_speed = 115200
19+
20+
[env:ttgo-lora32-v21]
21+
platform = espressif32
22+
board = ttgo-lora32-v21
23+
framework = arduino
24+
lib_deps =
25+
https://github.com/sandeepmistry/arduino-LoRa.git#f4a1d27
26+
https://github.com/ThingPulse/esp8266-oled-ssd1306.git#f96fd6a
27+
monitor_speed = 115200
28+
29+
[env:heltec-wifi-lora-32] ; Heltec ESP32 Board with SSD1306 display
30+
platform = espressif32
31+
board = heltec_wifi_lora_32
32+
framework = arduino
33+
lib_deps =
34+
https://github.com/sandeepmistry/arduino-LoRa.git#f4a1d27
35+
https://github.com/ThingPulse/esp8266-oled-ssd1306.git#f96fd6a
36+
monitor_speed = 115200

0 commit comments

Comments
 (0)