Go Down

Topic: grove lora radio remote (Read 420 times) previous topic - next topic

steeltechperformance

Hello mates, im trying to make a remote control with a lora module.
http://wiki.seeedstudio.com/Grove_LoRa_Radio/
im using 2 arduino nano, and im having problems with the coding, so the transmitter is allways sending ON message and when i push a putton is sending OFF. can someone help me with the receiver code so i can turn the bulit in led on and off depending on the message received? at this moment the receiver code will show me on the serial monitor ON or OFF depending if the button is pressed or not.

Transmitter

Code: [Select]
#include <RH_RF95.h>
const int buttonPin = 2;
int buttonState = 0; 
#ifdef __AVR__
#include <SoftwareSerial.h>
SoftwareSerial SSerial(10, 11); // RX, TX
#define COMSerial SSerial
#define ShowSerial Serial
RH_RF95<SoftwareSerial> rf95(COMSerial);
#endif
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define COMSerial Serial1
#define ShowSerial SerialUSB
RH_RF95<Uart> rf95(COMSerial);
#endif
#ifdef ARDUINO_ARCH_STM32F4
#define COMSerial Serial
#define ShowSerial SerialUSB
RH_RF95<HardwareSerial> rf95(COMSerial);
#endif

void setup()
{
    ShowSerial.begin(115200);
    ShowSerial.println("RF95 client test.");
    pinMode(buttonPin, INPUT);
   
    if (!rf95.init())
    {
        ShowSerial.println("init failed");
        while(1);
    }

    // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

    // The default transmitter power is 13dBm, using PA_BOOST.
    // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
    // you can set transmitter powers from 5 to 23 dBm:
    //rf95.setTxPower(13, false);
   
    rf95.setFrequency(868.0);
}

void loop() {
  ShowSerial.println("Sending to rf95_server");
    // Send a message to rf95_server
    rf95.waitPacketSent();
    // Now wait for a reply
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
   // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);
   // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
 
  if (buttonState == HIGH) {
    // turn LED on:
     uint8_t data[] = "ON";
           rf95.send(data, sizeof(data));
  }
  else {
    // turn LED off:
      uint8_t data[] = "OFF";
           rf95.send(data, sizeof(data));
            ShowSerial.println("recv failed");
  }
}


Receiver

Code: [Select]
#include <RH_RF95.h>
#ifdef __AVR__
#include <SoftwareSerial.h>
SoftwareSerial SSerial(10, 11); // RX, TX
#define COMSerial SSerial
#define ShowSerial Serial
RH_RF95<SoftwareSerial> rf95(COMSerial);
#endif
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define COMSerial Serial1
#define ShowSerial SerialUSB
RH_RF95<Uart> rf95(COMSerial);
#endif
#ifdef ARDUINO_ARCH_STM32F4
#define COMSerial Serial
#define ShowSerial SerialUSB
RH_RF95<HardwareSerial> rf95(COMSerial);
#endif
int ledPin = LED_BUILTIN;
int ledState = HIGH;
void setup()
{
    ShowSerial.begin(115200);
    ShowSerial.println("RF95 server test.");
    pinMode(ledPin, OUTPUT);
   
    if (!rf95.init())
    {
        ShowSerial.println("init failed");
        while(1);
    }
    // Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on

    // The default transmitter power is 13dBm, using PA_BOOST.
    // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
    // you can set transmitter powers from 5 to 23 dBm:
    //rf95.setTxPower(13, false);
    rf95.setFrequency(868.0);
}

void loop() {
  if(rf95.available())
  {
    // Should be a message for us now   
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    ShowSerial.println((char*)buf);
    if(rf95.recv(buf, &len))
       ShowSerial.println((char*)buf);
  }
  } 

ZPedro15

Hi,
I'm trying to do something like that too and can't pass the "init failed" message. Can you show how did you connect the grove board to the arduino nano?

Go Up