Interval/sleep sensor settings


  • Mod

    Let's assume I have a temperature sensor and let's assume I want to set it to sleep for 30 minutes before reporting any change (if any) makes me thing the following:

    1. Is it possible to change this interval through the controller/gateway currently or can I and do I need to add this myself?
    2. When I do this the sensor is asleep and can not receive any request from the controller (if I am correct). Will the request be queued or do I have to create a queue on the controller?
    3. When I have to create a queue on the controller how is it reported that the request did not arrive?
    4. And, lastly, is it possible to wake the sensor up with a special request (I assume not)?


  • here is my solution for dht-22 battery driven sensor:

      #include <Sleep_n0m1.h>
      #include <SPI.h>
      #include <EEPROM.h>  
      #include <RF24.h>
      #include <Sensor.h>  
      #include <DHT.h>  
      
      
      // Set RADIO_ID to something unique in your sensor network (1-254)
      // or set to AUTO if you want gw to assign a RADIO_ID for you.
      #define RADIO_ID AUTO
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      #define HUMIDITY_SENSOR_DIGITAL_PIN 3
      // Send the Battery Percentage to the Gateway
      
      #define VBatMax 4.0            // The voltage of New Batteries
      #define VBatDropout  2.0    // The battery dropout voltage
      
      unsigned long SLEEP_TIME = 30; // Sleep time between reads (in seconds)
      float SENSOR_TOLERANCE = 0.1; // Tempereture or humidity sensetive change +- 0.1 will not go to data transmit
      unsigned long SEND_FREQ = 1; // If the temperature doesn't change for this period, force data transition of battery level to reset TTL on vera gate. 1 = means send each time the sensor wake-up
      unsigned long SETTINGS_REQ_FREQ = 1; // How often node reqsts a new settings 1 = means req. each time the sensor wake-up. NOTE: Settings reqsts each time the sensor resets.
      int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
      
      
      struct port_param_t{String name; String value;};
      #define MAX_PARAMS 20 // ������� ���������� ����������� �� ����� �������
      port_param_t params[MAX_PARAMS]; // � ���� ������ ����� ��������� ���� ��������� ���������
      byte parsedParams=0; // ������� ���������� ��� ������� ���������
      
      Sensor gw(9, 10);
      DHT dht;
      Sleep sleep;
      
      float lastTemp;
      float lastHum;
      float lastSend = 0;
      float lastReq = 0;
      float lastBattLevel;
      boolean metric = true; 
      
      void setup()  
      { 
        
        delay(2000);
        // use the 1.1 V internal reference
        analogReference(INTERNAL);
      
        Serial.begin(BAUD_RATE);  // Used to type in characters
        gw.begin(RADIO_ID);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        gw.sendSketchInfo("Humidity", "1.2");
        // Register all sensors to gw (they will be created as child devices)
        gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM);
        gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);
        
        metric = gw.isMetricSystem();
        
        veraCfgread();
        
      }
      
      void veraCfgread()
      {
      	char* defCfg="1,1,30,1"; 
      	char* Cfg = gw.getStatus(RADIO_ID, V_VAR1); // getting cfg
      	Serial.println("parsed:");
      	parseParams(Cfg);
      	if (parsedParams==0)
      		gw.sendVariable(RADIO_ID, V_VAR1, defCfg);
      	else
      	{
      		SENSOR_TOLERANCE = float(strtoint(params[0].value))/10;
      		SEND_FREQ = strtoint(params[1].value);
      		SLEEP_TIME = strtoint(params[2].value);
      		SETTINGS_REQ_FREQ = strtoint(params[3].value);
      	}
      	Serial.print(SENSOR_TOLERANCE);
      	Serial.print(",");
      	Serial.print(SEND_FREQ);
      	Serial.print(",");
      	Serial.print(SLEEP_TIME);
      	Serial.print(",");
      	Serial.print(SETTINGS_REQ_FREQ);
      	Serial.print(".");
      }
      
      // Read the Battery
      long readVcc() {
          long result;
          // Read 1.1V reference against AVcc
          ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
          delay(2); // Wait for Vref to settle
          ADCSRA |= _BV(ADSC); // Convert
          while (bit_is_set(ADCSRA,ADSC));
          result = ADCL;
          result |= ADCH<<8;
          result = 1125300L / result; // Back-calculate AVcc in mV
          return result;
      }
      
      
      int strtoint(String str) // ��������� ��������������� ������ � �����
      {
        int tempInt;
        char rez[str.length()+1];
        str.toCharArray(rez, sizeof(rez));
        tempInt = atoi(rez);
        return tempInt;
      }
      
      void parseParams(char* inputString)
      {
      	parsedParams=0; // ���� ������ �� ���������
      	String message = inputString;
      	int commaPosition;  // the position of the next comma in the string
      	do
      	{
            commaPosition = message.indexOf(',');
            if(commaPosition != -1)
            {
                params[parsedParams].value = message.substring(0,commaPosition);
      		  parsedParams++;
                message = message.substring(commaPosition+1, message.length());
            }
            else
            {  // here after the last comma is found
               if(message.length() > 0)
                 params[parsedParams].value = message;  // if there is text after the last comma
            }
         }
         while(commaPosition >=0);
      }
      
      void loop()      
      {     
        lastSend--;
        lastReq--;
        // -1------------------------------------------
        delay(dht.getMinimumSamplingPeriod());
        float temperature = dht.getTemperature();
        if (isnan(temperature)) 
        {
            Serial.println("Failed reading temperature from DHT");
        } 
        else if (temperature != lastTemp || lastSend == 0)
      		{
      			Serial.print("TR: ");
      			Serial.println(temperature);
      
      			float delta = lastTemp - temperature;
      			delta = abs (delta);
      			if (delta > SENSOR_TOLERANCE || lastSend == 0)
      			{
      				lastTemp = temperature;
      				if (!metric) 
      					{
      						temperature = dht.toFahrenheit(temperature);
      					}
      				gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
      				Serial.print("T: ");
      				Serial.println(temperature);
      			}
      		}
        // -2------------------------------------------
      
        float humidity = dht.getHumidity();
        if (isnan(humidity)) 
        {
            Serial.println("Failed reading humidity from DHT");
        } 
        else if (humidity != lastHum || lastSend == 0) 
      	   {
      		   Serial.print("HR: ");
      		   Serial.println(humidity);
      		   float delta = lastHum - humidity;
      		   delta = abs (delta);
      		   if (delta > SENSOR_TOLERANCE || lastSend == 0)
      		   {
      			   lastHum = humidity;
        Serial.println(lastSend);
        if (lastSend <= 0)
      	  lastSend = SEND_FREQ; 
        Serial.print("LastSndAfter: ");
        Serial.println(lastSend);
      
        if (lastReq <= 0)
        {
      	  veraCfgread();
      	  lastReq = SETTINGS_REQ_FREQ;
        }
        // -3------------------------------------------
        long curVcc;// = readVcc();
        float pct;  // = (curVcc-(VBatDropout*1000)) /(((VBatMax-VBatDropout) *1000))*100;
        
        
        int sensorValue = analogRead(BATTERY_SENSE_PIN);
        float batteryV  = sensorValue * 0.003363075;
        int batteryPcnt = sensorValue / 10;
      
        curVcc = batteryV*1000;
        pct = batteryPcnt;
      
        if (pct != lastBattLevel)
        {
      	  lastBattLevel = pct;
      	  gw.sendBatteryLevel(pct);
      	  
      	  /*Serial.print("Batt%: ");
            Serial.println(pct);
      	  Serial.print("Vcc: ");
            Serial.println(batteryV);*/
      	  
      	  gw.sendVariable(RADIO_ID, V_VAR2, curVcc);
        }
        // -------------------------------------------
        // Power down the radio.  Note that the radio will get powered back up
        // on the next write() call.
        delay(1000); //delay to allow serial to fully print before sleep
        gw.powerDown();
        sleep.pwrDownMode(); //set sleep mode
        sleep.sleepDelay(SLEEP_TIME * 1000); //sleep for: sleepTime 
      }
      			   gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
      			   Serial.print("H: ");
      			   Serial.println(humidity);
      		   }
      	   }
        // -------------------------------------------
        Serial.print("LastSndBefore: ");

  • Mod

    Wow thank you but... Can you please correct the formatting 🙂


  • Admin

    Thanks for the example @mitekg

    Took the liberty to fix the formatting in your post.

    Start a line with 4 spaces or tab to format as code.



  • @hek, thx4fix!)
    @marceltrapman, if u have any questions, u r welcome)


  • Hero Member

    @mitekg
    Have you converted you sketch to v 1.4?


Log in to reply
 

Suggested Topics

  • 87
  • 6
  • 2
  • 1
  • 8
  • 7

10
Online

11.4k
Users

11.1k
Topics

112.7k
Posts