Author Topic: Dynamic Device Name with Gazell Protocol  (Read 835 times)

MRF

  • RFduino Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Dynamic Device Name with Gazell Protocol
« on: October 26, 2016, 02:05:27 AM »
Hello,

I'm currently setting up a low-energy wireless sensor system using the RFduino and the Gazell Protocol.

With the Gazell Protocol, an DeviceName like DEVICE0, DEVICE1,... is needed for communication.
In the Setup, I'm giving all Devices the Name DEVICE7, but after this, the program loads data from a page from the Flash, where a variable which determines the real DeviceNumber is stored, to a struct called "value".
Then the programs runs an if-loop to Change the Name, but it doesn't seem to work.  :(
I tought I could stop the Gazell Stack, change the Name and then start it again, but it doesn't work.
My change code looks somehow like this:

if(value.DeviceNumber == 1)
     {
       device_t role = DEVICE0;
     }
     if(value.DeviceNumber == 2)
     {
       device_t role = DEVICE1;
     }
....................

Is it anyhow possible to Change the DeviceName during operation?
Any Help or Tips arewelcome!
I'm sorry for my bad english, I am german.  :)

Greetings, MRF

tolson

  • Global Moderator
  • *****
  • Posts: 854
  • Karma: +20/-0
    • View Profile
    • Thomas Olson Consulting
Re: Dynamic Device Name with Gazell Protocol
« Reply #1 on: October 26, 2016, 12:10:31 PM »
Perhaps you can show how you are stopping an starting the GZZL. Can you show a script we can load and play with to replicate what you are getting. Snippits don't help much, because there could be some other issue too.

MRF

  • RFduino Newbie
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Dynamic Device Name with Gazell Protocol
« Reply #2 on: October 27, 2016, 01:32:35 AM »
Hi, here is a bigger part of my code, which you can upload and compile.
You can set the DeviceNumber if you type in '1'-'6', if you type in '9' you can set a new SerialNumber with 8 Digits.
The Problem is, the saved DeviceNumber is changing, but the DEVICE can't change it's number.
Thanks for your help!  :)


Code: [Select]
#include <RFduinoGZLL.h> // GZLL-Bibliothek

// select a flash page that isn't in use (see Memory.h for more info)
#define  MY_FLASH_PAGE  251
// double level of indirection required to get gcc
// to apply the stringizing operator correctly
#define  str(x)   xstr(x)
#define  xstr(x)  #x

device_t role = DEVICE7; // Setup as Device 8

struct data_init
{
  int DeviceNumber;
  char SerialNumber[8];
  bool set;
};
struct data_init value;
data_init *p = (data_init*)ADDRESS_OF_PAGE(MY_FLASH_PAGE); // Initalize from Flash Page 251


void flash_output( struct data_init *p )
{
  Serial.println("");
  Serial.println("Page " str(MY_FLASH_PAGE) " contains:");
  Serial.print("DeviceNumber = ");
  Serial.println(p->DeviceNumber);
  Serial.print("SerialNumber = ");
  Serial.println(p->SerialNumber);
  Serial.println("");
  Serial.println("If you want to change the DeviceNumber,");
  Serial.println("insert a number between 1 and 6.");
  value.DeviceNumber = p->DeviceNumber;
  value.set = p->set;
  for (int i = 0; i <= 7; i++)
   {             
     value.SerialNumber[i] = p->SerialNumber[i];
   }
}

void Einlesen()
{
 RFduinoGZLL.end();
 if( value.set == 1)
    {
     String number = Serial.readString();   // Eingabe speichern
     number.toCharArray(value.SerialNumber,9);
     int rc = flashPageErase(PAGE_FROM_ADDRESS(p));
     rc = flashWriteBlock(p, &value, sizeof(value));
     flash_output(p);
     value.set = 0;
    }
    if( value.set == 0)
    {
     int number = Serial.read()-'0';   // save input in puffer
     if( number >= 1 and number <= 6)
      {
        value.DeviceNumber = { number };
        int rc = flashPageErase(PAGE_FROM_ADDRESS(p));
        rc = flashWriteBlock(p, &value, sizeof(value));   // write it in the flash
        flash_output(p);
      }
     if( number == 9 )
      {
        value.set = 1;
        Serial.println("");
        Serial.println("Now you can set a new SerialNumber with 8 digits!");
      }
  }
}
 
void setup()
{
  RFduinoGZLL.txPowerLevel = +4; //transmit power in dBm (-20 to +4)
  // start Serial
  Serial.begin(115200);

  flash_output(p);
   RFduinoGZLL.end();
     if(value.DeviceNumber == 1)
     {
       device_t role = DEVICE0;
     }
     if(value.DeviceNumber == 2)
     {
       device_t role = DEVICE1;
     }
     if(value.DeviceNumber == 3)
     {
       device_t role = DEVICE2;
     }
     if(value.DeviceNumber == 4)
     {
       device_t role = DEVICE3;
     }
     if(value.DeviceNumber == 5)
     {
       device_t role = DEVICE4;
     }
     if(value.DeviceNumber == 6)
     {
       device_t role = DEVICE5;
     }
}

void loop()
{
  RFduinoGZLL.end();
  if( Serial.available() > 0 )
  {
    Einlesen();
  } 
}

flurin4

  • RFduino Newbie
  • *
  • Posts: 16
  • Karma: +0/-0
    • View Profile
Re: Dynamic Device Name with Gazell Protocol
« Reply #3 on: November 30, 2016, 06:05:45 AM »
Hi MRF,

I tried going through your code to understand your problem.
First, I have two suggestions for readability. I don't want to be here lecturing on coding style, but it helps if others can read your code more easily.
I would recommend using True/False with boolean data types, to make clear for the reader that we are treating with such a variable.
Also try to keep same-level statements at a same indentation. This is visible in your Einlesen() function, where following two have different indentations:
Code: [Select]
if( value.set == 1)
    {
    ...
    }
    if( value.set == 0)
    {
    ...
    }
even if they have same "level" of importance. This makes understanding your code already harder.
Compare this to
Code: [Select]
if (value.set == True)
    {
    ...
    }
if (value.set == False)
    {
    ...
    }
It might be seen as a minor thing, but readers that can understand your code easily are also more ready to help out.

Now, to the problem, I wasn't able to compile and upload your example, but I think that your struct data_init called "value" is redefined to a random value at startup?
So when you look at value.DeviceNumber in the setup() (this is the FIRST thing the module does!), it doesn't meet any condition. Is that possible? Let me know if I'm wrong.
Also, what does your "int rc" return?

I'll see wether I can get to upload your code and look at it.
Flurin