Getting numbers from Vera to a sensor


  • Contest Winner

    Has anyone tried to send a signal (numbers or binary) back and forth to Vera? I'm looking for a little guidance on an approach.

    I've got a good handle on a sensor that will monitor the temp and humidity. I have integrated LCD's into the sensors and can make a device that reports locally via display ass well as through Vera. I want to send communications back to vera to allow the user (person pushing the button) to create an event in Vera.

    Example:

    a) Pushng a button on the Temp/Humidity box will allow user to lower AC temporarily.
    b) Display on my LCD data from Vera like outdoor temperature

    am thinking that the pushbutton is the obvious choice for A, and integrating the DHT22 and LCD into that sketch is certainly do-able, I was just looking for a shortcut on this.

    Has anyone already tried these kinds of things?


  • Mod

    MySensors is well prepared for your task.
    May be this will be good for you to start with http://code.mios.com/trac/mios_arduino-sensor/wiki/TipsAndTricks


  • Contest Winner

    @axillent

    Thanks for the link. I am having trouble with this.

    I was able to create the Variable1 field in the corresponding Vera Device with the

    gw.getStatus(CHILD_ID_HUM, V_VAR1);
    

    line in the setup part of the sketch, and I populated it with a number, so all is well there

    My problem now is to make the call back to vera in the loop and return what's there. I tried:

    OutdoorHumidity = gw.getstatus(CHILD_ID_HUM, V_VAR1)
    

    but that isn't working, with an error on compiling.

    Cannot find a single example sketch here so any assistance is appreciated.


  • Contest Winner

    OK, I found Bruce's example...

    So I do this in the loop:

    gw.getStatus(CHILD_ID_HUM, V_VAR1);
    HumStatus(gw.getMessage());
    

    And then I have a loop called HumStatus:

    void HumStatus(message_s message){
    if (message.header.type==V_VAR1) { 
    OutdoorHumidity = atoi(message.data);
       }
    }
    

    Voila!

    Thanks again Axilent for the pointer!


  • Contest Winner

    @axillent

    Again, thanks for the help.

    I attached some photos of the display if anyone is interested. The sketch has not been simplified test, but it works so if you are at all interested, you have a good place to start:

    // Humidity and Temperature Sensor with LCD Display
    // Retrieves Temperature, Humidity and today's Hi/Low from Vera and displays it on an LCD alongside with Room Temperature and Humidity.
    //Updates Vera with Temp and Humidity just like any other sensor
    #include <Sleep_n0m1.h>
    #include <SPI.h>
    #include <EEPROM.h>  
    #include <RF24.h>
    #include <Sensor.h>  
    #include <DHT.h> 
    #include <Wire.h> 
    #include <LiquidCrystal_I2C.h>
    //
    LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x20 for a 16 chars and 2 line display
    //
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 4
    unsigned long SLEEP_TIME = 5; // Sleep time between reads (in seconds)
    //
    Sensor gw;
    DHT dht;
    Sleep sleep;
    float lastTemp;
    float lastHum;
    float temperature;
    float humidity;
    float temp;
    boolean metric = false;
    int OutdoorHumidity;
    int OutdoorTemp;
    int TodayLow;
    int TodayHigh;
    int counter = 60;
    //
    void setup()  
    { 
      Serial.begin(9600);
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      gw.sendSketchInfo("Humidity", "1.0");
      gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM); 
      gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);
      gw.getStatus(CHILD_ID_HUM, V_VAR1);
      gw.getStatus(CHILD_ID_TEMP,V_VAR1);//current exterior temperature
      gw.getStatus(CHILD_ID_TEMP,V_VAR2);//today's high temperature
      gw.getStatus(CHILD_ID_TEMP,V_VAR3);//today's low temperature
      //
      lcd.init(); // initialize the lcd 
      // Print a message to the LCD.
      lcd.backlight();
      lcd.setCursor(0,0);
      lcd.print("    Welcome!");
      lcd.setCursor(0,1);
      lcd.print("      Home");
      delay(2000);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("    Wireless");
      lcd.setCursor(0,1);
      lcd.print("  WeatherWatch");
      delay(2000);
    }
    //
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
      if (counter == 60){
        gw.getStatus(CHILD_ID_HUM, V_VAR1);
        HumStatus(gw.getMessage());
        gw.getStatus(CHILD_ID_TEMP, V_VAR1);
        TempStatus(gw.getMessage());
        gw.getStatus(CHILD_ID_TEMP, V_VAR2);
        LowStatus(gw.getMessage());
        gw.getStatus(CHILD_ID_TEMP, V_VAR3);
        HighStatus(gw.getMessage());
        counter = 0;
      }
      temperature = dht.getTemperature();
      temp = dht.toFahrenheit(temperature);
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
         if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
          Serial.print("T: ");
          Serial.println(temperature);
      }
      humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
           lastHum = humidity;
          gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
          Serial.print("H: ");
          Serial.println(humidity);
      }
      // 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
      updateOutsideTemp();
      updateOutsideHumid();
      delay(3000);
      updateLow();
      updateHigh();
      delay(3000);
      updateTemp();
      updateHumid();
      counter = (counter + 1);
    }
    void updateTemp(){
      lcd.setCursor(0,0);
      lcd.print("Temperature: ");
      lcd.print(round(temp));
      lcd.print("F ");
    }
    void updateLow(){
      lcd.setCursor(0,0);
      lcd.print("Today's Low: ");
      lcd.print(round(TodayLow));
      lcd.print("F ");
    }
    void updateHigh(){
      lcd.setCursor(0,1);
      lcd.print("       High: ");
      lcd.print(round(TodayHigh));
      lcd.print("F ");
    }
    void updateHumid(){
      lcd.setCursor(0,1);
      lcd.print("Humidity:    ");
      lcd.print(round(humidity));
      lcd.print("% ");
    }
    void updateOutsideTemp(){
      lcd.setCursor(0,0);
      lcd.print("Outside Temp:");
      lcd.print(OutdoorTemp);
      lcd.print("F ");
    }
    void updateOutsideHumid(){
      //getStatus(CHILD_ID_HUM, V_VAR1);
      lcd.setCursor(0,1);
      lcd.print("Humidity:    ");
      lcd.print(OutdoorHumidity);
      lcd.print("% ");
    }
    void HumStatus(message_s message){
      if (message.header.type==V_VAR1) {
         OutdoorHumidity = atoi(message.data);
       }
    }
    void TempStatus(message_s message){
      if (message.header.type==V_VAR1) {
         OutdoorTemp = atoi(message.data);
       }
    }
    void LowStatus(message_s message){
      if (message.header.type==V_VAR2) {
         TodayLow = atoi(message.data);
       }
    }
    void HighStatus(message_s message){
      if (message.header.type==V_VAR3) {
         TodayHigh = atoi(message.data);
       }
    }
    

    Have fun with it and let me know if you have any ideas to improve or take it further!

    photo.JPG
    photo2.JPG
    photo3.JPG


  • Contest Winner

    OK, one last question (hopefully) on this post:

    I'm trying to get the Weather Status off of weather underground, and get that sent to the sensor and print on the LCD. I have it successfully populating Variable4 on the Thermometer Device.

    Defining the variable like this:

    char TodayWeather; //*****
    

    I cannot seem to get it formatted right. I am using like this:

    void WeatherStatus(message_s message){
      if (message.header.type==V_VAR4) {
         TodayWeather = atoi(message.data);
       }
    }
    

    And Printing like this:

    void updateWeather(){
      lcd.setCursor(0,0);
      lcd.print("Today's Weather ");
      lcd.setCursor(0,1);
      lcd.print("                ");//clear the line
      delay(25);
      lcd.setCursor(0,1);
      lcd.print(TodayWeather);
    }
    

    I'm confident that this is some string variable problem that I simply cannot see, or it has to do with the length of the string?
    Any advice out there?

    Screen Shot 2014-04-30 at 11.35.31 PM.png
    Screen Shot 2014-04-30 at 11.37.19 PM.png


  • Hero Member

    Hi mate - looks good and im glad you brought this topic up...its something i was about to get to myself for my pool controller.
    Anyway, with respect to your "Todays Weather" var... i wonder if its similar to the problem i had here:
    http://forum.micasaverde.com/index.php/topic,24588.msg170887.html#msg170887

    • i think the message.data is not a string....
      What do you see if you do Serial.print (TodayWeather); ?

    HTH


  • Contest Winner

    @gregl

    Thanks for reminding me... It is the two lines between 'weather' and 'T: 75.20' Photo attached, it is returning two characters that I cannot cut and paste here.

    Some kind of unsigned character array? So I changed the variable declaration to :

    unsigned int TodayWeather;
    

    and now the string returns as to lines of zero, instead. Looks like progress... (2nd screen shot)

    Screen Shot 2014-05-01 at 12.06.55 AM.png

    Screen Shot 2014-05-01 at 12.16.24 AM.png


  • Contest Winner

    I guess I have to change the title of this thread to "Getting Text from Vera to a Sensor."

    I wonder if there is a way to write the string in vera in a format that is better suited for the message?


  • Hero Member

    you could use some luup code (from the weather plugin) to set the variable to a int
    http://code.mios.com/trac/mios_weather
    and http://www.wunderground.com/weather/api/d/docs?d=resources/phrase-glossary#forecast_description_phrases
    then in the sketch convert the var to a known string...

    ...but im sure there is a way to send the string without needing to do this.. Hek ?


  • Contest Winner

    @gregl

    So, I figured it out. it turns out I'm a <redacted>.

    TodayWeather = atoi(message.data);
    

    Will ALWAYS return an integer, because that is what it is supposed to do...

    void WeatherStatus(message_s message){
      if (message.header.type==V_VAR4) {
         String TodayWeather = String(message.data);
         Serial.println("weather: ");
         Serial.println(TodayWeather);
         delay(1000);
       }
    }
    

    brings in the string.

    can't believe I missed that one.

    my only problem is that the string has a little something extra, now and wouldn't print to the LCD, which I solved by appending in Vera the string I grab with an "&" and clipping it in Arduino:

    void WeatherStatus(message_s message){
      if (message.header.type==V_VAR4) {
         String Weather = String(message.data);
         int locator = Weather.indexOf("&");
         Weather = Weather.substring(0, locator);
         TodayWeather= Weather;
       }
    }
    

    and Lua

    local weather = luup.variable_get("urn:upnp-micasaverde-com:serviceId:Weather1","Condition", 59)
    weather = (weather.." &")
    luup.variable_set("urn:upnp-org:serviceId:VContainer1","Variable4",weather,58)
    

    And magically we have text and numbers!

    Now, I knock out the interrupt and get a 1 back to Vera and I am golden.

    @gregl

    thanks for the help.

    I'd like to post the (semi-rough) code but it is a nuisance having to indent every line. Is there a quick way? Hek?

    Screen Shot 2014-05-01 at 12.29.15 PM.png



  • Ohh good god, this is great. I was looking to delve in to something like this soon, for the same use: displaying weather.

    Thanks much for info. Where did you put the display(or plan to) if I may ask? I have been considering where to place them so that the light is not a nuisance....


  • Contest Winner

    @NotYetRated

    So funny that you mentioned. In the sketch you will see my note that I have to figure out how to turn off that LED. I think it is very easy, I just need to find the command. I was hoping it was harder to move text and digits back and forth. I am tenacious when it comes to those things, so I will find out how. But, if you find out first, please let me know.

    The display/device will go into the guest bedroom, It will allow a guest to bump the A/C if they need more air conditioning. I am now in the process of adding in a flag to signal to Vera to take an action. I want a push of a button (interrupt) to signal an event. I feel like I got a lot done here already and am happy if it means you got there quicker. Please use the (albeit chunky) sketch. I will update it when it is done including the communication back to Vera.

    Cheers.

    does anyone know how to post code without having to indent every line by 4 spaces... troublesome.

    in any case I updated the MCV site with the sketch theta captures the text.

    Moved to next post


  • Contest Winner

    @NotYetRated

    Here is the sketch with the ability to toggle the LCD's backlight. The command was easy, It took me a while to find it. When I did, it occurred to me how obvious it should have been.

    I added a variable to the Hygrometer device (Variable2) and the code to get the LED to switch. It will be on if there is a 1 in the field and off if there isn't. The sketch calls back to Vera to update temperature, humidity, etc. every 60 turns (you can change) so it will switch when it makes that call.

    I set up PLEG to toggle Variable2 to "0" at 23:00 and to "1" 30mins after sunrise.

    I hope that helps.

    LCDSensor.ino


  • Contest Winner

    @NotYetRated

    OKAY,

    Interrupt is in, the button push on interrupt 1 will toggle 'on' a variable on the hygrometer (set it to "1").

    You can use that to create a PLEG action and use that action to run a scene and the toggle it back to zero, if you want. You could do a lot with a couple pushbuttons next to the bed. Plus you can also use the regular available pins as interrupts with a little more effort. You could basically create a multi-button scene controller!

    The thing runs clunky because of all of the delays (the weather has to cycle through but is easily fixable). I am going to get this cleaned up to get the feedback response as quickly as possible.

    Sketch attached

    going to a baseball game to get my head out of this... Its in a good place to do that.

    Can you post Video here?

    LCDSensor_ino.ino



  • Wow - I was just logging in to ask about the details on if anyone had tried sending data from the gateway to sensors and here we are. This is great! I was looking to light up a LED when sensors tripped, but now I must send text to an LCD for a "real time feed" of what my sensors "see" or "know". I may need one of these by the arm chair in the den now that it can tell me useful stuff!

    As to mounting and placement (not sure if NOTYETRATED was asking more along the lines of this context), I am mounting both Unos and Pro minis in standard light switch boxes (plastic electrical junction box). They are cheap and easy to "blend in" with a faceplate that matches everything else in my house that was built in the 1960's. I have several in my house that are duplex boxes where we are only using one switch, so I have the extra space for mounting a sensor and battery pack. An uno and batteries are pretty tight - I don't have any of these mounted "in production" yet. A mini pro mounted via "3M VHB Tape" (Very High Bond - stickiest tape I think ever made) works well.

    Now to see about cutting a blank switchplate to mount an LCD, add a button and voila'! - weather by the back door to the garage!


  • Admin

    @BulldogLowell said:

    Can you post Video here?

    There is a file upload limit of 2 megabyte.
    Would prefer a youtube link (or similar).

    I think I've seen a youtube (embed) plugin to this forum somewhere. Will install it together with next upgrade.


  • Contest Winner

    @hek

    thanks, I'll get the next one on Video... MIND BLOWING!!!

    Well... kinda cool at least.


  • Contest Winner

    @Sleepyhat

    Great ideas. Post some photos when you are installing.

    I posted my final code on this thread.

    I removed all of the noisy delays and other clutter to get it to respond to the interrupt as quickly as I can. I can still improve that:) but the new code works really well.

    Check it our and have some fun.

    This one was "Man Vs. Arduino"

    Man won.



  • is there a way to display these things (temp sensor readings) on to a web page?


 

259
Online

8.7k
Users

9.5k
Topics

99.8k
Posts