Github Devices Community Docs Blog

Project with Lora / Sensors

Did you bought a 433mhz board or a 868mhz one?

I bought this: https://www.banggood.com/2Pcs-LILYGO-TTGO-LORA-SX1278-ESP32-0_96-OLED-Display-Module-16-Mt-Bytes-128-Mt-bit-433Mhz-p-1270420.html?rmmds=myorder&cur_warehouse=UK

I already checked the issue.
Your firmware has 868E6 lora band, and i need 433E6, can you compile .bin and share with me?

Hmm, are you sure 433mhz Lora is authorized on your country ?

433 is for Asia, Europe is 868 but was my mistake.
I think I solved the problem… I changed the value in config_LORA.h to 433e6 and now works properly.
If you add the bin the github here you go:https://github.com/htcheroportugal/lora433/blob/master/lora%20433mhz.bin

now missing connect reed switch and code, maybe some member for this community can help.

I flashed this code in Emitter Board:

 #include <SPI.h>
#include <LoRa.h>
#include <Wire.h>  
#include "SSD1306.h" 
#include "images.h"

#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  433E6

unsigned int counter = 0;

SSD1306 display(0x3c, 21, 22);
String rssi = "RSSI --";
String packSize = "--";
String packet ;

 

void setup() {
  pinMode(16,OUTPUT);
  pinMode(2,OUTPUT);
  
  digitalWrite(16, LOW);    // set GPIO16 low to reset OLED
  delay(50); 
  digitalWrite(16, HIGH); // while OLED is running, must set GPIO16 in high
  
  Serial.begin(115200);
  while (!Serial);
  Serial.println();
  Serial.println("LoRa Sender Test");
  
  SPI.begin(SCK,MISO,MOSI,SS);
  LoRa.setPins(SS,RST,DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  //LoRa.onReceive(cbk);
//  LoRa.receive();
  Serial.println("init ok");
  display.init();
  display.flipScreenVertically();  
  display.setFont(ArialMT_Plain_10);
   
  delay(1500);
}

void loop() {
  display.clear();
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);
  
  display.drawString(0, 0, "Sending packet: ");
  display.drawString(90, 0, String(counter));
  Serial.println(String(counter));
  display.display();

  // send packet
  LoRa.beginPacket();
  LoRa.print("hello ");
  LoRa.print(counter);
  LoRa.endPacket();

  counter++;
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  }

I think the code for ReedSwitch is something like that:

const int reedPin = 2;
bool switchState = HIGH;

