Weirdest problem with Moteino code [solution: use 16byte encrypt key!]

Started by ydylwj23, August 16, 2019, 02:48:08 PM

ydylwj23

Hey guys,

So I was trying to simplify the example code from the awesome RFM69 library when I encountered this weird issue. There is this line of code: "sprintf(buff1, "\nTransmitting at %d Mhz...", radio.getFrequency()/1000000);" that doesn't really have anything to do with the core function of the Moteino so I thought I should just delete it for my own use. But after I have done that, the Moteino cannot successfully send any package out to the destination when I send the command through serial port like: "2:Hi!". And it was working just fine before I did that.

It took a while for me to hunt down the problem because I would never have thought it was that line of code. So I start to modify the line little by little and realize in order for the send function to work, the line has to at least be like this: "sprintf(buff1, "\nTransm%d", radio.getFrequency()/1000000)". I thought about the length of the array and memory issue but if I replace any of the char in the string, it still won't work!

I thought I'd better post this just to share this unique experience and ask for help. Please slam me if I've done any stupid error that I didn't realize. But just to clarify, I have tried many many times: Deleting the line or trimming the line too much will result in message not able to be sent; With the line, it's completely fine.

Cheers!

// **********************************************************************************************************
// Moteino gateway/base sketch that works with Moteinos equipped with RFM69W/RFM69HW/RFM69CW/RFM69HCW
// This is a basic gateway sketch that receives packets from end node Moteinos, formats them as ASCII strings
//      with the end node [ID] and passes them to Pi/host computer via serial port
//     (ex: "messageFromNode" from node 123 gets passed to serial as "[123] messageFromNode")
// It also listens to serial messages that should be sent to listening end nodes
//     (ex: "123:messageToNode" sends "messageToNode" to node 123)
// Make sure to adjust the settings to match your transceiver settings (frequency, HW etc).
// **********************************************************************************
// Copyright Felix Rusu 2016, http://www.LowPowerLab.com/contact
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it 
// and/or modify it under the terms of the GNU General    
// Public License as published by the Free Software       
// Foundation; either version 3 of the License, or        
// (at your option) any later version.                    
//                                                        
// This program is distributed in the hope that it will   
// be useful, but WITHOUT ANY WARRANTY; without even the  
// implied warranty of MERCHANTABILITY or FITNESS FOR A   
// PARTICULAR PURPOSE. See the GNU General Public        
// License for more details.                              
//                                                  
// Licence can be viewed at                               
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
// **********************************************************************************
#include <RFM69.h>         //get it here: https://github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h>     //get it here: https://github.com/lowpowerlab/RFM69
#include <RFM69_OTA.h>     //get it here: https://github.com/lowpowerlab/RFM69
#include <SPIFlash.h>      //get it here: https://github.com/lowpowerlab/spiflash
#include <SPI.h>           //included with Arduino IDE (www.arduino.cc)

//****************************************************************************************************************
//**** IMPORTANT RADIO SETTINGS - YOU MUST CHANGE/CONFIGURE TO MATCH YOUR HARDWARE TRANSCEIVER CONFIGURATION! ****
//****************************************************************************************************************
#define NODEID          1 //the ID of this node
#define NETWORKID     100 //the network ID of all nodes this node listens/talks to
#define FREQUENCY     RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define ENCRYPTKEY    "11235813" //identical 16 characters/bytes on all nodes, not more not less!
#define IS_RFM69HW_HCW  //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
#define ACK_TIME       30  // # of ms to wait for an ack packet
//*****************************************************************************************************************************
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL
#define ATC_RSSI      -75  //target RSSI for RFM69_ATC (recommended > -80)
//*****************************************************************************************************************************

kobuki

Hmm... Does it work if you replace the sprintf() code with something like delay(100)?

TomWS

@ydylwj23, perhaps if you used a 16 byte encrypt key like the comment line instructs, it would work better...

In cases like you're experiencing, where adding or modifying seemingly unrelated code affect execution, it generally hints at your variable storage getting inadvertently changed.  Using the correct data sizes helps a lot.

kobuki

Ah well. The library code indeed makes assumptions about the length of the key. Having it too short could indeed potentially wreak havoc or cause weird errors like this. I'm curious.

TomWS

Quote from: kobuki on August 18, 2019, 07:46:00 AM
Ah well. The library code indeed makes assumptions about the length of the key. Having it too short could indeed potentially wreak havoc or cause weird errors like this. I'm curious.
I'll wager that the encrypt key actually being used ends with: "\nTransm" on both ends...

Felix

Quote from: TomWS on August 18, 2019, 07:37:51 AM
@ydylwj23, perhaps if you used a 16 byte encrypt key like the comment line instructs, it would work better...
Confirmed.

#define ENCRYPTKEY    "11235813" //identical 16 characters/bytes on all nodes, not more not less!


I guess that comment qualifies as TLDR;

ydylwj23

Quote from: TomWS on August 18, 2019, 07:37:51 AM
@ydylwj23, perhaps if you used a 16 byte encrypt key like the comment line instructs, it would work better...

In cases like you're experiencing, where adding or modifying seemingly unrelated code affect execution, it generally hints at your variable storage getting inadvertently changed.  Using the correct data sizes helps a lot.

OMG... I would never think about this... Now everything makes total sense! Thank you!