Sending Variables to a arduino switch



  • I want to send variable 1 to a arduino switch, that is lightened with a LED. Variable 1 is the brightness of the led.
    I read several threads to that topic:
    http://forum.mysensors.org/topic/957/red-green-matrix-information-panel-with-real-time-clock
    http://code.mios.com/trac/mios_arduino-sensor/wiki/TipsAndTricks
    http://forum.mysensors.org/topic/80/getting-numbers-from-vera-to-a-sensor/6
    but they have syntax mysensors library doesn't recognise (mabe the changed with new versions)
    gw.getStatus(CHILD_ID, V_VAR1); for example
    In "TipsAndTricks" Case # 2 I'm not sure what radioId="4,255" is. Is that what is in vera altid?

    It would be great if someone could give a simple script to send a variable to a node and the code I should use in vera scene.
    TIA Patrick


  • Mod

    Which version of the MySensors library are you using?



  • the one on codebender ... and I also tried 1.5
    and vera UI5



  • I tried to write a node that sets the brightness of an attached LED with the value of V_VAR1
    I did this based on the Power Meter & Water Meter Sketch

    unfortunately it doesn't work 😞 edit: it works now!
    here is the code:
    https://codebender.cc/sketch:221589


  • Mod

    What is the output on the serial port when you run it?
    What type of led are you using?

    Most leds cannot be controlled by voltage level (analogWrite), they need to be controlled by pwm.



  • The LED is not the problem. AnalogWrite works perfect.
    Serial out is:
    Requesting VAR1
    LED Level 255

    connecting at 9600
     ,pt=0,l=23,sg=0,st=ok:LED brightness via VAR1
     send: 5-5-13-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:0.2 
    send: 5-5-13-0 s=0,c=0,t=3,pt=0,l=0,sg=0,st=ok: 
    Requesting VAR1 send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    read: 13-13-255 s=255,c=3,t=7,pt=0,l=0,sg=0: 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    read: 13-13-255 s=255,c=3,t=7,pt=0,l=0,sg=0: 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255 
    Requesting VAR1send: 5-5-13-0 s=0,c=2,t=24,pt=0,l=0,sg=0,st=ok: 
    LED Level: 255
    

    IncomingMessage routine is never executed


  • Hardware Contributor

    Im having this issue as well with domoticz... sometimes i get a reply but its like that ack doesnt work somewhere. Im trying to figure this out but no luck so far. Was just about to post in Domoticz forum yesterday but didnt...

    Im trying to recieve a VAR1 for my rain sensor... sometimes it takes 10 tries and sometimes it never returns. I have the sensor just beside the gateway with no luck so my feeling is that there is an error in the code somewhere (Domoticz or Mysensors, dont know).

    So far I have not debugge my gateway (no logs) but in MYSController i get the request in but never see any reply out with the VAR1 value.


  • Contest Winner

    @patrick-schaerer the problem in your code snipped is the delay(1000).

    This is a blocking call which blocks also your radio module from communicating. You have to use gw.wait(1000); i.s.o. delay. Now your plugin will receive data.



  • @BartE said:

    @patrick-schaerer the problem in your code snipped is the delay(1000).

    This is a blocking call which blocks also your radio module from communicating. You have to use gw.wait(1000); i.s.o. delay. Now your plugin will receive data.

    ok Ill try that


  • Hardware Contributor

    I don't have any delay in my code. Still same problem with var1 and domoticz.



  • delay is not the problem ... i changed the code to gw.wait(1000)
    Serial output is the same


  • Hardware Contributor

    I wrote a code that only fetch time and the V_VAR1 - works every time.
    MySensors 2.0b, Ethernet GW, Domoticz latest version:

    #NOTE THAT CODE IS FOR MySensors > 1.6.0b version
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    #define MY_NODE_ID 7
    
    #include <SPI.h>
    #include <MySensor.h>  
    #include <Time.h>  
    
    boolean timeReceived = false;
    unsigned long lastUpdate=0, lastRequest=0;
    #define CHILD_ID 1   
    
    boolean pcReceived = false; 
    unsigned long lastSend =0; 
    
    void setup()  
    {  
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Test to get V_VAR1", "1.0");
    }
    
    void loop()     
    {     
      unsigned long now = millis();
    
      // If no time has been received yet, request it every 5 second from controller
      // When time has been received, request update every hour
      if ((!timeReceived && (now-lastRequest) > (5UL*1000UL))
        || (timeReceived && (now-lastRequest) > (60UL*1000UL*60UL))) {
        requestTime();  
        lastRequest = now;
      }
    
        //Request V_VAR1 every 5 sec if not recieved.
        if (!pcReceived && (now - lastSend > 5000)) {      
          request(CHILD_ID, V_VAR1);
          lastSend=now;
          return;
        }
        if (!pcReceived) {
          return;
        }
    }
    
    void receive(const MyMessage &message) {
      if (message.type==V_VAR1) {
      Serial.print("Received last pulse count from gw:");
      Serial.println(message.getULong());
      pcReceived = true;
      }
    }
    
    // This is called when a new time value was received
    void receiveTime(unsigned long time) {
      // Ok, set incoming time 
      setTime(time);
      Serial.print("Got time, hour: ");
      Serial.println(hour());
      timeReceived = true;
    }
    

    Its not different from my Rain sensor code... where i have the same problem as above... so there must be something else blocking recieve function in that code or radiotraffic... Its nothing wrong with Domoticz and/or Gateway what i can see... its something wrong with the code in the sensor itself. To be continued...


  • Hardware Contributor

    What is similar in our sketches that does not work is that we use pin 3. I have seen some post with relays that it worked if you move to pin 4. Don't know why but I will try this tonight. Worth a shot...


  • Contest Winner

    @patrick-schaerer and @sundberg84

    I've compiled your code and uploaded in my Arduino to debug your issue. What i noticed is that the sketch worked on my side, two remarks on the code snipped:

    • why switching back to 9600 baud and not just setting your terminal to 115200 baud.
    • line 83 contains an error i think the + should be removed so it becomes this line:
    LED_Level = gwLEDLevel;
    

    But this will not solve you original problem. What i also noticed is when you requesting data from a none exiting node you do not get any data but the following error in your Vera controller log file:

    Open a terminal like putty -> new connection to your Vera's IP address login with username root and the WIFI password and give this command

    tail -f /var/log/cmh/LuaUPnP.log
    

    The error line you might see is this one (the second line is yellow marked)

    50      01/17/16 15:07:28.915   luup_log:64: Arduino: Log: read: 50-50-0 s=255,c=3,t=12,pt=0,l=3,sg=0:0.2 <0x6011>
    02      01/17/16 15:07:28.918   luup_log:64: Arduino: Incoming internal command '50;255;3;0;12;0.2' discarded for child: nil <0x6011>
    

    So it might help to:

    • start MySensors inclusion mode
    • give a reset on your Arduino
    • wait to see MySensors does find new nodes
    • press stop inclusion
    • wait until Vera is ready again
    • press reload
    • wait until Vera is ready again
    • reset your Arduino again and make sure the MySensor node shows the corrent version information

  • Hardware Contributor

    Tried my hypothesis above but same error. What made some different now was to add a second cap of 10uF to the radio... still nog good, but at least after some tries I now receive the V_VAR1.



  • @sundberg84
    Can your code be used on 1.5.1 ??
    I need to get Temp Values from Vera. to my PID controller


  • Hardware Contributor

    No - code is > 1.6b
    @clio75 I couldnt find out what my problem was... got tired of it and uploaded same sketch so complete new set of hardware... and now its working so my occlusion was bad hardware.



  • @sundberg84
    Thanx for your reply,

    Do you know where I can find one example for getting variables from Vera ?
    I have tried searching, but can't seem to find any 😞
    Thanx


  • Hardware Contributor

    In the library you can use timeawaresensor to recieve time
    https://github.com/mysensors/Arduino/tree/development/libraries/MySensors/examples/TimeAwareSensor

    The raingauge, WaterMeterPulseSensor and Power meter to recieve V_VAR1



  • @BartE: Thank you for your corrections to the code. I changed that.

    Now the sketch works. All I did is to add a new repeaternode between the gateway and the receiving node. It was definetly a hardware problem (or the problem was solved in 1.5.3 update).

    The LED_Node_var sketch works!



  • Eh, how do you define or set a value for, let's say, V_VAR1 in Vera?



  • in the advanced tab all the way down at the bottom there are should be Variable1. Put a value in the field and save.
    the next time your device requests the variable, the new value will be sent.



  • @patrick-schaerer said:

    in the advanced tab all the way down at the bottom there are should be Variable1. Put a value in the field and save.
    the next time your device requests the variable, the new value will be sent.

    Thanks, though so, too.
    So the next question is, is it possible to add variables to sensor types that don't have those to begin with? In my case I'd like to add few variables to window covers (S_COVER in http://www.mysensors.org/download/serial_api_15). Can't test now, but would this gw.present(CHILD_ID_OF_BLINDS, V_VAR1) work in adding this property to the blinds?



  • It should work. The variable1 appears in vera after the sensor had requested var1. (But Im not completely shure about that ... )



  • @patrick-schaerer said:

    It should work. The variable1 appears in vera after the sensor had requested var1. (But Im not completely shure about that ... )

    Oh yeah, works as you suggested. Thanks!
    First I didn't get it to work, but re-reading your answer solved it. One really needs to request the variable to make it appear in the controller (Vera, that is in this case).



  • On my newest project I have several sensors on the same node. Some of those sensors demand values on Var1 and Var2.
    The code works fine so far, but I have to write those values all in the same number of digits. (sorry for my english) For example:
    Sensor 1 has V_Var1 422
    Sensor 2 has V_Var1 20
    In order to get the right value on my node I must write the value on Sensor2 in Vera like this: 020
    Otherwise Sensor 2 would receive a V_Var1 of 420.
    Also I had to write a workaround for the incoming message of the switch states. I receive a String and check if the first value is asci 48 for 0 or asci 49 for 1

    It looks to me that some memory in receiving values is left after a value is received. Is it possible to clear that memory?

    Here is the code I am talking about:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     */
    
    #define SKETCH_NAME "Bewässerung"
    #define SKETCH_VERSION "0.7a"
    
    #include <MySensor.h>  
    #include <SPI.h>
    #include "DHT.h"
    #include <Wire.h>
    #include <BH1750.h>
    
    
    
    DHT dht;
    BH1750 lightMeter;
    
    
    int dhtpin = 47;
    int strom_beet_pin = 48;
    int first_hum_pin = 0;
    int first_temp_pin = 4;
    int first_valve_pin = 43;
    
    unsigned long timer_manual;
    unsigned long timer_umgebung;
    unsigned long timer_beet;
    unsigned long zeit_umgebung = 30;
    unsigned long zeit_beet = 50;
    unsigned long zeit_manual = 60;
    unsigned int helligkeit = 700; // Schwellenwert für Licht bei Bewässerung
    int feuchtigkeit = 50; // Schwellenwert für Erdfeuchtigkeit bei Bewässerung
    int temperatur = 25; //Schwellenwert für Erdtemperatur bei Bewässerung
    
    // 0 wasser ein, 1-4 hum, 5-8 temp, 9-12 valve, 13 DHT temp, 14 DHT hum, 15 light
    float beet_data[16];
    
    //definitions for mySensors
    #define DHT_TEMP_ID 13
    #define DHT_HUM_ID 14
    #define LIGHT_ID 15
    
    //Initialize MySensors
    MyTransportNRF24 transport(49, 53);
    MySensor gw(transport);
    MyMessage msg_temp(DHT_TEMP_ID, V_TEMP);
    MyMessage msg_hum(DHT_HUM_ID, V_HUM);
    MyMessage msg_light(LIGHT_ID, V_LIGHT_LEVEL);
    MyMessage msg_hum1(1,V_HUM);
    MyMessage msg_hum2(2,V_HUM);
    MyMessage msg_hum3(3,V_HUM);
    MyMessage msg_hum4(4,V_HUM);
    MyMessage msg_temp1 (5,V_TEMP);
    MyMessage msg_temp2 (6,V_TEMP);
    MyMessage msg_temp3 (7,V_TEMP);
    MyMessage msg_temp4 (8,V_TEMP);
    MyMessage msg_valve1 (9,V_LIGHT);
    MyMessage msg_valve2 (10,V_LIGHT);
    MyMessage msg_valve3 (11,V_LIGHT);
    MyMessage msg_valve4 (12,V_LIGHT);
    //Initialize mySensor Variables
    MyMessage msg_zeit_umgebung(DHT_TEMP_ID, V_VAR1);
    MyMessage msg_helligkeit(LIGHT_ID, V_VAR1);
    MyMessage msg_feuchtigkeit(1, V_VAR1);
    MyMessage msg_zeit_beet(1, V_VAR2);
    MyMessage msg_temperatur(5, V_VAR1);
    MyMessage msg_zeit_manual(9, V_VAR1);
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200); 
      Serial1.begin(115200); 
      gw.begin(incomingMessage);
      gw.sendSketchInfo(SKETCH_NAME, SKETCH_VERSION);
      lightMeter.begin();
      dht.setup(dhtpin);
      
      pinMode(strom_beet_pin, OUTPUT);
      gw.present(DHT_TEMP_ID, S_TEMP);
      gw.present(DHT_HUM_ID, S_HUM);
      gw.present(LIGHT_ID, S_LIGHT_LEVEL);
      int i=0;
      for ( i=0;i<4;i++) {
        pinMode(first_valve_pin+i,OUTPUT);
        gw.present(i+1,S_HUM);
        gw.present(i+5,S_TEMP);
        gw.present(i+9, S_LIGHT);
        digitalWrite(first_valve_pin+i,HIGH);
      }
      getVariables();
      timer_umgebung = millis()-zeit_umgebung*1000;
      timer_beet = millis()-zeit_beet*1000;
      Serial.println("Initialized");
    }
    
    void loop() {
     gw.process(); 
      // Abfrage der Umgebungsdaten
    if (zeit_umgebung*1000 <= millis()- timer_umgebung) {
      umgebung();
      timer_umgebung = millis();
      Ausgabe();
    }
    //Abfrage der Beetdaten und evtl. Wässerung
    if (zeit_beet*1000 <= millis() - timer_beet) {
      beet();
      send_beet_data();
      wassern_entscheid();
      timer_beet = millis();
      getVariables();
    }
    for (int i=0;i<4;i++) {
      if (beet_data[9+i] == HIGH) {
      beet();
      wassern_entscheid();
      }
    }
    // Manuelle Wässerung nach zeit_manual ausschalten
    if (zeit_manual*1000 <= millis() - timer_manual && beet_data[0] == HIGH) {
      Serial.println("Manual time is up");
      for (int i=0;i<4;i++) {
        beet_data[i+9] = LOW;
        wasser(i,LOW);
        }
       beet_data[0] = LOW;
       }
    
          
    // Alle Timer nach Overflow von millis() zurücksetzen
    if (millis() < timer_umgebung) {
      timer_umgebung = millis();
      timer_beet = millis();
      timer_manual = millis();   
      }
    }
    
    void getVariables() {
      gw.request(DHT_TEMP_ID, V_VAR1);
      gw.request(LIGHT_ID, V_VAR1);
      gw.request(1, V_VAR1);
      gw.request(1, V_VAR2);
      gw.request(5, V_VAR1);
      gw.request(9, V_VAR1);
    }
    
    void wassern_entscheid() {
      /* kriterien: 
       *  Morgen oder Abend d.h. Lux unter bestimmtem Wert
       *  Feuchtigkeit Beet unter bestimmtem Wert
       *  Temperatur Beet über bestimmtem Wert ?
       */
       if (beet_data[0] == HIGH) {
        Serial.println("Manual Valve override");
       }
       else {
       
       if (beet_data[15] < helligkeit) {
        for (int i=0;i<4;i++) {
          if (beet_data[i+1] < feuchtigkeit) {
            if (beet_data[i+9] == LOW) wasser(i,HIGH);
          }
          else {
            if (beet_data[i+9] == HIGH) wasser(i,LOW);
          }
        }
       }
       else {
        for (int i=0;i<4;i++) {
          if (beet_data[i+9] == HIGH) wasser(i,LOW);
        }
       }
    }
    }
    
    void umgebung() {
      float temperature = dht.getTemperature();
      float humidity = dht.getHumidity();
      uint16_t lux = lightMeter.readLightLevel();
      gw.send(msg_temp.set(temperature,1));
      gw.send(msg_hum.set(humidity,1));
      gw.send(msg_light.set(lux,1));
      beet_data[13] = temperature;
      beet_data[14] = humidity;
      beet_data[15] = lux;
    }
    
    
    void beet() {
      digitalWrite(strom_beet_pin, HIGH);
      gw.wait(2000);
      int i=0;
      for(i = 0;i<4;i++) {
        int hum = analogRead(first_hum_pin+i);
        hum = map(hum,1023,0,0,100);
        beet_data[1+i] = hum;
        int rawvoltage= analogRead(first_temp_pin+i);
        rawvoltage = 1024 - rawvoltage;
        float temp= ((rawvoltage/1024.0) * 5000/10)-273.15;
        beet_data[5+i] = temp;
      }
      digitalWrite(strom_beet_pin, LOW);
    }
    
    
    void send_beet_data() {
        gw.send(msg_hum1.set(beet_data[1],1));
        gw.send(msg_hum2.set(beet_data[2],1));
        gw.send(msg_hum3.set(beet_data[3],1));
        gw.send(msg_hum4.set(beet_data[4],1));
        gw.send(msg_temp1.set(beet_data[5],1));
        gw.send(msg_temp2.set(beet_data[6],1));
        gw.send(msg_temp3.set(beet_data[7],1));
        gw.send(msg_temp4.set(beet_data[8],1));
        Serial.println("beet data sent");
    }
    
    void Ausgabe() {
      Serial.print("Hum: ");
     for (int i=0; i<4; i++) {
      Serial.print(beet_data[i+1]);
      Serial.print(" ");
     }
     Serial.print(" Temp: ");
     for (int i=0; i<4; i++) {
      Serial.print(beet_data[i+5]);
      Serial.print(" ");
     } 
     Serial.print(" Valve: ");
     for (int i=0; i<4; i++) {
      Serial.print(beet_data[i+9]);
      Serial.print(" ");
     }
     Serial.print(" Zeit Beet: ");
     Serial.print(millis() - timer_beet);
     Serial.println("finished");
     //Variabeln anzeigen
       Serial.print("Variables ");
       Serial.print(zeit_umgebung);
       Serial.print("  ");
       Serial.print(zeit_beet);
       Serial.print("  ");
       Serial.print(zeit_manual);
       Serial.print("  ");
       Serial.print(feuchtigkeit);
       Serial.print("  ");
       Serial.print(helligkeit);
       Serial.print("  ");
       Serial.print(temperatur);
       Serial.println("  ");
    }
    
    void wasser(int valve_nr,boolean state) {
        digitalWrite(first_valve_pin+valve_nr,!state);
        beet_data[valve_nr+9] = state;
        if (valve_nr == 0) gw.send(msg_valve1.set(state,1));
        if (valve_nr == 1) gw.send(msg_valve2.set(state,1));
        if (valve_nr == 2) gw.send(msg_valve3.set(state,1));
        if (valve_nr == 3) gw.send(msg_valve4.set(state,1));
    }
    
    void incomingMessage(const MyMessage &message) {
      int i;
      for (i=0;i<4;i++) {
         if (message.sensor == i+9) {
          if (message.type == V_LIGHT) {
            char buffer[10];
            char* empfang = message.getString(buffer);
            int asci = buffer[0];
            boolean state;
            if (asci == 49) state=HIGH;
            if (asci == 48) state = LOW;
            wasser(i,state);
            if (state == HIGH) beet_data[0] = HIGH;
            timer_manual = millis();
            Serial.print(" Valve: ");
            Serial.print(i+9);
            //Serial.print(" Empfangen:  ");
            //Serial.print(asci);
            Serial.print(" State: ");
            Serial.println(state);
          }
         }  
      }
      if (message.sensor == DHT_TEMP_ID && message.type == V_VAR1) zeit_umgebung = message.getULong();
      if (message.sensor == LIGHT_ID && message.type == V_VAR1) helligkeit = message.getInt();
      if (message.sensor == 1 && message.type == V_VAR1) feuchtigkeit = message.getInt();
      if (message.sensor == 1 && message.type == V_VAR2) zeit_beet = message.getULong();
      if (message.sensor == 5 && message.type == V_VAR1) temperatur = message.getInt();
      if (message.sensor == 9 && message.type == V_VAR1) zeit_manual = message.getULong();
    }
    
    


  • I have started to use ESP8266 for some of my devices with my Vera. So a combination of mysensors and esp's.
    How would I go around reading values via WIFI only(esp8266) ? I can do a HTTP request to put values but how would I do a READ ?

    /M


  • Admin

    The ESP gateway will send any incoming (radio messages) or locally sent messages on to the socket you have opened from the controller.

    The ESP gateway doesn't differ from the Ethernet variant in any way (from the controllers point of view).


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 1
  • 6

58
Online

11.4k
Users

11.1k
Topics

112.6k
Posts