Sending offset to node



  • I guys, i'm trying to figure out how to send from Home Assistant a temperature offset to temperature sensor nodes that i have created.
    Is it possible ? .
    that way i would not have to reprogram my nodes if i need to change an offset .

    I would like to be able to set the offsetTemp variable from remote

    Here my sketch :

    //*********************
    // oled0.91 with aht10 and touch switch to enable display for x second
    // set to 30 minutes,screen 10 sec then sleep
    // Pro mini lo power 8MHz BOD dis ext CLK
    //*********************
    
    // Enable and select radio type attached
    #define MY_RADIO_RF24                            // A 2.4Ghz transmitter and receiver, often used with MySensors.
    // Are you using this sensor on battery power?
    #define BATTERY_POWERED                        // Just remove the two slashes at the beginning of this line if your node is battery powered. It will then go into deep sleep as much as possible. While it's sleeping it can't work as a repeater!
    //#define MY_DEBUG    // Enables debug messages in the serial log
    
    #include <SPI.h>
    #include <MySensors.h>                            // The MySensors library. Hurray!
    #include <Wire.h>
    #include <Vcc.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <Fonts/FreeSerif9pt7b.h>
    #include <Arduino.h>
    #include <Adafruit_AHT10.h>
    
    #define TEMP_CHILD_ID 0        // for MySensors. Within this node each sensortype should have its own ID number.
    #define HUM_CHILD_ID 1        // for MySensors. Within this node each sensortype should have its own ID number.
    #define BAUDRATE 9600                                      // Device to MH-Z19 Serial baudrate (should not be changed)
    #define buttonPin 3     // the number of the pushbutton pin
    #define CHILD_ID_BATTERY    2
    #define CHILD_ID_BATTERY_V  3
    #define BATTERY_FULL        3.14      // 2xAA usually give 3.143V when full
    #define BATTERY_ZERO        2.3      // 2.34V limit for 328p at 8MHz. 1.9V, limit for nrf24l01 without step-up. 2.8V limit for Atmega328 with default BOD settings.
    #define ONE_DAY_SLEEP_TIME  600000  // will send data every 30 minutes(1800000) at least, or everytime button is pressed 10 min =600000
    int8_t wakeupReason = MY_WAKE_UP_BY_TIMER;
    MyMessage voltage_msg(CHILD_ID_BATTERY_V, V_VOLTAGE);
    MyMessage temperatureMsg(TEMP_CHILD_ID, V_TEMP);
    MyMessage humidityMsg(HUM_CHILD_ID, V_HUM);
    
    Vcc vcc(1.0);
    float oldvoltage = 0;
    float offsetTemp = 0.5; // offset orange sensor 0.5
    
    unsigned long oled_previousMillis = 0;        // will store last time OLED was updated
    unsigned long currentMillis = millis();
    const long oled_interval = 10000;           // time display on
    
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 32 // OLED display height, in pixels
    
    // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
    
    #define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
    #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
    
    
    Adafruit_AHT10 aht;
    char outstr[6];
    
      
    //*********************************************************
    void setup() {
    
    //  SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
        if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
        }
    
      
      // Clear the buffer
        display.clearDisplay();
    
    
        if (! aht.begin()) {
          Serial.println("Could not find AHT10? Check wiring");
          while (1) delay(10);
         }
    
      display.setFont(&FreeSerif9pt7b);
      Serial.println("AHT10 found");
      display.ssd1306_command(SSD1306_SETCONTRAST);
      display.ssd1306_command(64); // Where c is a value from 0 to 255 (sets contrast e.g. brightness)
    
      pinMode(buttonPin, INPUT); 
    
    }
    //*****************************************************
    void presentation() {
      sendSketchInfo("Temp sensorAHT10", "2.0 ");
      wait(250);
      present(TEMP_CHILD_ID, S_TEMP);
      wait(250);
      present(CHILD_ID_BATTERY_V, S_MULTIMETER);//present(CHILD_ID_BATTERY, S_MULTIMETER);  S_CUSTOM  
      wait(250);
      present(CHILD_ID_BATTERY, S_CUSTOM);//present(CHILD_ID_BATTERY, S_MULTIMETER);  S_CUSTOM  
      wait(250);
      present(HUM_CHILD_ID, S_HUM);
      wait(250);
    }
    //******************************************************
    void loop() {
      
      unsigned long currentMillis = millis();
      static int buttonState = 0;         // variable for reading the pushbutton status
      
      buttonState = digitalRead(buttonPin);
      if (buttonState == HIGH) {
         oled_previousMillis = currentMillis;   // save time you enable OLED
        } 
      if (currentMillis - oled_previousMillis <= oled_interval) { //keep oled active for oled_interval
            
          display.clearDisplay();
          
        // display.drawRect(0,0,128,32,WHITE);
           display.drawRoundRect(0, 0, 127, 32, 4, WHITE);
           display.drawLine(68, 0, 68, 32, WHITE); 
           sensors_event_t humidity, temp;
           aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
           temp.temperature = temp.temperature + offsetTemp ;
    
        // ScreenPrint("Temp:",9,12,1,false);
           dtostrf(temp.temperature,4, 1, outstr);
           ScreenPrint(outstr,6,21,1,false);
           display.drawCircle(43, 11, 2, SSD1306_WHITE);
           ScreenPrint("C",48,21,1,false); 
            
       //  ScreenPrint("Humi:",9,31,1,false);
           dtostrf(humidity.relative_humidity,4, 1, outstr);
           ScreenPrint(outstr,73,21,1,false);
           ScreenPrint("%",105,21,1,false);
    
          
          display.display();
          wait(5000); 
          }
        else { //if oled time done
            
          sensors_event_t humidity, temp;
          aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
    
          temp.temperature = temp.temperature + offsetTemp ;
    
          send(temperatureMsg.set(temp.temperature, 1));
          send(humidityMsg.set(humidity.relative_humidity, 1));
          
          float voltage = vcc.Read_Volts();
          float percentage = vcc.Read_Perc(BATTERY_ZERO, BATTERY_FULL, true);
          int batteryPcnt = static_cast<int>(percentage);
    
           if (oldvoltage != voltage) { // Only send battery information if voltage has changed, to conserve battery.
             send(voltage_msg.set(voltage, 3)); // Set how many decimals are used (3 in our case)
             sendBatteryLevel(batteryPcnt);
             oldvoltage = voltage;               
             }
          blackScreen();
       // sleep to conserve energy
    
         
        wakeupReason = sleep(digitalPinToInterrupt(buttonPin), RISING, ONE_DAY_SLEEP_TIME);
        //wakeupReason = sleep(digitalPinToInterrupt(INT_PIN), CHANGE, sleepTime);
        if (wakeupReason == digitalPinToInterrupt(buttonPin)){
        display.ssd1306_command(SSD1306_DISPLAYON);
        }
        }
    }
    
     
    void blackScreen(void){
        display.ssd1306_command(SSD1306_DISPLAYOFF);
       // display.clearDisplay();
      //  display.display();
      }
    
    
    void ScreenPrint(String text, int x, int y, int size, bool d){
    
      display.setTextSize(size);
      display.setTextColor(WHITE);
      display.setCursor(x,y);
      display.println(text); 
      if(d){
        display.display(); 
        }
      }
    
    

Log in to reply
 

Suggested Topics

  • 1
  • 3
  • 3
  • 3
  • 6
  • 6

67
Online

11.5k
Users

11.1k
Topics

112.7k
Posts