NFR24 radio, instructie NOT radio.available

Arduino shields
Berichten: 68
Geregistreerd: 21 Apr 2013, 22:48
Woonplaats: Hoorn

NFR24 radio, instructie NOT radio.available

Berichtdoor babbelkwebbel » 20 Dec 2018, 15:32

Goede dag,

Ik ben sinds kort met de RFL24 radio's modules aan de gang en heb alles werkend, echter op 1 ding na, ik krijg het niet voor elkaar om er achter te komen dat de zender om welke reden geen data meer verstuurt, was kennelijk iets te simpel gedacht met de volgende instructie, als voorbeeld.

if (!radio.available)
{
Serial.println("radio signal is lost !!"):
}

Tevens de cpp file bekeken van de library RF24, maar daar wordt ik niet veel wijzer van, zelf heb ik het idee dat er een buffer in de "ontvanger" waarin de laatste verzonden data in blijft staan, als ik de zender uitzet, dan blijft namelijk evengoed de laatste data in de ontvanger staan.

Waar het mij om gaat dat ik kan detecteren als het signaal van de zender om welke reden dan ook weg valt.

Zie het stukje void RF24_radio

Iemand hier bekend mee?.

bij voorbaat dank
Erik.










Code: Alles selecteren

#include <Wire.h>                                                                                   // I2C library
#include <SPI.h>                                                                                    // SPI library
#include <nRF24L01.h>                                                                               // NRF24 radio library
#include <RF24.h>                                                                                   // NRF24 radio library
RF24 radio(7, 8);                                                                                   // declare CE and CSN pins for NRF24 radio
bool newData = false;
const byte Address[6]  = "00001";                                                                   // set node adress for NRF24 radio
int dataReceived[5];                 

int cleaning_mode               = 1;
int special_days                = 1;
int buzzer_led                  = 0;
int buzzer_led_timer_state      = 0;
int run_once_radio              = 0;
int run_once_door_drawer_timer  = 0;
int run_once_reedswitch         = 0;                                                             // variable to make sure routine will runs only one time (reedswitch)
int run_once_special_days       = 0;                                                             // variable to make sure routine will runs only one time
int intensity_worktop_led       = 0;                                                                 // holds value of intensity for led worktop PWM ( no function with oven cabinets )
int intensity_plinth_led        = 0;                                                                 // holds value of intensity for led plinth  PWM
byte reedswitch;                                                                                    // holds 8 bits value of the A port from the MC23017, (reed switches)
const int power_supply         =  4;                                                                // set power supply switching on/off (solid state relay) to digital 4
const int alarm_buzzer         =  5;                                                                // set alarm buzzer to digital  5
const int plinth_led           = 10;                                                                // set led plinth   to digital 10
const int worktop_led          =  9;                                                                // set led worktop  to digital 11
const long ddinterval       = 10000;                                                                // door drawer timer, time is 10 minutes
unsigned long ddpreviousMillis =  0;                                                                // will store last time there was updated
const long bbinterval        = 1000;                                                                // buzzer and blinking led timer, time is 1 second
unsigned long bbpreviousMillis =  0;                                                                // will store last time there was updated
const long radiointerval     = 5000;
unsigned long radiopreviousMillis =  0;
unsigned long timer;
//================================================================================================================================================================

void setup()
{
  Serial.begin(115200);                   // remove if your are done!!!!!##########
  Wire.begin();                                                                                     // start I2C bus

  // setup NRF24 rf radio
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setChannel(102);
  radio.openReadingPipe(0, Address);
  radio.startListening();

  // setup MC23017 I2C expander
  Wire.beginTransmission(0x20);                                                                     // I2C adress of the MCP23017
  Wire.write(0x00);                                                                                 // IODIRA register (I/O DIRECTION REGISTER)
  Wire.write(0xFF);                                                                                 // set all pins of bank A to be imputs (o=output / 1=input)
  Wire.endTransmission();                                                                           // end of transmission
  Wire.beginTransmission(0x20);                                                                     // I2C adress of the MCP23017
  Wire.write(0x01);                                                                                 // IODIRB register (I/O DIRECTION REGISTER)
  Wire.write(0x00);                                                                                 // set all pins of bank B to be outputs (o=output / 1=input)
  Wire.endTransmission();                                                                           // end of transmission

  // declare inputs and outputs
  pinMode(power_supply,      OUTPUT);                                                               // declare digital  4 to be output (solid state relais)
  pinMode(alarm_buzzer,      OUTPUT);                                                               // declare digital  5 to be output (buzzer)
  pinMode(plinth_led,        OUTPUT);                                                               // declare digital 10 to be output (PWM)
  pinMode(worktop_led,       OUTPUT);                                                               // declare digital  9 to be output (PWM)
}
//##############test programma maken ######################
//================================================================================================================================================================

