Author Topic: Gazell Host Stuck  (Read 665 times)

adamh

  • RFduino Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
Gazell Host Stuck
« on: March 11, 2016, 02:05:07 AM »
I'm experiencing a bug I don't know how to tackle, and any help would be much appreciated.

I have a device (1 for now, plan to add more later) with a led, and a host that's connected to the computer, and controls the device. I want to be able to (1) turn the led on a off and (2) send battery queries from the host to the device. The device's battery should be as small as possible, so I'm trying to make it as energy efficient as I can.

The problem: the code works fine for 3-10 times (both the led and the battery query) and than gets stuck. It doesn't always gets stuck in the same iteration - sometimes after 2 on commands and 1 query, sometimes after 5 ons, etc...

This is the code on the device:
Code: [Select]
#include <RFduinoGZLL.h>
device_t role = DEVICE1;

// Variables declarations
int green_led;               // pin for the Green Led

struct infoPacket
{
  unsigned long timeToSleep; 
  bool state;
  unsigned int pulseTime;
  byte batteryLevel;
  bool checkBattery;
};

infoPacket packet;


void setup()
{
  // init defaults
  packet.timeToSleep = 190;        // in ms
  packet.pulseTime = 500;          // in ms
  packet.checkBattery = false;
  packet.state = false;
  green_led = 6;
 
  pinMode(green_led, OUTPUT);
  digitalWrite(green_led, LOW);
  RFduinoGZLL.txPowerLevel = -20;

  // start the GZLL stack
  RFduinoGZLL.begin(role);
}

void loop()
{
  RFduino_ULPDelay(packet.timeToSleep);

  if (packet.checkBattery == true) {
    // send battery info to host
    packet.checkBattery = false;
    analogReference(VBG); // Sets the Reference to 1.2V band gap           
    analogSelection(VDD_1_3_PS);  //Selects VDD with 1/3 prescaling as the analog source
    int sensorValue = analogRead(1); // the pin has no meaning, it uses VDD pin
    byte batteryVoltage = sensorValue * (3.6 / 1023.0) * 55.0; // convert value to voltage
    RFduinoGZLL.sendToHost(batteryVoltage);
  }
  else {RFduinoGZLL.sendToHost(NULL,0);}
  delay(10); 

}

void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
  // ignore acknowledgement without payload
  if (len > 0)
  {
    // init packet received
    memcpy( &packet, &data[0], len);
      if (packet.state == true)
      {
        digitalWrite(green_led, HIGH);
        delay(packet.pulseTime);
        digitalWrite(green_led, LOW);
      }
  }
}

And this is the code on the host:
Code: [Select]
#include <RFduinoGZLL.h>
device_t role = HOST;

struct infoPacket
{
  unsigned long timeToSleep; 
  bool state;
  unsigned int pulseTime;
  byte batteryLevel;
  bool checkBattery;
};

// variable declarations:
const int numberOfDevices = 2;
boolean ledsArray[numberOfDevices];
int deviceNumber;
int actionType;
infoPacket packet;

void setup()
{
  Serial.begin(9600);

  // init boolean array such that all leds are off in the beginning
  for (int thisDevice = 0; thisDevice < numberOfDevices; thisDevice++) {
    ledsArray[thisDevice] = false;
  }
 
  // init params
  Serial.println("Initializing parameters");
  Serial.println("Enter timeToSleep && pulseTime:");
  while(Serial.available() == 0){}
  packet.timeToSleep = long(Serial.parseInt());
  packet.pulseTime = Serial.parseInt();
  Serial.println("Starting host loop. Enter device number and actionType (1 = trigger, 2 = check batt):");
 
  // start the GZLL stack
  RFduinoGZLL.begin(role);
}

void loop()
{
  while (Serial.available() > 0)
  {
    deviceNumber = Serial.parseInt();
    actionType = Serial.parseInt();
    Serial.println(deviceNumber);
    Serial.println(actionType);
    ledsArray[deviceNumber] = true;
    Serial.println("Enter device number and actionType (1 = trigger, 2 = check batt):");
  }
 
}

void RFduinoGZLL_onReceive(device_t device, int rssi, char *data, int len)
{
  if (!(len == 0)) {
    Serial.print("Recieved non-empty packet from DEVICE1: ");
    Serial.println(float(byte(*data))/55.0);
  }
  else {
    packet.state = false;
    if (device == DEVICE1) {
      if (ledsArray[deviceNumber] == true) {
        ledsArray[deviceNumber] = false;
        if (actionType == 1) {
          Serial.println("Sending trigger to DEVICE1");
          packet.state = true;
          RFduinoGZLL.sendToDevice(device, (char *)&packet, sizeof(packet));
          packet.state = false;
        }
        else if (actionType == 2) {
          Serial.println("Sending batt request to DEVICE1");
          packet.checkBattery = true;
          RFduinoGZLL.sendToDevice(device, (char *)&packet, sizeof(packet));
          packet.checkBattery = false;
        }
      }
      else {RFduinoGZLL.sendToDevice(device, (char *)&packet, sizeof(packet));}
    }
  }

}

Any idea what might cause such problem?

VLorz

  • RFduino Full Member
  • ***
  • Posts: 68
  • Karma: +2/-0
    • View Profile
Re: Gazell Host Stuck
« Reply #1 on: March 25, 2016, 10:11:50 AM »
adamh,
Perhaps your 10ms delay in your device code is too short when you have more air-traffic. You have included one Serial.println() call in your server code before calling RFduinoGZLL.sendToDevice(device, (char *)&packet, sizeof(packet)); and it takes a lot of time to complete in the RFduino. Try changing the call order or try implementing another type of GZLL "on" time control strategy (using the host's 0 length ACK packet, may be?).