How to add a sensor to the serial gateway



  • Hi,

    Maybe this is a stupid question, but I've only got my first Arduino Uno in two days ago and I'm a complete mysensors newbie... Already have been working with Domoticz on Rpi for over a year now. Recently I've build a water irrigation system for my garden, controlled by a relay which can be switched through Domoticz using a GPIO. Now I would like to add some ground moisture sensors, using a Arduino and the domoticz Gateway to Mysensors to water the garden when the soil gets too dry.
    I've uploaded the serialgateway sketch to an Arduino Uno and I also have a sketch to read the output of the sensor using the Analog output. Now my question is, how can a add this sketch to the arduino without deleting the gateway sketch?

    If I understand correctly, uploading the gateway sketch is obviously the first step, after that you'll have to upload specific sketches for various sensors to be able to add a 'node' ?

    Can anyone put me on the right track? Thanks in advance!


  • Hero Member

    @Bram81 domoticz with mysensors is awesome. However, the gateway is one arduino with nrf that is connected to the rpi and notify the controller what is happening. The sensor nodes are also arduinos with nrf and they are running the sensor sketch. Hope this clarified thing a bit...



  • @Moshe-Livne Thanks for your reply! So if I understand correctly there is one Arduino with the serial sketch on it that acts as the gateway and I have to use another arduino to add sensors to. Will a second Arduino connected to the Rpi through USB and running my sensor sketch be picked up by the gateway automatically?


  • Hardware Contributor

    Yes you can do that. For example, you can have a gateway and a node connected on your computer so you can serial debug each. But of course, one Gateway (gateway sketch) for n nodes (nodes sketch).



  • @scalz Ok, that is what I was hoping... Right now I have one Arduino Uno conncted through USB to my Rpi running the gateway sketch on port ACM0

    Another Uno which has a soil moisture sensor connected to it and running a sketch is also connected through USB to ACM1 port. Still I can't see any new devices in Domoticz...

    Would this be the way to go or am I doing something verry wrong..?


  • Hardware Contributor

    I think you have to add the gateway as hardware in domoticz. Then when the gateway recieve info from the node/sensor its transferred into domoticz. If you have connected the node/sensor through the GPIO i think you need to create some sort of virtual device and program this in domoticz, but then the gateway are not used. The best way is Domoticz -> Gateway -> Sensors.

    The MySensors use the radio to talk to the gateway... i dont know how to do that with a wire/usb.



  • Thanks for your post. Aha I thought that working without radio was the difference between serial and ethernet gateways. I've ordered some nRF24L01+. modules, probably getting them in a couple of days.

    So I'll have to connect one of those to the gateway and one to the arduino Uno holding my moisture sensors?


  • Hero Member

    @Bram81 yes.... no way around it. the controller talks with the gateway through the USB (Serial)
    or Ethernet (Ethernet gateway) but the gateway communicates with the sensor nodes through the nrf modules.


  • Admin

    @Bram81

    You try using the serial gateway in this branch:
    https://github.com/mysensors/Arduino/tree/gateway-refactoring

    It allows sensors to be attached to the gateway itself. Note that it is still WIP (and I'm unsure if all controllers support this).



  • Thanks everybody! Got the serial gateway working and it's being picked up by domoticz. Now another Arduino Uno is holding a moisture sensor, it's communicating with the gateway, but Domoticz sees the sensor as a lamp so it's not logging the sensor output. The sensor is a YL-38. Is there a way to fix this or is this type simply not supported by Domoticz?


  • Hardware Contributor

    Hi!

    Is the sensor presented correctly - what does the domoticz log say when you power up the sensor?
    Do you see anything in the domoticz and or gateway log when sending temp-values?



  • @sundberg84 Hey!

    Domoticz log shows:
    015-07-01 15:44:03.331 MySensors: Gateway Ready...
    2015-07-01 15:44:28.776 Hardware Monitor: Fetching data (System sensors)
    2015-07-01 15:44:41.187 (Gateway) Lighting 2 (Vochtigheid border)
    2015-07-01 15:44:45.357 (Gateway) Lighting 2 (Vochtigheid border)
    2015-07-01 15:44:58.920 Hardware Monitor: Fetching data (System sensors)
    2015-07-01 15:45:28.298 MySensors: Node: 3, Sketch Name: Soil Moisture Sensor
    2015-07-01 15:45:28.302 MySensors: Node: 3, Sketch Version: 1.0

    Hooking up the gateway and node to a laptop to use the serial monitor shows data being send from the sensor and received by the gateway.

    After plugging in the gateway to my pi running domoticz there was a new hardware detected right away, but only not as a sensor but as a lightning2 type lamp switch which I've named 'vochtigheid border'. The log of this 'switch itself shows:
    2015-07-01 15:44:45 On
    2015-07-01 15:44:41 Off
    2015-07-01 15:41:02 On
    2015-07-01 14:49:43 Off
    2015-07-01 14:31:56 On
    2015-07-01 14:31:56 Off

    Does this make any sense to you...?



  • Oh and I'm using this sketc btw : http://www.mysensors.org/build/moisture


  • Hardware Contributor

    Hi @Bram81
    Looks good so far... i was just about to suggest the domoticz forum, but then i saw this: link text and i guess its you 🙂

    In the link (Rain & Moisture scetch) it looks like the GW.present: gw.present(CHILD_ID, S_MOTION);
    Does this mean its presented as a motion sensor? @hek ?

    Anyhow - in the Domoticz code there is not any code for S_MOTION so you have to request this.
    MySensorsBase.cpp

    I dont know what happens if you try to present is a humidity sensor? Both humidity and soil is presented as % ??

    Br



  • Hi@sundberg84 Yes, that's me.. 😉 I saw that file too, but being still a newbie in Mysensors, Arduino and sketches I didn't really know how to use it. Do I have to put in my libraries folder and add a line #include <filename> in the gateway sketch..?

    Right now the sensor is automatically seen as a certain device, within Domoticz there is no way to present it otherwise right?


  • Hardware Contributor

    If you mean MySensorsBase.cpp - no thats the domoticz scourscode and you dont use that in the sensor/gateway.
    Looking closer at the code shows also your sensors sends V_TRIPPED to domoticz, and thats why you get on/off.

    I dont know it this works, but i would try to change:
    MyMessage msg(CHILD_ID, V_TRIPPED);
    to
    MyMessage msg(CHILD_ID, V_HUM);

    and
    gw.present(CHILD_ID, S_MOTION);
    to
    gw.present(CHILD_ID, S_HUM);

    and upload the scetch to the Moisture sensor again...
    It will present the sensor in domoticz as a Humidity sensor but as I said i hope both Moisture and Humidity presents its value from 0-100 %

    Br



  • Hey, thanks for your help. The sensor presents itself as a Humidity sensor and shows a value of 1% even after adding water to the ground nothing changes, so it seems it's not being updated correctly.

    Last week when I was just playing around for the first time with the arduino and sensor used this simple sketch:

    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor

    void setup() {
    // declare the ledPin as an OUTPUT:
    Serial.begin(9600);
    }

    void loop() {
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    Serial.print("sensor = " );
    Serial.println(sensorValue);
    }

    Using the analog output on the sensor and analog0 on the arduino. The values are presented in number between 1-1200 (aprox.)

    Even readings in just a number would be fine for me / can be easily transered to a % I guess, where a value of aprox 300 is liquid water and 1050 is the value read when the sensor is in the open air and not sticked in the soil.

    Can you think of alike types of sensors with values presented in that range?


  • Hardware Contributor

    Hi @Bram81

    Yes - the analog is 0 - 1023 and many use it to measure battery %.
    This can be converted to % pretty much by dividing it by 10 i think

    int sensorValue = analogRead(sensorPin);
    Serial.println(sensorValue);
    sensorValue  = sensorValue / 10;
    

    You can do much more with upper and lower limits... its just a matter of math and code.

    Edit:
    This is used in the battery when we want figures from 1.9 (VMIN)-3.3V (VMAX)... maybe it can help you somhow:

    int Pcnt = static_cast<int>(((Vbat-VMIN)/(VMAX-VMIN))*100.);```

  • Hardware Contributor

    Doing some fast math here: Vbat = your analog read value (say 500 as example), VMIN = 300 VMAX = 1023 gives us
    500 - 300 / 1023-300 = 200/ 723 = 0,27 = 27%



  • @sundberg84 Hi, I've been trying to modify the sketch
    screenshot sketch analog.png
    But it gives me errors I cant resolve... Got to a point where the node send back info to the gateway:

    683
    send: 3-3-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
    682
    send: 3-3-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
    683
    send: 3-3-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0
    682
    send: 3-3-0-0 s=0,c=1,t=1,pt=2,l=2,st=ok:0

    Also Domoticz was receiving info, log showed 👍
    2015-07-02 10:31:24.898 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.914 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.929 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.944 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.959 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.975 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:24.990 (Gateway) Temp + Humidity (TempHum)
    2015-07-02 10:31:25.005 (Gateway) Temp + Humidity (TempHum)

    But still the humidity switch was showing either 0% or 1%.
    I guess that is propably because the values must be transfered into % in the sketch, but I am kind of stuck...


  • Hardware Contributor

    @Bram81
    I would try (not at home and cant try):

    int Analog_Input_Soil_Sensor= A0;
    int Analog_Input_Soil_Sensor = 0;

    as you wrote before, and not #define interrupt analog_input_soil_sensor -2

    Also you get only 0 and 1 because this is what you send to the gateway: gw.send(Msg.set(soilva... 0?1:0)
    Either you have to define what 0 and 1 is (say dry = 0 moist = 1) or if you want to send it as % use another gw.send code.


  • Hardware Contributor

    Here is an example of someone build a soil sensor using the light_sensor
    http://forum.mysensors.org/topic/1492/soil-moisture-analog-input-question
    If you remove the battery code this could be used if you connect to analog pin (Se code below)

    #include <SPI.h>
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    
    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    int lastLightLevel;
    
    void setup()
    {
    gw.begin();
    
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Soil_Moist_Sensor_grb", "1.15");
    
    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()
    {
    int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
    Serial.println(lightLevel);
    if (lightLevel != lastLightLevel) {
    gw.send(msg.set(lightLevel));
    lastLightLevel = lightLevel;
    }
    }
    
    

    if you want lightlevel in % with a Min and a Max then:

    #include <SPI.h>
    #include <MySensor.h>
    
    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define MIN 300  // Liquid
    #define MAX 1023 // Air
    
    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    int lastLightLevel;
    
    void setup()
    {
    gw.begin();
    
    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Soil_Moist_Sensor_grb", "1.15");
    
    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }
    
    void loop()
    {
    int lightLevel = static_cast<int>(((analogRead(LIGHT_SENSOR_ANALOG_PIN)-MIN)/(MAX-MIN))*100.);
    Serial.println(lightLevel);
    if (lightLevel != lastLightLevel) {
    gw.send(msg.set(lightLevel));
    lastLightLevel = lightLevel;
    }
    }
    
    

    I have read some more...
    About the digital pin and use (as you mentioned at first... )
    It looks like there is a switch/nob on the hardware you can turn.
    This will set the trigger point between dry and moist and send 1 or 0.
    I think thats why the scetches was using S_Motion... either its dry (untriggered) or wet (triggered).
    I dont know how to program or wire this though...

    Best of luck 🙂 Hope i didnt just complicate things...



  • @sundberg84 Hey! Thank you verry much for your help and suggestions!!
    I actually got it working combining your sketch witch my simple one resulting in this:

    #include <SPI.h>
    #include <MySensor.h>

    #define CHILD_ID_LIGHT 0
    #define LIGHT_SENSOR_ANALOG_PIN 1
    #define MIN 300 // Liquid
    #define MAX 1023 // Air

    int sensorPin = A0; // select the input pin for the potentiometer
    int sensorValue = 0; // variable to store the value coming from the sensor

    MySensor gw;
    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    int lastLightLevel;

    void setup(){

    Serial.begin(9600);

    gw.begin();

    // Send the sketch version information to the gateway and Controller
    gw.sendSketchInfo("Soil_Moist_Sensor_grb", "1.15");

    // Register all sensors to gateway (they will be created as child devices)
    gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    }

    void loop(){
    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);
    delay(1000);
    Serial.print("sensor = " );
    Serial.println(sensorValue);
    gw.send(msg.set(sensorValue));
    }

    The sensor shows up in Domoticz as a light sensor meassuring in lux, but the numbers do match the output of the sensor.

    Another thing is that after about 15 min the gateway started functioning. Seems like a problem that I had last week returned...
    Loading the board with the pingpongtest shows that suddenly my radio chip is recognized as NRF24L01 instead of the + version, although the label on te chips shows NRF24L01+
    Changing radios doesn't help, I'm completely in the dark... maybe just shitty radios..?



  • Hi @hek , the link above it's broken. Is it yet available anywhere?
    Thank you.


  • Admin

    It has been merged now. Available here:
    https://github.com/mysensors/Arduino


Log in to reply
 

Suggested Topics

  • 5
  • 1
  • 5
  • 3
  • 4
  • 1

38
Online

11.4k
Users

11.1k
Topics

112.6k
Posts