Moteino-USB-LoRa Send and Recieve

Started by martjst, June 21, 2016, 11:52:30 PM

martjst

Is it possible to use two Moteino-USBs for transmit and receive?
 
The following is to code i uploaded to The first  Moteino-USB as a Node
// Sample RFM69 sender/node sketch, with ACK and optional encryption
// Sends periodic messages of increasing length to gateway (id=1)
// It also looks for an onboard FLASH chip, if present
// Library and code by Felix Rusu - [email protected]
// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/

#include <RFM69.h>    //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash

#define NODEID        2    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
#define GATEWAYID     1
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY   RF69_433MHZ
//#define FREQUENCY   RF69_868MHZ
//#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
//#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME      30 // max # of ms to wait for an ack
#ifdef __AVR_ATmega1284P__
  #define LED           15 // Moteino MEGAs have LEDs on D15
  #define FLASH_SS      23 // and FLASH SS on D23
#else
  #define LED           9 // Moteinos have LEDs on D9
  #define FLASH_SS      8 // and FLASH SS on D8
#endif

#define SERIAL_BAUD   115200

int TRANSMITPERIOD = 150; //transmit a packet to gateway so often (in ms)
char payload[] = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char buff[20];
byte sendSize=0;
boolean requestACK = false;
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit  Windbond chip (W25X40CL)
RFM69 radio;

void setup() {
  Serial.begin(SERIAL_BAUD);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
  radio.setHighPower(); //uncomment only for RFM69HW!
#endif
  radio.encrypt(ENCRYPTKEY);
  //radio.setFrequency(919000000); //set frequency to some custom frequency
  char buff[50];
  sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
  
  if (flash.initialize())
  {
    Serial.print("SPI Flash Init OK ... UniqueID (MAC): ");
    flash.readUniqueId();
    for (byte i=0;i<8;i++)
    {
      Serial.print(flash.UNIQUEID[i], HEX);
      Serial.print(' ');
    }
    Serial.println();
  }
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

long lastPeriod = 0;
void loop() {
  //process any serial input
  if (Serial.available() > 0)
  {
    char input = Serial.read();
    if (input >= 48 && input <= 57) //[0,9]
    {
      TRANSMITPERIOD = 100 * (input-48);
      if (TRANSMITPERIOD == 0) TRANSMITPERIOD = 1000;
      Serial.print("\nChanging delay to ");
      Serial.print(TRANSMITPERIOD);
      Serial.println("ms\n");
    }

    if (input == 'r') //d=dump register values
      radio.readAllRegs();
    //if (input == 'E') //E=enable encryption
    //  radio.encrypt(KEY);
    //if (input == 'e') //e=disable encryption
    //  radio.encrypt(null);

    if (input == 'd') //d=dump flash area
    {
      Serial.println("Flash content:");
      uint16_t counter = 0;

      Serial.print("0-256: ");
      while(counter<=256){
        Serial.print(flash.readByte(counter++), HEX);
        Serial.print('.');
      }
      while(flash.busy());
      Serial.println();
    }
    if (input == 'e')
    {
      Serial.print("Erasing Flash chip ... ");
      flash.chipErase();
      while(flash.busy());
      Serial.println("DONE");
    }
    if (input == 'i')
    {
      Serial.print("DeviceID: ");
      word jedecid = flash.readDeviceId();
      Serial.println(jedecid, HEX);
    }
  }

  //check for any received packets
  if (radio.receiveDone())
  {
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    for (byte i = 0; i < radio.DATALEN; i++)
      Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");

    if (radio.ACKRequested())
    {
      radio.sendACK();
      Serial.print(" - ACK sent");
    }
    Blink(LED,3);
    Serial.println();
  }

  int currPeriod = millis()/TRANSMITPERIOD;
  if (currPeriod != lastPeriod)
  {
    lastPeriod=currPeriod;
    
    //send FLASH id
    if(sendSize==0)
    {
      sprintf(buff, "FLASH_MEM_ID:0x%X", flash.readDeviceId());
      byte buffLen=strlen(buff);
      if (radio.sendWithRetry(GATEWAYID, buff, buffLen))
        Serial.print(" ok!");
      else Serial.print(" nothing...");
      //sendSize = (sendSize + 1) % 31;
    }
    else
    {
      Serial.print("Sending[");
      Serial.print(sendSize);
      Serial.print("]: ");
      for(byte i = 0; i < sendSize; i++)
        Serial.print((char)payload[i]);
  
      if (radio.sendWithRetry(GATEWAYID, payload, sendSize))
       Serial.print(" ok!");
      else Serial.print(" nothing...");
    }
    sendSize = (sendSize + 1) % 31;
    Serial.println();
    Blink(LED,3);
  }
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}


Modified: TomWS placed code in code block for readability.



martjst

#1
The following is to code i uploaded to The Second Moteino-USB as a Gateway


// Sample RFM69 receiver/gateway sketch, with ACK and optional encryption
// Passes through any wireless received messages to the serial port & responds to ACKs
// It also looks for an onboard FLASH chip, if present
// Library and code by Felix Rusu - [email protected]
// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/

#include <RFM69.h>    //get it here: https://www.github.com/lowpowerlab/rfm69
#include <SPI.h>
#include <SPIFlash.h> //get it here: https://www.github.com/lowpowerlab/spiflash

#define NODEID        1    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY     RF69_433MHZ
//#define FREQUENCY     RF69_868MHZ
//#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
//#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ACK_TIME      30 // max # of ms to wait for an ack
#define SERIAL_BAUD   115200

#ifdef __AVR_ATmega1284P__
  #define LED           15 // Moteino MEGAs have LEDs on D15
  #define FLASH_SS      23 // and FLASH SS on D23
#else
  #define LED           9 // Moteinos have LEDs on D9
  #define FLASH_SS      8 // and FLASH SS on D8
#endif

RFM69 radio;
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit  Windbond chip (W25X40CL)
bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network

void setup() {
  Serial.begin(SERIAL_BAUD);
  delay(10);
  radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW
  radio.setHighPower(); //only for RFM69HW!
#endif
  radio.encrypt(ENCRYPTKEY);
  radio.promiscuous(promiscuousMode);
  //radio.setFrequency(919000000);
  char buff[50];
  sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915);
  Serial.println(buff);
  if (flash.initialize())
  {
    Serial.print("SPI Flash Init OK. Unique MAC = [");
    flash.readUniqueId();
    for (byte i=0;i<8;i++)
    {
      Serial.print(flash.UNIQUEID[i], HEX);
      if (i!=8) Serial.print(':');
    }
    Serial.println(']');
    
    //alternative way to read it:
    //byte* MAC = flash.readUniqueId();
    //for (byte i=0;i<8;i++)
    //{
    //  Serial.print(MAC[i], HEX);
    //  Serial.print(' ');
    //}
    
  }
  else
    Serial.println("SPI Flash Init FAIL! (is chip present?)");
}

