Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Bug Reports
  3. Strange value being sent from controller using mixed temp/relay node

Strange value being sent from controller using mixed temp/relay node

Scheduled Pinned Locked Moved Bug Reports
multiple sensormqtterror
42 Posts 6 Posters 16.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gambituk
    wrote on last edited by
    #2

    For ease of access here is the mqtt gateway sketch with the intro comments removed for pasting here,
    #include <SPI.h>
    #include <MySensor.h>
    #include "MyMQTTClient.h"
    #include "PubSubClient.h"

        #include <Ethernet.h>
        
        /*
         * To configure MQTTClientGateway.ino to use an ENC28J60 based board include
         * 'UIPEthernet.h' (SPI.h required for MySensors anyway). The UIPEthernet-library can be downloaded
         * from: https://github.com/ntruchsess/arduino_uip
         */
        
        //#include <UIPEthernet.h>
        /*
         * To execute MQTTClientGateway.ino on Yun uncomment Bridge.h and YunClient.h.
         * Do not include Ethernet.h or SPI.h in this case.
         * On Yun there's no need to configure local_ip and mac in the sketch
         * as this is configured on the linux-side of Yun.
         */
        
        //#include <Bridge.h>
        //#include <YunClient.h>
        // * Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
        /*
         #define RADIO_CE_PIN        3			// radio chip enable
         #define RADIO_SPI_SS_PIN    8			// radio SPI serial select
         #define RADIO_ERROR_LED_PIN A2  		// Error led pin
         #define RADIO_RX_LED_PIN    A1  		// Receive led pin
         #define RADIO_TX_LED_PIN    A0  		// the PCB, on board LED*/
        
        // * Use this for default configured pro mini / nano etc :
        ///*
        #define RADIO_CE_PIN        7		// radio chip enable
        #define RADIO_SPI_SS_PIN    8		// radio SPI serial select
        #define RADIO_ERROR_LED_PIN 5		// Error led pin
        #define RADIO_RX_LED_PIN    6		// Receive led pin
        #define RADIO_TX_LED_PIN    9		// the PCB, on board LED*/
        
        //replace with ip of server you want to connect to, comment out if using 'remote_host'
        uint8_t remote_ip[] =
          { 192, 168, 0, 7 };
        //replace with hostname of server you want to connect to, comment out if using 'remote_ip'
        //char* remote_ip = "server.local";
        //replace with the port that your server is listening on
        #define remote_port 1883
        //replace with arduinos ip-address. Comment out if Ethernet-startup should use dhcp. Is ignored on Yun
        uint8_t local_ip[] = {192,168,0,10};
        //replace with ethernet shield mac. It's mandatory every device is assigned a unique mac. Is ignored on Yun
        uint8_t mac[] =
          { 0x90, 0xA2, 0xDA, 0x0D, 0x07, 0x02 };
        
        //////////////////////////////////////////////////////////////////
        
        #if defined remote_ip && defined remote_host
        #error "cannot define both remote_ip and remote_host at the same time!"
        #endif
        
        #ifdef _YUN_CLIENT_H_
        YunClient ethClient;
        #else
        EthernetClient ethClient;
        #endif
        
        void
        processMQTTMessages(char* topic, byte* payload, unsigned int length);
        
        PubSubClient client(remote_ip, remote_port, processMQTTMessages, ethClient);
        MyMQTTClient gw(client,RADIO_CE_PIN, RADIO_SPI_SS_PIN);
        
        void
        setup()
        {
          Ethernet.begin(mac, local_ip);
          delay(1000);   // Wait for Ethernet to get configured.
          gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE,
          RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
        }
        
        void
        loop()
        {
          if (!client.connected())
            {
              client.connect("mysensor");
              client.subscribe("MyMQTT/#");
            }
          client.loop();
          gw.processRadioMessage();
        }
        
        void
        processMQTTMessages(char* topic, byte* payload, unsigned int length)
        {
          gw.processMQTTMessage(topic, payload, length);
        }
    
    1 Reply Last reply
    0
    • G Offline
      G Offline
      Gambituk
      wrote on last edited by
      #3

      and here is the modified relay/temp sketch

      // Running DS temperature sensor(s) and relay(s) on one mysensor arduino node
          // Combines Onewire and Relay code
          // 2014-10-14 Pego: Tested and Running on Uno/Clone and MQTT gateway
          
          // Example sketch showing how to send in OneWire temperature readings
          // Example sketch showing how to control physical relays. 
          // This example will remember relay state even after power failure.
          
          #include <MySensor.h>  
          #include <SPI.h>
          #include <DallasTemperature.h>
          #include <OneWire.h>presen
          
          #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
          #define MAX_ATTACHED_DS18B20 16
          
          #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
          #define NUMBER_OF_RELAYS 2 // Total number of attached relays
          #define RELAY_ON 1  // GPIO value to write to turn on attached relay
          #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
          
          unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) 30000 orig
          OneWire oneWire(ONE_WIRE_BUS);
          DallasTemperature sensors(&oneWire);
          MySensor gw;
          float lastTemperature[MAX_ATTACHED_DS18B20];
          int numSensors=0;
          boolean receivedConfig = false;
          boolean metric = true; 
          // Initialize temperature message
          MyMessage msg(0,V_TEMP);
          
          void setup()  
          { 
            // Startup OneWire 
            sensors.begin();
          
            // Startup and initialize MySensors library. Set callback for incoming messages. 
            //gw.begin(); 
            gw.begin(incomingMessage, AUTO, true);
          
            // Send the sketch version information to the gateway and Controller
            gw.sendSketchInfo("Temp and Relays", "1.0");
          
            // Fetch the number of attached temperature sensors  
            numSensors = sensors.getDeviceCount();
          
            // Present all sensors to controller
            for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
               gw.present(i, V_TEMP);
            }
          
            // Fetch relay status
            for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
              // Register all sensors to gw (they will be created as child devices)
              gw.present(sensor, S_LIGHT);
              // Then set relay pins in output mode
              pinMode(pin, OUTPUT);   
              // Set relay to last known state (using eeprom storage) 
              digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
            }
          
          }
          
          
          void loop()     
          {     
            // Process incoming messages (like config from server)
            gw.process(); 
          
            // Fetch temperatures from Dallas sensors
            sensors.requestTemperatures(); 
          
            // Read temperatures and send them to controller 
            for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
           
              // Fetch and round temperature to one decimal
              float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
           
              // Only send data if temperature has changed more then 1 degC and no error
              if (int(lastTemperature[i]) != int(temperature) && temperature != -127.00) { //added integer
           
                // Send in the new temperature
                gw.send(msg.setSensor(i).set(temperature,1));
                lastTemperature[i]=temperature;
              }
            }
            //gw.sleep(SLEEP_TIME); //no sleep for relays!!!!
          }
          
          void incomingMessage(const MyMessage &message) {
            // We only expect one type of message from controller. But we better check anyway.
            if (message.type==V_LIGHT) {
               // Change relay state
               digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
               // Store state in eeprom
               gw.saveState(message.sensor, message.getBool());
               // Write some debug info
               Serial.print("Incoming change for sensor:");
               Serial.print(message.sensor);
               Serial.print(", New status: ");
               Serial.println(message.getBool());
             } 
          }
      
      T 1 Reply Last reply
      0
      • G Offline
        G Offline
        Gambituk
        wrote on last edited by
        #4

        I guess this probably should have been in troubleshooting :S

        1 Reply Last reply
        0
        • gaduG Offline
          gaduG Offline
          gadu
          wrote on last edited by
          #5

          Just guessing here, have you different child id's on the temp and relay?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gambituk
            wrote on last edited by
            #6

            No it seems like they are given the same child id's. Ok, so maybe i'l look at how to force a different child id for each sensor/actuator

            1 Reply Last reply
            0
            • BulldogLowellB Offline
              BulldogLowellB Offline
              BulldogLowell
              Contest Winner
              wrote on last edited by
              #7

              the combining of the extensible sketches make it harder than needs to be.

              How many sensors are you connecting of each type?

              1 Reply Last reply
              0
              • G Offline
                G Offline
                Gambituk
                wrote on last edited by
                #8

                Hi, i have 3 temp sensors and 2 relays, I am just looking at how to make this work but am not making much progress. Cant figure out how to assign the child id's. Any help would be appreciated.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  Gambituk
                  wrote on last edited by
                  #9

                  was thinking of presenting the temps sensors first, and adding something like && i>NUMBER_OF_RELAYS to start assigining child id's above the range of relays? Will test and report back

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    Gambituk
                    wrote on last edited by Gambituk
                    #10

                    I tried to modify the temp child presentation like this..... but it seemed to have no effect

                    int offsetforrelays=5;   //(at start of sketch)
                    
                    // and this further down in the presentation for temp sensors
                    
                    
                     for (int i=0; i<(numSensors+offsetforrelays) && i<(MAX_ATTACHED_DS18B20+offsetforrelays) && i>offsetforrelays; i++) {   
                         gw.present(i, V_TEMP);
                      }
                    

                    but so far it hasn't assigned any different id's to the temp sensors. Anyone got any pointers?

                    BulldogLowellB 1 Reply Last reply
                    0
                    • G Gambituk

                      I tried to modify the temp child presentation like this..... but it seemed to have no effect

                      int offsetforrelays=5;   //(at start of sketch)
                      
                      // and this further down in the presentation for temp sensors
                      
                      
                       for (int i=0; i<(numSensors+offsetforrelays) && i<(MAX_ATTACHED_DS18B20+offsetforrelays) && i>offsetforrelays; i++) {   
                           gw.present(i, V_TEMP);
                        }
                      

                      but so far it hasn't assigned any different id's to the temp sensors. Anyone got any pointers?

                      BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #11

                      @Gambituk said:

                      #include <OneWire.h>presen

                      #include <OneWire.h>presen
                      

                      do you still have this typo in your code?

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        Gambituk
                        wrote on last edited by
                        #12

                        no, i took it out (without effect) about an hour ago

                        BulldogLowellB 1 Reply Last reply
                        0
                        • G Gambituk

                          no, i took it out (without effect) about an hour ago

                          BulldogLowellB Offline
                          BulldogLowellB Offline
                          BulldogLowell
                          Contest Winner
                          wrote on last edited by
                          #13

                          @Gambituk

                          try something like this:

                          #define NUMBER_OF_TEMP_SENSORS 3
                          
                          void setup()  
                          { 
                            sensors.begin();
                            gw.begin(incomingMessage, AUTO, true);
                            gw.sendSketchInfo("Temp and Relays", "1.0");
                            for (int i=0; i < NUMBER_OF_TEMP_SENSORS ; i++) 
                            {   
                              gw.present(i, V_TEMP); // creates 0, 1 and 2
                            }
                            for (int sensor=1, pin=RELAY_1 ; sensor <= NUMBER_OF_RELAYS ; sensor++ , pin++) 
                            {
                              gw.present(NUMBER_OF_TEMP_SENSORS + sensor, S_LIGHT); // should create 3 and 4
                              pinMode(pin, OUTPUT);   
                              digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
                            }
                          }
                          

                          you will also have to sort out the sensor numbers in the callback

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            Gambituk
                            wrote on last edited by
                            #14

                            0;0;3;0;9;read: 25-25-0 s=0,c=0,t=0,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=1,c=0,t=0,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=2,c=0,t=0,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=4,c=0,t=3,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=5,c=0,t=3,pt=0,l=5:1.4.1

                            So far it seems to create 0,1,2,4,5 but i think that's not going to be a problem, just working my way through to figure what else i need to change for the callback? (and what a callback is :D )

                            Thanks for the help so far!

                            1 Reply Last reply
                            0
                            • BulldogLowellB Offline
                              BulldogLowellB Offline
                              BulldogLowell
                              Contest Winner
                              wrote on last edited by
                              #15

                              @Gambituk said:

                              what else i need to change for the callback?

                              the call back function:

                              incomingMessage()
                              

                              look to get this right:

                              digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                              

                              so that the command matches the sensor numbers...

                              G 1 Reply Last reply
                              0
                              • BulldogLowellB BulldogLowell

                                @Gambituk said:

                                what else i need to change for the callback?

                                the call back function:

                                incomingMessage()
                                

                                look to get this right:

                                digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
                                

                                so that the command matches the sensor numbers...

                                G Offline
                                G Offline
                                Gambituk
                                wrote on last edited by
                                #16

                                @BulldogLowell

                                for (int sensor=1, pin=RELAY_1 ; sensor <= NUMBER_OF_RELAYS ; sensor++ , pin++)
                                {
                                gw.present(NUMBER_OF_TEMP_SENSORS + sensor, S_LIGHT); // should create 3 and 4

                                For the earlier part, should this be "for (int sensor=0" to get 3 and 4?

                                I get 4 and 5 ... But either way, i dont suppose it matters that there is a gap of 1 in the sensor child id's.

                                1. I change the callback to this:

                                  digitalWrite(message.sensor - 4 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);

                                and that works.... i can send a message and it activates the relay.. :D

                                1. BUT.... :(

                                   repeater started, id 25
                                   send: 25-25-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                                   read: 0-0-25 s=255,c=3,t=6,pt=0,l=1:M
                                   send: 25-25-0-0 s=255,c=3,t=11,pt=0,l=15,st=ok:Temp and Relays
                                   send: 25-25-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                   send: 25-25-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=1,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=4,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=5,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                                   send: 25-25-0-0 s=0,c=1,t=0,pt=7,l=5,st=ok:19.1
                                  

                                Here i am sending a message with 1 and then 0 to relay id4 and then the same to relay id5 -It works, but i still have the 1.3 and 0.3 values

                                    read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:1.3r
                                    Incoming change for sensor:5
                                    
                                    Incoming change for sensor:5, New status: 1
                                    
                                    read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:0.3r
                                    Incoming change for sensor:5
                                    
                                    Incoming change for sensor:5, New status: 0
                                

                                And then it starts going even crazier, only seeing the status as 1 and received command is nothing like boolean so it see's 0's and 1's as only 1's

                                    read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:1.3r
                                    Incoming change for sensor:4
                                    
                                    Incoming change for sensor:4, New status: 1
                                    
                                    read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:0.3r
                                    Incoming change for sensor:4
                                
                                    Incoming change for sensor:5, New status: 1
                                    
                                    read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:003r
                                    Incoming change for sensor:5
                                    
                                    Incoming change for sensor:5, New status: 1
                                    
                                    read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:003r
                                    Incoming change for sensor:5
                                    
                                    Incoming change for sensor:5, New status: 1
                                    
                                    read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:013r
                                    Incoming change for sensor:5
                                    
                                    Incoming change for sensor:5, New status: 1
                                    
                                    read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:113r
                                    Incoming change for sensor:4
                                    
                                    Incoming change for sensor:4, New status: 1
                                    
                                    read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:013r
                                    Incoming change for sensor:4
                                    
                                    Incoming change for sensor:4, New status: 1
                                
                                1. So i am thinking it is still a problem with what is sent from gateway somehow... :(

                                Any ideas

                                BulldogLowellB 1 Reply Last reply
                                0
                                • G Gambituk

                                  @BulldogLowell

                                  for (int sensor=1, pin=RELAY_1 ; sensor <= NUMBER_OF_RELAYS ; sensor++ , pin++)
                                  {
                                  gw.present(NUMBER_OF_TEMP_SENSORS + sensor, S_LIGHT); // should create 3 and 4

                                  For the earlier part, should this be "for (int sensor=0" to get 3 and 4?

                                  I get 4 and 5 ... But either way, i dont suppose it matters that there is a gap of 1 in the sensor child id's.

                                  1. I change the callback to this:

                                    digitalWrite(message.sensor - 4 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);

                                  and that works.... i can send a message and it activates the relay.. :D

                                  1. BUT.... :(

                                     repeater started, id 25
                                     send: 25-25-0-0 s=255,c=0,t=18,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
                                     read: 0-0-25 s=255,c=3,t=6,pt=0,l=1:M
                                     send: 25-25-0-0 s=255,c=3,t=11,pt=0,l=15,st=ok:Temp and Relays
                                     send: 25-25-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
                                     send: 25-25-0-0 s=0,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=1,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=2,c=0,t=0,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=4,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=5,c=0,t=3,pt=0,l=5,st=ok:1.4.1
                                     send: 25-25-0-0 s=0,c=1,t=0,pt=7,l=5,st=ok:19.1
                                    

                                  Here i am sending a message with 1 and then 0 to relay id4 and then the same to relay id5 -It works, but i still have the 1.3 and 0.3 values

                                      read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:1.3r
                                      Incoming change for sensor:5
                                      
                                      Incoming change for sensor:5, New status: 1
                                      
                                      read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:0.3r
                                      Incoming change for sensor:5
                                      
                                      Incoming change for sensor:5, New status: 0
                                  

                                  And then it starts going even crazier, only seeing the status as 1 and received command is nothing like boolean so it see's 0's and 1's as only 1's

                                      read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:1.3r
                                      Incoming change for sensor:4
                                      
                                      Incoming change for sensor:4, New status: 1
                                      
                                      read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:0.3r
                                      Incoming change for sensor:4
                                  
                                      Incoming change for sensor:5, New status: 1
                                      
                                      read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:003r
                                      Incoming change for sensor:5
                                      
                                      Incoming change for sensor:5, New status: 1
                                      
                                      read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:003r
                                      Incoming change for sensor:5
                                      
                                      Incoming change for sensor:5, New status: 1
                                      
                                      read: 0-0-25 s=5,c=1,t=2,pt=0,l=4:013r
                                      Incoming change for sensor:5
                                      
                                      Incoming change for sensor:5, New status: 1
                                      
                                      read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:113r
                                      Incoming change for sensor:4
                                      
                                      Incoming change for sensor:4, New status: 1
                                      
                                      read: 0-0-25 s=4,c=1,t=2,pt=0,l=4:013r
                                      Incoming change for sensor:4
                                      
                                      Incoming change for sensor:4, New status: 1
                                  
                                  1. So i am thinking it is still a problem with what is sent from gateway somehow... :(

                                  Any ideas

                                  BulldogLowellB Offline
                                  BulldogLowellB Offline
                                  BulldogLowell
                                  Contest Winner
                                  wrote on last edited by
                                  #17

                                  @Gambituk

                                  would you mind posting your latest (sorta working) code?

                                  1 Reply Last reply
                                  0
                                  • G Offline
                                    G Offline
                                    Gambituk
                                    wrote on last edited by
                                    #18

                                    // Running DS temperature sensor(s) and relay(s) on one mysensor arduino node
                                    // Combines Onewire and Relay code
                                    // 2014-10-14 Pego: Tested and Running on Uno/Clone and MQTT gateway

                                        // Example sketch showing how to send in OneWire temperature readings
                                        // Example sketch showing how to control physical relays.
                                        // This example will remember relay state even after power failure.
                                        
                                        #include <MySensor.h>
                                        #include <SPI.h>
                                        #include <DallasTemperature.h>
                                        #include <OneWire.h>
                                        
                                        #define ONE_WIRE_BUS 3 // Pin where dallas sensor is connected 
                                        #define MAX_ATTACHED_DS18B20 16
                                        
                                        #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
                                        #define NUMBER_OF_RELAYS 2 // Total number of attached relays
                                        #define RELAY_ON 1  // GPIO value to write to turn on attached relay
                                        #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
                                        #define NUMBER_OF_TEMP_SENSORS 3
                                        
                                        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) 30000 orig
                                        OneWire oneWire(ONE_WIRE_BUS);
                                        DallasTemperature sensors(&oneWire);
                                        MySensor gw;
                                        float lastTemperature[MAX_ATTACHED_DS18B20];
                                        //int numSensors = 0;
                                        boolean receivedConfig = false;
                                        boolean metric = true;
                                        // Initialize temperature message
                                        MyMessage msg(0, V_TEMP);
                                        //int offsetforrelays = 5;
                                        void setup()
                                        {
                                          // Startup OneWire
                                          sensors.begin();
                                          gw.begin(incomingMessage, AUTO, true);        // Startup and initialize MySensors library. Set callback for incoming messages.
                                          gw.sendSketchInfo("Temp and Relays", "1.0");   // Send the sketch version information to the gateway and Controller
                                        
                                          for (int i = 0; i < NUMBER_OF_TEMP_SENSORS ; i++)
                                          {
                                            gw.present(i, V_TEMP); // creates 0, 1 and 2
                                          }
                                          for (int sensor = 1, pin = RELAY_1 ; sensor <= NUMBER_OF_RELAYS ; sensor++ , pin++)
                                            //   loop from sensor1/pin4 to sensor2/pin5 (numofrelays=2)
                                          {
                                            gw.present(NUMBER_OF_TEMP_SENSORS + sensor, S_LIGHT); // should create 3 and 4
                                            pinMode(pin, OUTPUT);
                                            digitalWrite(pin, gw.loadState(sensor) ? RELAY_ON : RELAY_OFF);
                                          }
                                        }
                                        
                                         void loop()
                                        {
                                          // Process incoming messages (like config from server)
                                          gw.process();
                                        
                                          // Fetch temperatures from Dallas sensors
                                          sensors.requestTemperatures();
                                        
                                          // Read temperatures and send them to controller
                                          for (int i = 0; i < NUMBER_OF_TEMP_SENSORS && i < MAX_ATTACHED_DS18B20; i++) {
                                        
                                            // Fetch and round temperature to one decimal
                                            float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.;
                                        
                                            // Only send data if temperature has changed more then 1 degC and no error
                                            if (int(lastTemperature[i]) != int(temperature) && temperature != -127.00) { //added integer
                                        
                                              // Send in the new temperature
                                              gw.send(msg.setSensor(i).set(temperature, 1));
                                              lastTemperature[i] = temperature;
                                            }
                                          }
                                          //gw.sleep(SLEEP_TIME); //no sleep for relays!!!!
                                        }
                                        
                                        void incomingMessage(const MyMessage &message) {
                                          // We only expect one type of message from controller. But we better check anyway.
                                          if (message.type == V_LIGHT) {
                                            // Change relay state
                                            Serial.print("Incoming change for sensor:");
                                            Serial.println(message.sensor);
                                            digitalWrite(message.sensor - 4 + RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
                                            // Store state in eeprom
                                            gw.saveState(message.sensor, message.getBool());
                                            // Write some debug info
                                            Serial.print("Incoming change for sensor:");
                                            Serial.print(message.sensor);
                                            Serial.print(", New status: ");
                                            Serial.println(message.getBool());
                                          }
                                        }
                                    
                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      Gambituk
                                      wrote on last edited by Gambituk
                                      #19

                                      @BulldogLowell I just tried uploading a standard vanilla relay node, and i get the same garbled data instead of 1 and 0 (should have tried this at the start, but i am happy with what i have learned in the last 12 hours.) So it seems almost certain that it is an issue with the @gadu version of the mqtt gateway.. which is unaltered from the above post. #2 above

                                      1 Reply Last reply
                                      1
                                      • gaduG Offline
                                        gaduG Offline
                                        gadu
                                        wrote on last edited by
                                        #20

                                        Interesting findings, if you find any solution for this in the MQTT gateway I would really appreciate if you post it. I'm about to start implementing relays as well.

                                        (it's actually not my version, I think @ntruchsess is the one who created it) ;)

                                        1 Reply Last reply
                                        0
                                        • G Offline
                                          G Offline
                                          Gambituk
                                          wrote on last edited by
                                          #21

                                          @gadu if you have a setup running, could you publish a message on your mosquitto server just like this?

                                          MyMQTT/25/1/V_LIGHT 1
                                          MyMQTT/25/1/V_LIGHT 0
                                          

                                          I don't think it matters if you have any relays connected or defined, it's just to see what value it tries to send to the node... (assuming you have your gateway connected to a pc to be able to see the serial output)?

                                          No problems if this is not possible.

                                          gaduG 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          13

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular