Created
February 26, 2024 07:06
-
-
Save warmuuh/5ecd320b790f1e6348c91479b5160d4f to your computer and use it in GitHub Desktop.
heltec automation lora 32 + dht11 + mqtt sender/receiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// connects to a given mqtt server via wifi | |
// receives lora messages in format "<deviceId><jsonMessage>" and forwards them to mqtt channel /lora/device/<deviceId> | |
// message: {"message": <content of whatever sender sent>, "rssi":..., "snr":...} | |
#include "LoRaWan_APP.h" | |
#include "Arduino.h" | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#define RF_FREQUENCY 915000000 // Hz | |
#define TX_OUTPUT_POWER 14 // dBm | |
#define LORA_BANDWIDTH 0 // [0: 125 kHz, | |
// 1: 250 kHz, | |
// 2: 500 kHz, | |
// 3: Reserved] | |
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12] | |
#define LORA_CODINGRATE 1 // [1: 4/5, | |
// 2: 4/6, | |
// 3: 4/7, | |
// 4: 4/8] | |
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx | |
#define LORA_SYMBOL_TIMEOUT 0 // Symbols | |
#define LORA_FIX_LENGTH_PAYLOAD_ON false | |
#define LORA_IQ_INVERSION_ON false | |
#define RX_TIMEOUT_VALUE 1000 | |
#define BUFFER_SIZE 30 // Define the payload size here | |
char txpacket[BUFFER_SIZE]; | |
char rxpacket[BUFFER_SIZE]; | |
typedef struct { | |
byte src_addr; | |
char msg[255]; | |
int16_t rssi; | |
int8_t snr; | |
} _l_packet; | |
_l_packet pkt; | |
boolean gotpacket; | |
const char* ssid = "<your wlan ssid>"; | |
const char* password = "<your wlan password>"; | |
const char* mqttServer = "<mqtt server ip"; | |
const int mqttPort = 1883; | |
const char* mqttUser = "<mqtt user>"; | |
const char* mqttPassword = "<mqtt password>"; | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
static RadioEvents_t RadioEvents; | |
int16_t txNumber; | |
int16_t rssi,rxSize; | |
bool lora_idle = true; | |
void setup() { | |
Serial.begin(115200); | |
Mcu.begin(); | |
txNumber=0; | |
rssi=0; | |
initwifi(); | |
RadioEvents.RxDone = OnRxDone; | |
Radio.Init( &RadioEvents ); | |
Radio.SetChannel( RF_FREQUENCY ); | |
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, | |
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, | |
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, | |
0, true, 0, 0, LORA_IQ_INVERSION_ON, true ); | |
initmqtt(); | |
client.publish("lora/gateway","{\"status\":\"started\"}"); | |
} | |
void initwifi(){ | |
WiFi.begin(ssid, password); | |
while ( WiFi.status() != WL_CONNECTED) { | |
messageLog("Connecting to WiFi.."); | |
} | |
messageLog("Connected to the WiFi network"); | |
delay(1000); | |
} | |
void initmqtt(){ | |
client.setServer(mqttServer, mqttPort); | |
while (!client.connect("ESP32Client", mqttUser, mqttPassword )) { | |
messageLog("Connecting to MQTT..."); | |
} | |
messageLog("Connected to MQTT... "); | |
delay(1000); | |
} | |
void messageLog(const char *msg){ | |
Serial.printf(msg); | |
} | |
void loop() | |
{ | |
String topic; | |
String msg; | |
client.loop(); | |
if(lora_idle) | |
{ | |
lora_idle = false; | |
if (WiFi.status() == WL_CONNECTED && client.connected()) { | |
topic = "lora/device/"+String(pkt.src_addr); | |
msg = "{\"message\":" + String(pkt.msg) + ", \"rssi\": " + String(pkt.rssi) + ", \"snr\": " + String(pkt.snr) +"}"; | |
client.publish(topic.c_str(), msg.c_str()); | |
} | |
Serial.println("into RX mode"); | |
Radio.Rx(0); | |
} | |
Radio.IrqProcess( ); | |
} | |
void OnRxDone( uint8_t *payload, uint16_t packetSize, int16_t rssi, int8_t snr ) | |
{ | |
rssi=rssi; | |
rxSize=packetSize; | |
memcpy((uint8_t *)&pkt, payload, packetSize ); | |
pkt.msg[packetSize]='\0'; | |
pkt.rssi = rssi; | |
pkt.snr = snr; | |
Radio.Sleep( ); | |
Serial.printf("\r\nreceived packet \"%s\" with rssi %d , length %d\r\n",rxpacket,rssi,rxSize); | |
lora_idle = true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// wakes up from deepsleep every 30 minutes, waits a second and reads dht11 (configure pin!) | |
// sends it via lora in a format of <deviceId>{"temp": ..., "hum": ...} | |
#include "LoRaWan_APP.h" | |
#include "Arduino.h" | |
#include <Bonezegei_DHT11.h> | |
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ | |
#define TIME_TO_SLEEP 1800 /* 30 minutes, Time ESP32 will go to sleep (in seconds) */ | |
#define RF_FREQUENCY 915000000 // Hz | |
#define TX_OUTPUT_POWER 5 // dBm | |
#define LORA_BANDWIDTH 0 // [0: 125 kHz, | |
// 1: 250 kHz, | |
// 2: 500 kHz, | |
// 3: Reserved] | |
#define LORA_SPREADING_FACTOR 7 // [SF7..SF12] | |
#define LORA_CODINGRATE 1 // [1: 4/5, | |
// 2: 4/6, | |
// 3: 4/7, | |
// 4: 4/8] | |
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx | |
#define LORA_SYMBOL_TIMEOUT 0 // Symbols | |
#define LORA_FIX_LENGTH_PAYLOAD_ON false | |
#define LORA_IQ_INVERSION_ON false | |
#define RX_TIMEOUT_VALUE 1000 | |
#define BUFFER_SIZE 30 // Define the payload size here | |
Bonezegei_DHT11 dht(37); // # Pin 37 | |
char txpacket[BUFFER_SIZE]; | |
char rxpacket[BUFFER_SIZE]; | |
double txNumber; | |
bool lora_idle=true; | |
bool goToSleep = false; | |
static RadioEvents_t RadioEvents; | |
void OnTxDone( void ); | |
void OnTxTimeout( void ); | |
void setup() { | |
Serial.begin(115200); | |
Mcu.begin(); | |
dht.begin(); | |
delay(1000); //# wait for dht sensor | |
txNumber=0; | |
RadioEvents.TxDone = OnTxDone; | |
RadioEvents.TxTimeout = OnTxTimeout; | |
Radio.Init( &RadioEvents ); | |
Radio.SetChannel( RF_FREQUENCY ); | |
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, | |
LORA_SPREADING_FACTOR, LORA_CODINGRATE, | |
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, | |
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 ); | |
} | |
void loop() | |
{ | |
if(lora_idle == true) | |
{ | |
if (dht.getData()) { // get All data from DHT11 | |
float tempDeg = dht.getTemperature(); // return temperature in celsius | |
float tempFar = dht.getTemperature(true); // return temperature in fahrenheit if true celsius of false | |
int hum = dht.getHumidity(); // return humidity | |
Serial.printf("Temperature: %0.1lf°C %0.1lf°F Humidity:%d \n", tempDeg, tempFar, hum); | |
txNumber += 0.01; | |
sprintf(txpacket,"\x01{\"temp\":%0.1lf,\"hum\":%d}",tempDeg, hum); //start a package, \x01 is the device id 1 | |
Serial.printf("\r\nsending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket)); | |
Radio.Send( (uint8_t *)txpacket, strlen(txpacket) ); //send the package out | |
lora_idle = false; | |
} | |
} | |
Radio.IrqProcess( ); | |
if (goToSleep) { | |
goToSleep = false; | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | |
esp_deep_sleep_start(); | |
lora_idle = true; | |
} | |
} | |
void OnTxDone( void ) | |
{ | |
Serial.println("TX done......"); | |
goToSleep = true; | |
} | |
void OnTxTimeout( void ) | |
{ | |
Radio.Sleep( ); | |
Serial.println("TX Timeout......"); | |
goToSleep = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment