|
| 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 | + |
0 commit comments