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. My Project
  3. Remote controlled switches (433MHz) and temperature sensors

Remote controlled switches (433MHz) and temperature sensors

Scheduled Pinned Locked Moved My Project
17 Posts 5 Posters 10.5k Views 1 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.
  • CaptainZapC Offline
    CaptainZapC Offline
    CaptainZap
    wrote on last edited by
    #4

    @Dwalt Thank you for pointing that out, I am that bad at coding. Anyway, I've done that and I've tried to rewrite the part for two temperature sensors but they still don't show up on the Vera interface.
    I've done some testing and said that I should ditch RCSwitch library and use instead a simple function they provide, so I've retested the switches, and made sure that the code send is correct but I still can't control any switches. Also the temperature sensors do not show up. I need help guys, I feel I am so close it's kind of frustrating :(

    Below is the revised code :

    #include <MySensor.h>
    #include <SPI.h>  
    #include <RF24.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    //  Define Constants
    #define RF433_CHILD_ID 0
    #define NUMBER_OF_OUTLETS 3
    #define CLIENT_ID AUTO  // Sets MYSensors client id
    #define RADIO_CH 76  // Sets MYSensors to use Radio Channel
    #define MAX_ATTACHED_DS18B20 2
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    //Temp Sensor bits
    #define ONE_WIRE_BUS 4 // Pin where dallas sensor is connected - on Rboard this is (D4)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0; // >> in original temp sketch this is 0 should it be changed to 2 ?
    int RCLpin = 3;
    MySensor gw;
    MyMessage msg(0,V_TEMP);
    
     void RCLswitch(uint16_t code) {
    for (int nRepeat=0; nRepeat<6; nRepeat++) {
        for (int i=4; i<16; i++) {
            RCLtransmit(1,3);
            if (((code << (i-4)) & 2048) > 0) {
                RCLtransmit(1,3);
            } else {
                RCLtransmit(3,1);
            }
        }
        RCLtransmit(1,31);    
    }
    }
    
    void RCLtransmit(int nHighPulses, int nLowPulses) {
    digitalWrite(RCLpin, HIGH);
    delayMicroseconds( 350 * nHighPulses);
    digitalWrite(RCLpin, LOW);
    delayMicroseconds( 350 * nLowPulses);
    }
    
    void setup() 
    {
    
     Serial.begin(115200); // Not sure why this was included
    
     //  The node is mains powered, so why not make it a repeater.
     gw.begin(incomingMessage, CLIENT_ID , false,AUTO,RF24_PA_MAX,RADIO_CH,RF24_250KBPS);
    
      // Send the sketch version information to gateway
     gw.sendSketchInfo("RF433&Temp", "1.0b");
    
      //enable RF 433MHz transmitter
     pinMode(RCLpin, OUTPUT);
    
     // Register outlets to gw (they will be created as child devices)
     for(int i=0; i<NUMBER_OF_OUTLETS;i++) {
       gw.present(i+1, S_LIGHT);
    
    // 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, S_TEMP);
    
     }
    }
    }
    
    void loop() {
      gw.process();
      // Fetch temperatures from Dallas sensors
    //DallasSensors.requestTemperatures();
    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 and no error
    if (lastTemperature[i] != temperature && temperature != -127.00) {
    
      // Send in the new temperature
      gw.send(msg.setSensor(i).set(temperature,1));
      lastTemperature[i]=temperature;
    }
    }
    }
      void incomingMessage(const MyMessage &message) {
      
      if (message.type==V_LIGHT) {
      int incomingLightState =  message.getBool(); 
      int incomingOutlet = message.sensor;
    
      Serial.print("Outlet #: ");
      Serial.println(message.sensor);
      Serial.print("Command: ");
      Serial.println(message.getBool());
    
     if (incomingOutlet==1) {
     if (incomingLightState==1) {
    // Turn on  socket 1
    Serial.println("Turn on Socket 1");
    RCLswitch(0b101011000001);// These codes are unique to each outlet
     }
     if (incomingLightState==0)  {
    // Turn off socket 1
    Serial.println("Turn off Socket 1");
    RCLswitch(0b101011000010);
     }
     }
     if (incomingOutlet==2) {
     if (incomingLightState==1) {
    // Turn on  socket 2
    Serial.println("Turn on Socket 2");
    RCLswitch(0b101010100001);
     }
     if (incomingLightState==0)  {
    // Turn off socket 2
    Serial.println("Turn off Socket 2");
    RCLswitch(0b101010100010);
     }
     }
     if (incomingOutlet==3) {
     if (incomingLightState==1) {
    // Turn on  socket 3
    Serial.println("Turn on Socket 3");
    RCLswitch(0b101010010001);
     }
     if (incomingLightState==0)  {
    // Turn off socket 3
    Serial.println("Turn off Socket 3");
    RCLswitch(0b101010010010); 
     }
     }
     }
     gw.sleep(SLEEP_TIME);
     }
    
    1 Reply Last reply
    0
    • HeinzH Offline
      HeinzH Offline
      Heinz
      Hero Member
      wrote on last edited by
      #5

      To be sure that the sensor works, you should download myscontroller from here : goo.gl/9DCWNo
      (http://forum.mysensors.org/topic/838/windows-gui-controller-for-mysensors/33)

      Edit the ini file and enter the ip and port of your mysensors-gateway or com port which depends on the type of gateway you are using.
      Start MysController and click the connect button.
      Now power on your sensor and check if the sensor is sending values.

      If everything is ok then something is wrong with your vera integration. If the sensor does not show up then something is wrong with your sensor. You can also send messages to your sensor with myscontroller to check if your incomming function works.

      1 Reply Last reply
      0
      • HeinzH Offline
        HeinzH Offline
        Heinz
        Hero Member
        wrote on last edited by
        #6

        Have a look at this lib
        https://code.google.com/p/rc-switch/

        CaptainZapC 1 Reply Last reply
        0
        • HeinzH Heinz

          Have a look at this lib
          https://code.google.com/p/rc-switch/

          CaptainZapC Offline
          CaptainZapC Offline
          CaptainZap
          wrote on last edited by CaptainZap
          #7

          @Heinz I've used that lib in both my tries (it works with their sketch, but not with mine), but I will try the windows GUI and see how that goes.

          LE: While bringing the serial gateway near my computer, I broke off the mini USB port (on the nano I was testing this), something dropped on it and all hell broke loose. I'll check it out tomorrow, but I would really appreciate if someone could take a look at my code. It has to be something I used incorrectly.

          1 Reply Last reply
          0
          • blaceyB Offline
            blaceyB Offline
            blacey
            Admin
            wrote on last edited by
            #8

            @CaptainZap I would start by trying to eliminate the st=fail: issue in your logs first. That is indicative of a wireless comm problem, at times indicating a message size failure. The root cause in my case was not due to software, but due to the circuit state which I corrected by power cycling, not hard resetting, to stabilize the current/radio.

            CaptainZapC 1 Reply Last reply
            0
            • blaceyB blacey

              @CaptainZap I would start by trying to eliminate the st=fail: issue in your logs first. That is indicative of a wireless comm problem, at times indicating a message size failure. The root cause in my case was not due to software, but due to the circuit state which I corrected by power cycling, not hard resetting, to stabilize the current/radio.

              CaptainZapC Offline
              CaptainZapC Offline
              CaptainZap
              wrote on last edited by CaptainZap
              #9

              @blacey
              Thanks for the feedback, today I started from scratch with a brand spanking new arduino nano ready for flashing. I ditched the temperature sensor part because yesterday I was being a bit too cocky, and rewrote the the sketch based on @Dwalt example combined with the TypeA_lightweight example from RC Switch and I can successfully say that I was able to control my switches !!!

              YAY! , now how do I add the temperature part as I need to monitor two temperature sensors from the device.

              FYI, this is the sketch I have that works :

              #include <MySensor.h>
              #include <SPI.h>  
              #include <RF24.h>
              
              #define NUMBER_OF_OUTLETS 3 // Each outlet will have 2 OOK codes
              #define SEND_DATA 3
              
              MySensor gw;
              int RCLpin = 3;
              
              void RCLswitch(uint16_t code) {
              for (int nRepeat=0; nRepeat<6; nRepeat++) {
                  for (int i=4; i<16; i++) {
                      RCLtransmit(1,3);
                      if (((code << (i-4)) & 2048) > 0) {
                          RCLtransmit(1,3);
                      } else {
                          RCLtransmit(3,1);
                      }
                  }
                  RCLtransmit(1,31);    
              }
              }
              
              void RCLtransmit(int nHighPulses, int nLowPulses) {
              digitalWrite(RCLpin, HIGH);
              delayMicroseconds( 350 * nHighPulses);
              digitalWrite(RCLpin, LOW);
              delayMicroseconds( 350 * nLowPulses);
              }
              
              void setup() 
              {
              
               //  The node is mains powered, so why not make it a repeater.
               gw.begin(incomingMessage, AUTO, true);
              
               // Send the sketch version information to gateway
               gw.sendSketchInfo("RF433", "0.1");
              
               pinMode(RCLpin, OUTPUT);
              
               // Register outlets to gw (they will be created as child devices)
              for(int i=0; i<NUMBER_OF_OUTLETS;i++) {
                 gw.present(i+1, S_LIGHT);
              
               }
              
              }
              
              void loop() {
                gw.process();
              }
                void incomingMessage(const MyMessage &message) {
              
                if (message.type==V_LIGHT) {
                int incomingLightState =  message.getBool(); 
                int incomingOutlet = message.sensor;
              
                Serial.print("Outlet #: ");
                Serial.println(message.sensor);
                Serial.print("Command: ");
                Serial.println(message.getBool());
              
               if (incomingOutlet==1) {
               if (incomingLightState==1) {
              // Turn on  socket 1
              Serial.println("Turn on Socket 1");
               RCLswitch(0b101011000001);// These codes are unique to each outlet
               delay(50); 
               }
               if (incomingLightState==0)  {
              // Turn off socket 1
               Serial.println("Turn off Socket 1");
               RCLswitch(0b101011000010);
               delay(50); 
               }
               }
               if (incomingOutlet==2) {
               if (incomingLightState==1) {
              // Turn on  socket 2
              Serial.println("Turn on Socket 2");
               RCLswitch(0b101010100001);
               delay(50); 
               }
               if (incomingLightState==0)  {
              // Turn off socket 2
               Serial.println("Turn off Socket 2");
               RCLswitch(0b101010100010);
               delay(50); 
               }
               }
               if (incomingOutlet==3) {
               if (incomingLightState==1) {
              // Turn on  socket 3
              Serial.println("Turn on Socket 3");
               RCLswitch(0b101010010001);
               delay(50); 
               }
               if (incomingLightState==0)  {
              // Turn off socket 3
               Serial.println("Turn off Socket 3");
               RCLswitch(0b101010010010);
               delay(50); 
               }
               }
               }
               delay(50);
               }
              
              1 Reply Last reply
              0
              • CaptainZapC Offline
                CaptainZapC Offline
                CaptainZap
                wrote on last edited by CaptainZap
                #10

                Just an update on this, I've spent several hours today but I was able to get the sketch to show up 1 temperature sensor, and then two temperature sensors. Now the problem I have is that the two temperature sensors display the same value. They are connected to different pins on the arduino, 4 and 5, and are not wired in parallel. This is the temperature sensor part :

                OneWire oneWire(ONE_WIRE_BUS);
                DallasTemperature DallasSensors(&oneWire);
                float lastTemperature ;
                float lastTemperature1 ;
                #define CHILD_ID_T1 4 //CHILD ID for temperature sensor1
                #define CHILD_ID_T2 5 //CHILD ID for temperature sensor2
                
                MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
                MyMessage tempMsg2(CHILD_ID_T2,V_TEMP);
                
                
                void setup() 
                {
                
                {...} // the lights part
                 gw.present(CHILD_ID_T1, S_TEMP);
                 gw.present(CHILD_ID_T2, S_TEMP);
                }
                
                void loop() {
                  gw.process();
                  // Fetch temperatures from Dallas sensors
                	DallasSensors.requestTemperatures();
                	float tempC = DallasSensors.getTempCByIndex(1);
                            float tempD = DallasSensors.getTempCByIndex(2);
                	
                	// Only send data if temperature has changed and no error
                	if (lastTemperature != tempC && tempC != -127.00) {
                		
                		// Send in the new temperature
                		gw.send(tempMsg.set(tempC,1));
                		lastTemperature=tempC;
                	}  
                          // Only send data if temperature has changed and no error
                	if (lastTemperature1 != tempD && tempD != -127.00) {
                		
                		// Send in the new temperature
                		gw.send(tempMsg2.set(tempD,1));
                		lastTemperature1=tempD;
                	}
                }
                

                Anyone got any hints ? I'm so close on finalizing this ! I've learned a lot over the last couple of days :D

                LE: After writing this here I realized I didn't declare the second pin in the code and thus was using the same one. I've corrected that and this WAS IT !

                1 Reply Last reply
                0
                • tbowmoT Offline
                  tbowmoT Offline
                  tbowmo
                  Admin
                  wrote on last edited by
                  #11

                  @CaptainZap

                  Why did you connect the two sensors to two different pins on the arduino? Onewire bus is exactly what it says, only a single wire, and a bus (meaning that it supports multiple devices)

                  you have

                  OneWire onewire(ONE_WIRE_BUS)
                  

                  ONE_WIRE_BUS is the pin that all your DS1820 sensors are connected to..

                  CaptainZapC 1 Reply Last reply
                  0
                  • tbowmoT tbowmo

                    @CaptainZap

                    Why did you connect the two sensors to two different pins on the arduino? Onewire bus is exactly what it says, only a single wire, and a bus (meaning that it supports multiple devices)

                    you have

                    OneWire onewire(ONE_WIRE_BUS)
                    

                    ONE_WIRE_BUS is the pin that all your DS1820 sensors are connected to..

                    CaptainZapC Offline
                    CaptainZapC Offline
                    CaptainZap
                    wrote on last edited by CaptainZap
                    #12

                    @tbowmo Because I don't know how to use the library correctly. I don't know how to display two sensors connected to the same port so I've done it how I knew. I am a beginner at coding and I know it can be simplified but this is as much as I know. I would like to see how you would do it :)

                    This is my last code for the temperature part:

                    #define ONE_WIRE_BUS 4 // Pin where dallas sensor is connected - on Rboard this is A0(D14)
                    #define ONE_WIRE_BUS2 5
                    OneWire oneWire(ONE_WIRE_BUS);
                    DallasTemperature DallasSensors(&oneWire);
                    
                    OneWire oneWire2(ONE_WIRE_BUS2);
                    DallasTemperature DallasSensors2(&oneWire2);
                    
                    float lastTemperature ;
                    float lastTemperature1 ;
                    #define CHILD_ID_T1 4 //CHILD ID for temperature sensor1
                    #define CHILD_ID_T2 5 //CHILD ID for temperature sensor2
                    
                    MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
                    MyMessage tempMsg2(CHILD_ID_T2,V_TEMP);
                    
                    void setup() 
                    {
                    
                    {...} // the lights part
                     gw.present(CHILD_ID_T1, S_TEMP);
                     gw.present(CHILD_ID_T2, S_TEMP);
                    }
                    void loop() {
                      gw.process();
                      // Fetch temperatures from Dallas sensors
                    	DallasSensors.requestTemperatures();
                    	float tempC = DallasSensors.getTempCByIndex(1);
                                DallasSensors2.requestTemperatures();
                                float tempD = DallasSensors2.getTempCByIndex(1);
                    	
                    	// Only send data if temperature has changed and no error
                    	if (lastTemperature != tempC && tempC != -127.00) {
                    		
                    		// Send in the new temperature
                    		gw.send(tempMsg.set(tempC,1));
                    		lastTemperature=tempC;
                    	}  
                              // Only send data if temperature has changed and no error
                    	if (lastTemperature1 != tempD && tempD != -127.00) {
                    		
                    		// Send in the new temperature
                    		gw.send(tempMsg2.set(tempD,1));
                    		lastTemperature1=tempD;
                    	}
                    }
                    
                    tbowmoT 1 Reply Last reply
                    0
                    • HeinzH Offline
                      HeinzH Offline
                      Heinz
                      Hero Member
                      wrote on last edited by Heinz
                      #13

                      Indexes usually start at 0 not at 1
                      try getTempCByIndex(0);

                      1 Reply Last reply
                      0
                      • CaptainZapC Offline
                        CaptainZapC Offline
                        CaptainZap
                        wrote on last edited by CaptainZap
                        #14

                        As per @Heinz suggestion I was able to use only one pin to get both temperature values, now I'm looking at lowering the refresh rate interval, now happens very fast. He suggested using millis(), but didn't help much.

                        Also I have one other question, I've noticed that when the node is on my desk (about 5 meters and 1 wall) from the gateway the RF switches no longer work, but the sensors do keep updating without any issues. I don't understand exactly why would this be the case.

                        LE: Seems that the issue with the RF is related to the power output from my USB port, I changed to a different one and it works without issue. Weird anyway.

                        1 Reply Last reply
                        0
                        • CaptainZapC CaptainZap

                          @tbowmo Because I don't know how to use the library correctly. I don't know how to display two sensors connected to the same port so I've done it how I knew. I am a beginner at coding and I know it can be simplified but this is as much as I know. I would like to see how you would do it :)

                          This is my last code for the temperature part:

                          #define ONE_WIRE_BUS 4 // Pin where dallas sensor is connected - on Rboard this is A0(D14)
                          #define ONE_WIRE_BUS2 5
                          OneWire oneWire(ONE_WIRE_BUS);
                          DallasTemperature DallasSensors(&oneWire);
                          
                          OneWire oneWire2(ONE_WIRE_BUS2);
                          DallasTemperature DallasSensors2(&oneWire2);
                          
                          float lastTemperature ;
                          float lastTemperature1 ;
                          #define CHILD_ID_T1 4 //CHILD ID for temperature sensor1
                          #define CHILD_ID_T2 5 //CHILD ID for temperature sensor2
                          
                          MyMessage tempMsg(CHILD_ID_T1,V_TEMP);
                          MyMessage tempMsg2(CHILD_ID_T2,V_TEMP);
                          
                          void setup() 
                          {
                          
                          {...} // the lights part
                           gw.present(CHILD_ID_T1, S_TEMP);
                           gw.present(CHILD_ID_T2, S_TEMP);
                          }
                          void loop() {
                            gw.process();
                            // Fetch temperatures from Dallas sensors
                          	DallasSensors.requestTemperatures();
                          	float tempC = DallasSensors.getTempCByIndex(1);
                                      DallasSensors2.requestTemperatures();
                                      float tempD = DallasSensors2.getTempCByIndex(1);
                          	
                          	// Only send data if temperature has changed and no error
                          	if (lastTemperature != tempC && tempC != -127.00) {
                          		
                          		// Send in the new temperature
                          		gw.send(tempMsg.set(tempC,1));
                          		lastTemperature=tempC;
                          	}  
                                    // Only send data if temperature has changed and no error
                          	if (lastTemperature1 != tempD && tempD != -127.00) {
                          		
                          		// Send in the new temperature
                          		gw.send(tempMsg2.set(tempD,1));
                          		lastTemperature1=tempD;
                          	}
                          }
                          
                          tbowmoT Offline
                          tbowmoT Offline
                          tbowmo
                          Admin
                          wrote on last edited by
                          #15

                          @CaptainZap said:

                          @tbowmo Because I don't know how to use the library correctly. I don't know how to display two sensors connected to the same port so I've done it how I knew. I am a beginner at coding and I know it can be simplified but this is as much as I know. I would like to see how you would do it :)

                          Take a look at the dallas example that comes with the mysensors library it supports multiple ds1820s.

                          http://www.mysensors.org/build/temp

                          Think that everything you need is there.

                          1 Reply Last reply
                          0
                          • HeinzH Offline
                            HeinzH Offline
                            Heinz
                            Hero Member
                            wrote on last edited by
                            #16

                            @CaptainZap I had the same problem with the range.
                            The sensor is able to send data to the gateway but the gateway fails to send commands to the sensor.
                            I found out that the maximum power level of the gateway sender is not set to maximum by default, but the
                            level of the sensor is. I simply changed the value before compiling the gateway in
                            MyConfig.h
                            #define RF24_PA_LEVEL_GW RF24_PA_MAX

                            CaptainZapC 1 Reply Last reply
                            0
                            • HeinzH Heinz

                              @CaptainZap I had the same problem with the range.
                              The sensor is able to send data to the gateway but the gateway fails to send commands to the sensor.
                              I found out that the maximum power level of the gateway sender is not set to maximum by default, but the
                              level of the sensor is. I simply changed the value before compiling the gateway in
                              MyConfig.h
                              #define RF24_PA_LEVEL_GW RF24_PA_MAX

                              CaptainZapC Offline
                              CaptainZapC Offline
                              CaptainZap
                              wrote on last edited by
                              #17

                              @Heinz Thanks for pointing that out, since I plan to have most if not all my sensors ac powered I'll change that.

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


                              14

                              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