void setup() 
{
  Serial.begin(9600);
  pinMode(reedPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}

void loop() 
{
 switchState = digitalRead(reedPin);
 if (switchState == LOW) 
  {
    Serial.println("ON");
  } 
 else
  {
   Serial.println("OFF");
  } 

But i dont know how can do it.

I tried do to that but i think something is wrong.

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>  
#include "images.h"

#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  433E6
const int reedPin = 2;
bool switchState = HIGH;

 

void setup() {
  
  Serial.begin(115200);
  pinMode(reedPin, INPUT);
  while (!Serial);
  
  SPI.begin(SCK,MISO,MOSI,SS);
  LoRa.setPins(SS,RST,DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
 
  delay(1500);
}

void loop() {

{
 switchState = digitalRead(reedPin);
 if (switchState == LOW) 
  {
    Serial.println("ON");
  } 
 else
  {
   Serial.println("OFF");
  } 



  // send packet
  LoRa.beginPacket();
  LoRa.print(switchState);
  LoRa.endPacket();

  }
    }

Because i receive repeated messages in loop.
image

Do you see a change in the mqtt payload when the button is pressed.

Glad you put your hands in the code !!

image

When my reed switch is closed i see
image
But always loop the messages does not stop… and the message should be “off” without Hello.

When my reed swich is Open i see “message” : “hello 1”} on loop to…
:face_with_raised_eyebrow:

On arduino IDE serial monitor when opened

when closed

i am very close, but i need help to achieve my goal.

The issue is that you are sending the data even if there is no change between 2 loops. So before sending you need to check if the data changed or not.
So as to do that you need to store the data with a static prefix and compare the reed switch reading with the static data value.

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include "images.h"
#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  433E6
const int reedPin = 2;
bool switchState = HIGH;

bool lastRead = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(reedPin, INPUT);
  while (!Serial);
  SPI.begin(SCK, MISO, MOSI, SS);
  LoRa.setPins(SS, RST, DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  delay(1500);
  lastRead = digitalRead(reedPin);
  sendState(lastRead);
}
void loop() {
  switchState = digitalRead(reedPin);

  if (switchState != lastRead)
  {
    sendState(switchState);
  }
  delay(1000); //ajustavel
}

void sendState(bool state) {
  Serial.print("Sending state: ");
  Serial.println(state);
  LoRa.beginPacket();
  if (state)
    LoRa.print("OFF");
  else
    LoRa.print("ON");
  LoRa.endPacket();
}

Almost but the loop continues…

Done.

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include "images.h"
#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  433E6
const int reedPin = 2;
bool switchState = HIGH;

bool lastRead = false;

void setup() {
  Serial.begin(115200);
  //pinMode(reedPin, INPUT);
  pinMode(reedPin, INPUT_PULLUP);
  while (!Serial);
  SPI.begin(SCK, MISO, MOSI, SS);
  LoRa.setPins(SS, RST, DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting //LoRa failed!");
    while (1);
  }
  delay(1500);
  lastRead = digitalRead(reedPin);
  sendState(lastRead);
}

void loop() {
  switchState = digitalRead(reedPin);

  if (switchState != lastRead)
  {
    lastRead = switchState;
    sendState(switchState);
  }
  delay(1000); //ajustavel
}

void sendState(bool state) {
  Serial.print("Sending state: ");
  Serial.println(state);
  LoRa.beginPacket();
  if (state)
    LoRa.print("ON");
  else
    LoRa.print("OFF");
  LoRa.endPacket();
}

a Portuguese friend helped me with the part I didn’t understand and it’s working properly.
now i want keep a good work with other sensors

great that it’s working!

Feel free to share this code in the docs.

1 Like

Another code tested and working to use with PIR

image

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>  

#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  868E6

//pir
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
bool switchState = LOW;
bool lastRead = false;

 

void setup() {

  pinMode(inputPin, INPUT_PULLUP);
  Serial.begin(115200);
  while (!Serial);
  Serial.println();
  Serial.println("LoRa Sender Test");
  
  SPI.begin(SCK,MISO,MOSI,SS);
  LoRa.setPins(SS,RST,DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }

  delay(1500);
  lastRead = digitalRead(inputPin);
  sendState(lastRead);
}




void loop() {
  switchState = digitalRead(inputPin);

  if (switchState != lastRead)
  {
    lastRead = switchState;
    sendState(switchState);
  }
  delay(1000); //ajustavel
}

void sendState(bool state) {
  Serial.print("Sending state: ");
  Serial.println(state);
  LoRa.beginPacket();
  if (state)
    LoRa.print("ON");
  else
    LoRa.print("OFF");
  LoRa.endPacket();
}

@1technophile

I have been trying to understand how i can send more than one message, but i cant understand.
currently i´m trying to use BME280 sensor… and works more or less…
I got the message:
image

32.6324.6010099,36

I can understand that 32.63 is temperature 24,60 Humidity and 1009,36 Pressure…
How i can send 3 different values?

The code is:

//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26

//433E6 for Asia
//866E6 for Europe
//915E6 for North America
#define BAND 433E6



//BME280 definition
#define SDA 21
#define SCL 13

TwoWire I2Cone = TwoWire(1);
Adafruit_BME280 bme;

//packet counter
int readingID = 0;

int counter = 0;
String LoRaMessage = "";
String temperaturee = "";
String humidityy = "";
String pressuree = "";


float temperature = 0;
float humidity = 0;
float pressure = 0;






//Initialize LoRa module
void startLoRA(){
  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);

  while (!LoRa.begin(BAND) && counter < 10) {
    Serial.print(".");
    counter++;
    delay(500);
  }
  if (counter == 10) {
    // Increment readingID on every new reading
    readingID++;
    Serial.println("Starting LoRa failed!"); 
  }
  Serial.println("LoRa Initialization OK!");
  delay(2000);
}

void startBME(){
  I2Cone.begin(SDA, SCL, 100000); 
  bool status1 = bme.begin(0x76, &I2Cone);  
  if (!status1) {
    Serial.println("Could not find a valid BME280_1 sensor, check wiring!");
    while (1);
  }
}

void getReadings(){
  temperature = bme.readTemperature();
  humidity = bme.readHumidity();
  pressure = bme.readPressure() / 100.0F;
}

void sendReadings() {
  //LoRaMessage = String(readingID) + "/" + String(temperature) + "&" + String(humidity) + "#" + String(pressure);

  //temperaturee = String(temperature);
  //humidityy = String(humidity);
  //pressuree = String(pressure);
  //Send LoRa packet to receiver
  LoRa.beginPacket();
  LoRa.print(temperature);
  LoRa.print(humidity);
  LoRa.print(pressure);
  
  LoRa.endPacket();
  
}

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  startBME();
  startLoRA();
}
void loop() {
  getReadings();
  sendReadings();
  delay(10000);
}

i use Home Assistant and only see ONE sensor too

When producing the LORA message, you should either follow a format to differentiate the values, either produce different messages.

@1technophile Hi, can u show me an example please? I am lost.

I should work on an XtoLORA gateway that will work accordingly a LORAtoMQTT, this is on my plan but not at short term

I was checking the code and it does ot seems to difficult to to it. If you are interested I can publish a fork of OMG with XtoLORA functionnality, you may have to do some configuration and I would be interested by your test results.

yes please, do the fork, i will try because i tested a lot of sensors, tilt, bme280,dht,distance sensor, pir… etc and all works fine. This could be s great project if OMG supported more than 1 message Lora.

image
Code for vibration sensor

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>

#define SCK     5    // GPIO5  -- SX1278's SCK
#define MISO    19   // GPIO19 -- SX1278's MISnO
#define MOSI    27   // GPIO27 -- SX1278's MOSI
#define SS      18   // GPIO18 -- SX1278's CS
#define RST     14   // GPIO14 -- SX1278's RESET
#define DI0     26   // GPIO26 -- SX1278's IRQ(Interrupt Request)
#define BAND  433E6
const int reedPin = 4;
bool switchState = HIGH;

bool lastRead = false;

void setup() {
  Serial.begin(115200);
  //pinMode(reedPin, INPUT);
  pinMode(reedPin, INPUT_PULLUP);
  while (!Serial);
  SPI.begin(SCK, MISO, MOSI, SS);
  LoRa.setPins(SS, RST, DI0);
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting //LoRa failed!");
    while (1);
  }
  delay(1500);
  lastRead = digitalRead(reedPin);
  sendState(lastRead);
}

void loop() {
  switchState = digitalRead(reedPin);

  if (switchState != lastRead)
  {
    lastRead = switchState;
    sendState(switchState);
  }
  delay(1000); //ajustavel
}

void sendState(bool state) {
  Serial.print("Sending state: ");
  Serial.println(state);
  LoRa.beginPacket();
  if (state)
    LoRa.print("ON");
  else
    LoRa.print("OFF");
  LoRa.endPacket();
}