byte ackCount=0;
uint32_t packetCount = 0;
void loop() {
  //process any serial input
  if (Serial.available() > 0)
  {
    char input = Serial.read();
    if (input == 'r') //d=dump all register values
      radio.readAllRegs();
    if (input == 'E') //E=enable encryption
      radio.encrypt(ENCRYPTKEY);
    if (input == 'e') //e=disable encryption
      radio.encrypt(null);
    if (input == 'p')
    {
      promiscuousMode = !promiscuousMode;
      radio.promiscuous(promiscuousMode);
      Serial.print("Promiscuous mode ");Serial.println(promiscuousMode ? "on" : "off");
    }
    
    if (input == 'd') //d=dump flash area
    {
      Serial.println("Flash content:");
      int counter = 0;

      while(counter<=256){
        Serial.print(flash.readByte(counter++), HEX);
        Serial.print('.');
      }
      while(flash.busy());
      Serial.println();
    }
    if (input == 'D')
    {
      Serial.print("Deleting Flash chip ... ");
      flash.chipErase();
      while(flash.busy());
      Serial.println("DONE");
    }
    if (input == 'i')
    {
      Serial.print("DeviceID: ");
      word jedecid = flash.readDeviceId();
      Serial.println(jedecid, HEX);
    }
    if (input == 't')
    {
      byte temperature =  radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient
      byte fTemp = 1.8 * temperature + 32; // 9/5=1.8
      Serial.print( "Radio Temp is ");
      Serial.print(temperature);
      Serial.print("C, ");
      Serial.print(fTemp); //converting to F loses some resolution, obvious when C is on edge between 2 values (ie 26C=78F, 27C=80F)
      Serial.println('F');
    }
  }

  if (radio.receiveDone())
  {
    Serial.print("#[");
    Serial.print(++packetCount);
    Serial.print(']');
    Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] ");
    if (promiscuousMode)
    {
      Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] ");
    }
    for (byte i = 0; i < radio.DATALEN; i++)
      Serial.print((char)radio.DATA[i]);
    Serial.print("   [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");
    
    if (radio.ACKRequested())
    {
      byte theNodeID = radio.SENDERID;
      radio.sendACK();
      Serial.print(" - ACK sent.");

      // When a node requests an ACK, respond to the ACK
      // and also send a packet requesting an ACK (every 3rd one only)
      // This way both TX/RX NODE functions are tested on 1 end at the GATEWAY
      if (ackCount++%3==0)
      {
        Serial.print(" Pinging node ");
        Serial.print(theNodeID);
        Serial.print(" - ACK...");
        delay(3); //need this when sending right after reception .. ?
        if (radio.sendWithRetry(theNodeID, "ACK TEST", 8, 0))  // 0 = only 1 attempt, no retries
          Serial.print("ok!");
        else Serial.print("nothing");
      }
    }
    Serial.println();
    Blink(LED,3);
  }
}

