Teensy 3.x and Dragino Lora Bee Question/Problem

Status
Not open for further replies.

Jenne

New member
Hi,

I tried to connect 2 Lora Bee (868MHz) to 2 Teensy 3.5. I run the master/client example and it worked as long as both devices (Teensy+Lora) are connected to my computer via USB (running both in separate terminals). However, my goal is to disconnect one device from my computer, run it autonomously from battery and send sensor data to the other one. But this is not working at the moment. As soon as I power one Teensy+Lora from battery (or USB external power supply) no data are transmitted. My guess is that this has something to do with interrupts. Sorry for my stupid question - but what do I need to change in the code that I can plug off the USB cable?

I run the "standard" RadioHead-Library example, adopting to "my" pins:

Client:
Code:
#include <SPI.h>
#include <RH_RF95.h>

RH_RF95 rf95(15,14);

void setup() 
{
  pinMode(24, OUTPUT);
  delay(100);
  digitalWrite(24, HIGH);

  delay(100);
  digitalWrite(24, LOW);
  delay(10);
  digitalWrite(24, HIGH);
  delay(10);
  
  Serial.begin(9600);
  delay(1000);
  
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
//  rf95.setTxPower(5, true);
  rf95.setFrequency(868.0);
}


void loop()
{
  Serial.println("Sending to rf95_server");
  // Send a message to rf95_server
  uint8_t data[] = "Hello World!";
  rf95.send(data, sizeof(data));
  
  rf95.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (rf95.waitAvailableTimeout(3000))
  { 
    // Should be a reply message for us now   
    if (rf95.recv(buf, &len))
   {
      Serial.print("got reply: ");
      Serial.println((char*)buf);
//      Serial.print("RSSI: ");
//      Serial.println(rf95.lastRssi(), DEC);    
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is rf95_server running?");
  }
  delay(400);
}

server:
Code:
#include <SPI.h>
#include <RH_RF95.h>

RH_RF95 rf95(15,14);

int led = 9;

void setup() 
{
  pinMode(24, OUTPUT);
  digitalWrite(24, HIGH);

  delay(100);
  digitalWrite(24, LOW);
  delay(10);
  digitalWrite(24, HIGH);
  delay(10);

  pinMode(led, OUTPUT);     
  Serial.begin(9600);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");  
  
 // rf95.setTxPower(5, true);
  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);
    if (rf95.recv(buf, &len))
    {
      digitalWrite(led, HIGH);
//      RH_RF95::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
//      Serial.print("RSSI: ");
//      Serial.println(rf95.lastRssi(), DEC);
      
      // Send a reply
      uint8_t data[] = "And hello back to you";
      rf95.send(data, sizeof(data));
      rf95.waitPacketSent();
      Serial.println("Sent a reply");
       digitalWrite(led, LOW);
    }
    else
    {
      Serial.println("recv failed");
    }
  }

  delay(1);
}

thanks a lot
 
Both your client and server examples hang waiting for the USB Serial port to come ready:
"while (!Serial) ; // Wait for serial port to be available"

If the USB isn't there, the code will never get past the while loop.

Either comment out the while on the unit you wish to disconnect, or add a timeout to the while loop.
 
Status
Not open for further replies.
Back
Top