Work with the Cayenne Dashboard - LoRa Device (part 4)

So that we also receive data for our Gateway, we will take the example from Part 1 rebuild on LoRa. To do this, we need to replace the D1 Mini with a ESP32 with LoRa replace. The DHT11 let's connect to the Pin Gpio22 and the Relay with the Pin Gpio23.

Sketch:

 Board for Arduino IDE = TTGO LoRa32-OLED V1

#include <arduino.houses>
#include <DHTesp.houses>
#include <SPI.houses>
#include <CayenneLPP.houses>
#include <LoRa.houses>
#include "SSD1306. h"

//LoRa Settings
#define OESTRADIOL      18
#define RST     14
#define DI0     26
#define BAND    433175000

// Time Interval (Seconds)
#define TX_INTERVAL 10

// Pin for DHT11
#define DHT_PIN 22

// POin for relays
#define RELAY  23

//Channel assignment
#define CHANNEL_TEMP 1
#define CHANNEL_HUM 2
#define Channel relays 3
#define CHANNEL_STATUS 4


//Global Variables
uint8_t devid[6]; // Device ID = MAC address
uint8_t relaisOn = 0; // Status of the relay
Temp andhumidity newValues; // Temperature and humidity of DHT11


struct LPP_BLOCK {   uint8_t guy;   int Val;
};

// Receive buffer for a maximum of 8 channels
LPP_BLOCK empBuf[8];


// Buffer CayenneLPP Data Format.
CayenneLPP lpp(64);

// Instance for temperature sensor
DHTesp dht;

// OLED Display
SSD1306  display(0x3c, 4, 15);

// Function to send the data to the Gateway
void sendLoRa() {     int CNTs;     uint8_t sze;     uint8_t choirs;     // Delete Buffer     lpp.reset();     // Write data packets into the Buffer     lpp.addTemperature(CHANNEL_TEMP, newValues.temperature);     lpp.addRelativeHumidity(CHANNEL_HUM, newValues.humidity);     lpp.addDigitalInput(CHANNEL_STATUS,relaisOn);     lpp.addDigitalOutput(Channel relays,0); //This is necessary for the Gateway to create a Buffer for this channel     // Create Lora package     if (LoRa.beginPacket()){       LoRa.write(devid,6); //first, the devices vID       LoRa.write(lpp.getBuffer(),lpp.getSize()); //then the data in LPP Format       if (LoRa.endPacket()) {         Serial.println("Lora transmission OK");       } else {         Serial.println("Lora Transmission Error");       }     } else {       Serial.println("Lora Transmission Error");     }     Serial.printf("%3i Bytes sent\n",lpp.getSize()+6);     //now we wait for the receipt     CNTs = 0;     do {       sze = LoRa.parsePacket();       CNTs++;       delay(100);     } while ((sze == 0) && (CNTs < 100));     if (CNTs >= 100) {       Serial.println("No response from Gateway");     }     else     {       Serial.printf("Data received %I Bytes\n",sze);       if (sze >= 6){         Serial.printf("Receipt received % I Bytes \ n",sze);         CNTs=0;         // we read the burst 6 bytes and compare them with the device Id         while ((sze > 0) && (CNTs<6)) {           sze--;           devid[CNTs++]==LoRa.read();         }         // if cnt = 6 the ID was correct         // We read the Rest in the receipt buffer         while (sze > 0) {         //ertses Byte = channel          choirs = LoRa.read();         sze--;         // if the channel is smaller than 8 we store type and values         if (choirs < 8) {           empBuf[choirs].guy = LoRa.read();           sze--;           switch (empBuf[choirs].guy) {             // Only action types are important                           case LPP_DIGITAL_OUTPUT: empBuf[choirs].Val = LoRa.read();               sze--;               Serial.printf("Receive channel=%02x type=%02x value=%i \n",choirs,empBuf[choirs].guy,empBuf[choirs].Val);               break;             case LPP_ANALOG_OUTPUT: empBuf[choirs].Val = LoRa.read() * 256 + LoRa.read();               sze-=2;               Serial.printf("Receive channel=%02x type=%02x value=%i \n",choirs,empBuf[choirs].guy,empBuf[choirs].Val);               break;           }         }       }       if (CNTs == 6) {         Serial.println("Receipt OK");       } else {         Serial.println("Invalid Answer");       }     }   }
}

void setup() {     Serial.begin(115200);     Serial.println("Starting");     pinMode(RELAY,OUTPUT);     SPI.begin(5,19,27,18);     esp_efuse_read_mac(devid);     // OLED Reset     pinMode(16,OUTPUT);     digitalWrite(16, LOW);      delay(50);     digitalWrite(16, HIGH);     //and initialize     display.init();     display.setFont(ArialMT_Plain_10);     display.display();         LoRa.setPins(OESTRADIOL,RST,DI0);     Serial.println("LoRa TRX");     if (!LoRa.begin(BAND)) {       Serial.println("Starting Lora failed!");     }     Serial.println("Lora Initial OK!");     LoRa.enableCrc();     dht.setup(DHT_PIN, DHTesp::DHT11);
}

void loop() {     uint8_t* buf;     uint8_t len;      int CNTs;     uint8_t sze;     newValues = dht.getTempAndHumidity();     sendLoRa();     digitalWrite(RELAY,empBuf[3].Val);     relaisOn = empBuf[3].Val;     if (dht.getStatus() == 0) {       display.clear();       display.drawString(0, 0, "Temperature: ");       display.drawString(80, 0, String(newValues.temperature));       display.drawString(110, 0,"°C");       display.drawString(0, 20, "Moisture  : ");       display.drawString(80,20, String(newValues.humidity));       display.drawString(110,20, "%");       display.drawString(0, 40, "Relay :");       if (relaisOn == 1) display.drawString(80, 40, "a"); else display.drawString(80, 40, "from");       display.display();       delay(TX_INTERVAL * 1000);       }
}

Registration at the Gateway:

After the Sketch has been uploaded and the program has been started, the circuit sends a data packet via LoRa every 10 seconds. If we now call up the Gateway website with the Browser, we should see the MAC address of our circuit next to the "Register" button.

We can now give the device a name and then register it. The device is displayed in the device list of the gateway.

Now we should also see the channels in the Cayenne Dashboard.

Now we can add and configure the Widgets to the Dashboard as described in Part 1. We also have to create a Widget with a button to switch the relay.

Have fun crafting.

 

DisplaysEsp-32Projekte für fortgeschritteneSensoren

Leave a comment

All comments are moderated before being published