void Blink(byte PIN, int DELAY_MS)
{
  pinMode(PIN, OUTPUT);
  digitalWrite(PIN,HIGH);
  delay(DELAY_MS);
  digitalWrite(PIN,LOW);
}


Modified: TomWS: Placed code in code block for readability

martjst

#2
This is what i get on Serial Monitor


Transmitting at 433 Mhz...
SPI Flash Init OK ... UniqueID (MAC): D7 65 64 43 1B 6E EC 37 
 nothing...
Sending[1]: 1 nothing...
Sending[2]: 12 nothing...
Sending[3]: 123 nothing...
Sending[4]: 123  nothing...
Sending[5]: 123 A nothing...
Sending[6]: 123 AB nothing...
Sending[7]: 123 ABC nothing...
Sending[8]: 123 ABCD nothing...
Sending[9]: 123 ABCDE nothing...
Sending[10]: 123 ABCDEF nothing...
Sending[11]: 123 ABCDEFG nothing...
Sending[12]: 123 ABCDEFGH nothing...
Sending[13]: 123 ABCDEFGHI nothing...
Sending[14]: 123 ABCDEFGHIJ nothing...
Sending[15]: 123 ABCDEFGHIJK nothing...
Sending[16]: 123 ABCDEFGHIJKL nothing...
Sending[17]: 123 ABCDEFGHIJKLM nothing...
Sending[18]: 123 ABCDEFGHIJKLMN nothing...
Sending[19]: 123 ABCDEFGHIJKLMNO nothing...
Sending[20]: 123 ABCDEFGHIJKLMNOP nothing...
Sending[21]: 123 ABCDEFGHIJKLMNOPQ nothing...
Sending[22]: 123 ABCDEFGHIJKLMNOPQR nothing...
Sending[23]: 123 ABCDEFGHIJKLMNOPQRS nothing...
Sending[24]: 123 ABCDEFGHIJKLMNOPQRST nothing...
Sending[25]: 123 ABCDEFGHIJKLMNOPQRSTU nothing...
Sending[26]: 123 ABCDEFGHIJKLMNOPQRSTUV nothing...
Sending[27]: 123 ABCDEFGHIJKLMNOPQRSTUVW nothing...
Sending[28]: 123 ABCDEFGHIJKLMNOPQRSTUVWX nothing...
Sending[29]: 123 ABCDEFGHIJKLMNOPQRSTUVWXY nothing...
Sending[30]: 123 ABCDEFGHIJKLMNOPQRSTUVWXYZ nothing...
 nothing...