void loop()
{
  RF_radio();
  door_drawer_timer();
  buzzer_led_timer();
  MC23017();
  days();
}

//================================================================================================================================================================

void MC23017()                                                                                      // I2C expander for reading reedswitches of drawers and doors, and controlling leds in cabinets.
{
  //********************* getting information from MC23017 *******************

  Wire.beginTransmission(0x20);                                                                     // I2C adress of the MCP23017
  Wire.write(0x12);                                                                                 // GPIOA (GENERAL PURPOSE I/O PORT REGISTER)
  Wire.endTransmission();                                                                           // end of transmission
  Wire.requestFrom(0x20, 1);                                                                        // read the inputs of bank A (request 1 BYTE)
  reedswitch = Wire.read();                                                                         // value of wire.read to variable reedswitch , these are the reedswitches in the cabinets
  Wire.beginTransmission(0x20);                                                                     // I2C adress of the MCP23017
  Wire.write(0x13);                                                                                 // GPIOB ((GENERAL PURPOSE I/O PORT REGISTER)
  Wire.write(reedswitch);                                                                           // writing variable reedswitch to GPIOB , write value of reedswitches to the leds in the cabinets
  Wire.endTransmission();                                                                           // end of transmission

  // ------------------------ door and/or drawers open -----------------------------------------------

  if ((reedswitch != 0) && (run_once_reedswitch == 0))                                                // check to see of a door or/and drawer is open?
  {
    run_once_reedswitch = 1;                                                                     // setting HIGH to lock routine
    digitalWrite(power_supply, HIGH);
    analogWrite (worktop_led, 250);                                                                // switch on worktop led
  }

  // ------------------------ door and/or drawers closed ----------------------------------------------

  if ((reedswitch == 0) && (run_once_reedswitch == 1))                                              // if all doors and drawers are closed, go on
  {
    run_once_reedswitch = 0;                                                                      // setting LOW to un-lock routine
    buzzer_led = 0;
  }

  //================================================================================================================================================================

  if ((intensity_worktop_led != 0) && (reedswitch == 0) && (buzzer_led == 0))
  {
    digitalWrite(power_supply, HIGH);
    analogWrite (worktop_led, (intensity_worktop_led));
  }

  if ((intensity_worktop_led == 0) && (reedswitch == 0) && (buzzer_led == 0))
  {
    analogWrite (worktop_led, 0);
  }

  if ((intensity_plinth_led != 0) && (buzzer_led == 0))
  {
    digitalWrite(power_supply, HIGH);
    analogWrite (plinth_led, (intensity_plinth_led));
  }

  if ((intensity_plinth_led == 0) && (buzzer_led == 0))
  {
    analogWrite (plinth_led, 0);
  }

  if (special_days == 0 && reedswitch == 0 && cleaning_mode == 1)
  {
    digitalWrite(power_supply, HIGH);
    buzzer_led = 1;
  }

  if ((intensity_worktop_led == 0) && (intensity_plinth_led == 0) && (reedswitch == 0) && (special_days == 1) && (cleaning_mode == 1))
  {
    buzzer_led = 0;
    digitalWrite(power_supply, LOW);
  }
}

//================================================================================================================================================================

