Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15473
    Christian Eugster
    Participant

    Hi, on my mini ultra pro I run a sketch with radiohead. When I try to send a message by rf95.send, rhdatagram.sendto or RHReliableDatagram.sendtoWait the program freezes. rtc wakes up to defined times but nothing further happens. Any idea?
    Thanks and regards Christian

    /*******************************************************************************
     Mini Ultra Pro Sleep RTC Alarm
     Version: 0.10
     Date: 20-02-2017
     Company: Rocket Scream Electronics
     Author: Lim Phang Moh
     Website: www.rocketscream.com
    
     An example code demonstrating the ultra low sleep current capability of the 
     Mini Ultra Pro while running the on-chip built-in RTC. Example puts the Mini
     Ultra Pro into standby mode (sleep) while RTC is allowed to run in the 
     background. Alarm is set to wake the processor on the 15 s of a minute.
     This results in sleep period of 1 minute. The LED on pin 13 is set to toggle
     on every alarm match state. In sleep state with the built-in LED turn off, 
     current measured is approximately 21.0 uA.
    
     Requirement:
     1. Mini Ultra Pro board powered by a single cell Li-Ion/Pol 3.7V battery.
    
     Important Note:
     1. Upon using the SerialFlash.sleep() function, the only function that the 
     serial flash chip response to is SerialFlash.wakeup(). 
    
     Revision Description
     ======== ===========
     0.10 Initial public release.
    *******************************************************************************/
    
    #include <SerialFlash.h>
    #include <RTCZero.h>
    #include <SPI.h>
    #include <RH_RF95.h>
    #include <RHReliableDatagram.h>
    #include <CayenneLPP.h>
    
    #define DEBUG
    
    #define SERVER_ADDR 1
    #define NODE_ADDR 32
    
    #define SENSOR_SI7021
    
    #define RF_FREQUENCY 868.0
    
    #define Serial SerialUSB
    
    // ***** CONSTANTS *****
    const int radioDio0 = 2;
    const int flashChipSelect = 4;
    const int radioChipSelect = 5;
    
    // Choose correct on-board radio
    RH_RF95 radio(radioChipSelect, radioDio0);
    RHReliableDatagram manager(radio, NODE_ADDR);
    RTCZero rtc;
    #ifdef SENSOR_SI7021
    #include <Adafruit_Si7021.h>
    Adafruit_Si7021 si7021 = Adafruit_Si7021();
    #endif
    
    /* Change these values to set the current initial time */
    const byte seconds = 0;
    const byte minutes = 00;
    const byte hours = 10;
    
    /* Change these values to set the current initial date */
    const byte day = 20;
    const byte month = 2;
    const byte year = 17;
    
    // ***** VARIABLES *****
    #ifdef SENSOR_SI7021
    float tmp;
    float hum;
    #endif
    CayenneLPP lpp(51);
    
    bool match = false;
    unsigned char pinNumber;
    float bat;
    
    float readVoltage()
    {
      int adcReading = analogRead(A5);
      // Discard inaccurate 1st reading
      adcReading = 0;
      // Perform averaging
      for (unsigned char counter = 10; counter > 0; counter--)
      {
        adcReading += analogRead(A5);
      }
      adcReading = adcReading / 10;
      // Convert to volts and return value
      return adcReading * (4.3 / 1023.0);
    }
    
    void alarmMatch()
    {
      match = true;
    #ifdef DEBUG
      Serial.println("Alarm callback");
    #endif
    }
    
    void setup()
    {
    #ifdef DEBUG
      SerialUSB.begin(9600);
      SerialUSB.println("Started");
    
      SerialUSB.println("Initializing board");
    #endif
      // Switch unused pins as input and enabled built-in pullup 
      for (pinNumber = 0; pinNumber < 23; pinNumber++)
      {
        pinMode(pinNumber, INPUT_PULLUP);
      }
      for (pinNumber = 32; pinNumber < 42; pinNumber++)
      {
        pinMode(pinNumber, INPUT_PULLUP);
      }
      pinMode(25, INPUT_PULLUP);
      pinMode(26, INPUT_PULLUP);
    
    #ifdef DEBUG
      SerialUSB.print("Initializing radio: ");
    #endif
      if (radio.init())
      {
    #ifdef DEBUG
        SerialUSB.println("OK");
    #endif
      }
      else
      {
    #ifdef DEBUG
        SerialUSB.println("Failed");
    #endif
      }  
      radio.sleep();
      radio.setFrequency(RF_FREQUENCY);
      radio.setModeTx();
      radio.setTxPower(14, false); // values between 5 to 23
      manager.setThisAddress(NODE_ADDR);
      manager.setHeaderFrom(NODE_ADDR);
     
      SerialFlash.begin(flashChipSelect);
      SerialFlash.sleep();
    
    #ifdef DEBUG
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWrite(LED_BUILTIN, LOW);
    #endif
    
      // ***** IMPORTANT DELAY FOR CODE UPLOAD BEFORE USB PORT DETACH DURING SLEEP *****
      delay(15000);
    
      //***** UNCOMMENT FOR MINI ULTRA PRO VERSION 0V30 & ABOVE IF PINS ARE CONNECTED TO RADIO ****
      pinMode(6, INPUT);
      pinMode(7, INPUT);
      digitalWrite(3, HIGH);
      pinMode(3, OUTPUT);
      digitalWrite(3, HIGH);
      //*****
    
    #ifdef SENSOR_SI7021
    #ifdef DEBUG
      SerialUSB.print("Initializing sensor ");
    #endif
      if (si7021.begin())
      {
    #ifdef DEBUG
        SerialUSB.println("Si7021: OK");
    #endif
      }
      else
      {
    #ifdef DEBUG
        SerialUSB.println("Si7021: Failed");
    #endif
      }
    #endif
    
    #ifdef DEBUG
      SerialUSB.println("Initializing clock");
    #endif
      // RTC initialization
      rtc.begin();
      rtc.setTime(hours, minutes, seconds);
      rtc.setDate(day, month, year);
    
      // RTC alarm setting on every 30 seconds, resulting in 1 minute sleep period
      rtc.setAlarmMinutes(1);
      rtc.enableAlarm(rtc.MATCH_SS);
      rtc.attachInterrupt(alarmMatch);
    #ifdef DEBUG
      SerialUSB.print("Alarm set to ");
      SerialUSB.print(rtc.getAlarmMinutes());
      SerialUSB.println(" minutes");
    
      // digitalWrite(LED_BUILTIN, LOW);
    //  USBDevice.detach();
    #endif
      rtc.standbyMode();
    }
    
    void loop()
    {
      SerialUSB.println(match);
      if (match)
      {
    #ifdef DEBUG
        SerialUSB.println("Alarm fired");
    #endif
        match = false;
    
    // Initialize USB and attach to host (not required if not in use)
    //#ifdef DEBUG
    //    USBDevice.init();
    //    USBDevice.attach();
    //
    //    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    //#else
    //    // Detach USB from host (not required if not in use)
    //    USBDevice.detach();
    //#endif 
    
        lpp.reset();
      
    #ifdef SENSOR_SI7021
        tmp = si7021.readTemperature();
        lpp.addTemperature(1, tmp);
    #ifdef DEBUG
        if (isnan(tmp))
          tmp = 20.0;
        SerialUSB.print("Temperature: ");
        SerialUSB.print(tmp);
        SerialUSB.println("ºC");
    #endif
        hum = si7021.readHumidity();
        lpp.addRelativeHumidity(2, hum);
    #ifdef DEBUG
        if (isnan(hum))
          hum = 50.0;
        SerialUSB.print("Humidity: ");
        SerialUSB.print(hum);
        SerialUSB.println("%");
    #endif
    #endif
    
    #ifdef DEBUG
        SerialUSB.print("Voltage: ");
    #endif
        bat = readVoltage();
        lpp.addVoltage(3, bat);
    #ifdef DEBUG
        SerialUSB.print(bat);
        SerialUSB.println("V");
    
        SerialUSB.print("Send radio packet: ");
        digitalWrite(LED_BUILTIN, HIGH);
    #endif
        radio.setModeTx();
        if (manager.sendtoWait(lpp.getBuffer(), lpp.getSize(), SERVER_ADDR))
        {
    #ifdef DEBUG
          SerialUSB.println("OK");
    #endif
        }
        else
        {
    #ifdef DEBUG
          SerialUSB.println("Failed");
    #endif
        }
        radio.sleep();
        
    #ifdef DEBUG
        SerialUSB.println("Going to sleep");
        digitalWrite(LED_BUILTIN, LOW);
    #endif
        rtc.standbyMode();
      }
    }
    
    #15475
    LIM PHANG MOH
    Keymaster

    You are putting the radio to sleep even before configuring them in setup(), that will hang the code. Only sleep the radio after configuring them.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.