Sending[1]: 1 nothing...
Sending[2]: 12 nothing...
Sending[3]: 123 nothing...
Sending[4]: 123  nothing...
Sending[5]: 123 A nothing...
Sending[6]: 123 AB nothing...
Sending[7]: 123 ABC nothing...
Sending[8]: 123 ABCD nothing...
Sending[9]: 123 ABCDE nothing...
Sending[10]: 123 ABCDEF nothing...
Sending[11]: 123 ABCDEFG nothing...
Sending[12]: 123 ABCDEFGH nothing...
Sending[13]: 123 ABCDEFGHI nothing...
Sending[14]: 123 ABCDEFGHIJ nothing...
Sending[15]: 123 ABCDEFGHIJK nothing...
Sending[16]: 123 ABCDEFGHIJKL nothing...
Sending[17]: 123 ABCDEFGHIJKLM nothing...
Sending[18]: 123 ABCDEFGHIJKLMN nothing...
Sending[19]: 123 ABCDEFGHIJKLMNO nothing...
Sending[20]: 123 ABCDEFGHIJKLMNOP nothing...
Sending[21]: 123 ABCDEFGHIJKLMNOPQ nothing...
Sending[22]: 123 ABCDEFGHIJKLMNOPQR nothing...
Sending[23]: 123 ABCDEFGHIJKLMNOPQRS nothing...
Sending[24]: 123 ABCDEFGHIJKLMNOPQRST nothing...
Sending[25]: 123 ABCDEFGHIJKLMNOPQRSTU nothing...
Sending[26]: 123 ABCDEFGHIJKLMNOPQRSTUV nothing...
Sending[27]: 123 ABCDEFGHIJKLMNOPQRSTUVW nothing...
Sending[28]: 123 ABCDEFGHIJKLMNOPQRSTUVWX nothing...
Sending[29]: 123 ABCDEFGHIJKLMNOPQRSTUVWXY nothing...
Sending[30]: 123 ABCDEFGHIJKLMNOPQRSTUVWXYZ nothing...
 nothing...
Sending[1]: 1 nothing...
Sending[2]: 12 nothing...
Sending[3]: 123 nothing...
Sending[4]: 123  nothing...
Sending[5]: 123 A nothing...
Sending[6]: 123 AB nothing..


Modified: TomWS: Placed output into code block for readability.

martjst

#3
This is what i get on Serial Monitor (Gateway)




Listening at 433 Mhz...
SPI Flash Init OK. Unique MAC = [D7:65:64:43:1B:7E:98:37:]

TomWS

Quote from: martjst on June 21, 2016, 11:52:30 PM
Is it possible to use two Moteino-USBs for transmit and receive?
This question is incomplete.  The answer is obviously yes, under the right circumstances.  You need to cite the conditions and problem you're having in more detail if you expect any help.

Tom

martjst

Thanks for your reply and cleaning up the code i posted. I do not know how you did that.
I am new to radio communication, i did some research and came across the LowPowerLab website and i bought 2 Moteino-USB
Transcievers RFM96 (LoRa 433mhz). Now i am trying to make them talk to each other with the code i posted, which i got at http://lowpowerlab.com/programming/. Why do i not get the same results?

Felix

martjst,
For LoRa radios you will need to use the RadioHead library, not the RFM69 library.
Please see this page for more on how to start on that.

TomWS

Quote from: martjst on June 22, 2016, 11:06:44 AM
Thanks for your reply and cleaning up the code i posted. I do not know how you did that.
When you do a posting you will see a small button about midway over your text.  The button has a '#' on it for some reason  ;)
If you click the button you will get a section with '[code][/code]' in it.  Insert your code or text in between the 'code' tags and they will be displayed in a neat, scrollable block.

Tom

martjst

Thank you Felix the radiohead libraries work. i got the two LoRa Radios to send and receive cool.
That leads to another question I also have a mighty hat with the  Transceiver RFM96 (LoRa 433mhz)
Do you have code built for the mighty hat that will send messages the pi gateway with that Radio?

martjst


Felix

Quote from: martjst on June 22, 2016, 03:45:49 PM
Thank you Felix the radiohead libraries work. i got the two LoRa Radios to send and receive cool.
That leads to another question I also have a mighty hat with the  Transceiver RFM96 (LoRa 433mhz)
Do you have code built for the mighty hat that will send messages the pi gateway with that Radio?
Unfortunately I do not, what you can do is adapt the MightyHat code into a sending/receiving sketch. I have not done that since I do not use LoRa radios for my home automation at the moment.