void RF_radio()
{
  if ( radio.available())
  {
    radio.read( &dataReceived, sizeof(dataReceived) );

    if (buzzer_led == 0)
    {
      intensity_worktop_led = (dataReceived[1]);
      intensity_plinth_led  = (dataReceived[2]);
    }
    cleaning_mode         = (dataReceived[3]);                           // 0 = cleaningmode ON, so alarm function is OFF
    special_days          = (dataReceived[4]);                           // 0 = special days ON, so if time and date is there, function is active
  }

  if (!radio.available() ==1)
  {
    Serial.println(" radio signal is lost !!!");                          // [b]dit als test, wat ik niet werkend krijg[/b]
  }
}

//================================================================================================================================================================

void door_drawer_timer()
{
  unsigned long ddcurrentMillis = millis();

  if (reedswitch != 0 && run_once_door_drawer_timer == 0)
  {
    timer = ddcurrentMillis + ddinterval;
    run_once_door_drawer_timer = 1;
  }

  if (timer - ddcurrentMillis < 200)
  {
    if (reedswitch != 0)
    {
      buzzer_led = 1;
    }
  }

  if ((cleaning_mode == 1) && (reedswitch == 0 && special_days == 1))
  {
    buzzer_led = 0;
    run_once_door_drawer_timer = 0;
  }
}


//================================================================================================================================================================

void buzzer_led_timer()
{
  unsigned long bbcurrentMillis = millis();
  if (bbcurrentMillis - bbpreviousMillis >= bbinterval)
  {
    bbpreviousMillis = bbcurrentMillis;
    if ((buzzer_led == 1) && (cleaning_mode == 1))
    {
      digitalWrite(power_supply, HIGH);
      if (buzzer_led_timer_state == 0)
      {
        buzzer_led_timer_state = 1;
        analogWrite(plinth_led, 250);
        analogWrite(worktop_led, 250);
        digitalWrite(alarm_buzzer, HIGH);
      }

      else
      {
        buzzer_led_timer_state = 0;
        analogWrite(plinth_led,  0);
        analogWrite(worktop_led, 0);
        digitalWrite(alarm_buzzer, LOW);
      }
    }

    if (buzzer_led == 0)
    {
      digitalWrite(alarm_buzzer, LOW);
    }
  }
}

//================================================================================================================================================================

void days()
{
  if (special_days == 0 && cleaning_mode == 1 && reedswitch == 0 && run_once_special_days == 0)
  {
    digitalWrite(power_supply, HIGH);
    buzzer_led = 1;
    run_once_special_days = 1;
  }

  if (special_days == 1 && cleaning_mode == 1 && reedswitch == 0 && run_once_special_days == 1)
  {
    buzzer_led = 0;
    run_once_special_days = 0;
  }
}


Advertisement

Berichten: 68
Geregistreerd: 21 Apr 2013, 22:48
Woonplaats: Hoorn

Re: NFR24 radio, instructie NOT radio.available

Berichtdoor babbelkwebbel » 20 Dec 2018, 15:34

oeps, stukje van void RF24_radio.

moet zijn

if (!radio.available())
{
Serial.println(" radio signal is lost !!!"); // dit als test, wat ik niet werkend krijg
}

Berichten: 68
Geregistreerd: 21 Apr 2013, 22:48
Woonplaats: Hoorn

Re: NFR24 radio, instructie NOT radio.available

Berichtdoor babbelkwebbel » 20 Dec 2018, 16:11

Nogmaals de cpp door gespit, en kwam deze instructie tegen radio.flush_rx en hier mee wordt de RX buffer leeg gemaakt.

Stukje "probeer" code hier onder en dat werkt als een tierelier.

Misschien dat iemand er wat aan heeft dan wel wat mee kan.

Serial.println("waarde voor flused");
Serial.println(dataReceived[2]);

(dataReceived[2] = 1313);
Serial.println("set waarde 1313");
Serial.println(dataReceived[2]);
delay(2000);

radio.read( &dataReceived, sizeof(dataReceived) );

Serial.println("usb uit");
delay(10000);
radio.flush_rx();
Serial.println("waarde na flused");
Serial.println(dataReceived[2]);
delay(3000);

Tja soms toch even meer door bijten, maar in ieder geval opgelost.

Iedereen alvast de beste wensen voor de kerstdagen en een top 2019 gewenst.
Erik,

Terug naar Shields

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 3 gasten