Framework multi sensor/actuator



  • Here is a small contribution. A arduino framework for making almost every sensor or actuator you ever need.😃

    It is still a work in progress so any help is usefull.

      /**
     * 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
     * original version by Meister_Petz
     * Version 1.0 - May 5, 2016 - Ton van den Berg 
     *
     * DESCRIPTION
     * This sketch provides a framework for multisensor
     * 
     * 
     */
        #include <MySensor.h>
        #include <SPI.h>
        MySensor gw;
    
        // Manual set node id
        // Change node id for each node.
        int NodeID = 20;
        
        // Child ID s - Triggers - Buttons, Motion Sensors...
        /*
        #define TRIGGER1_ID 1 // Trigger 1
        #define TRIGGER2_ID 2 // Trigger 2
        #define TRIGGER3_ID 3 // Trigger 3
        #define TRIGGER4_ID 4 // Trigger 4
        #define TRIGGER5_ID 5 // Trigger 5
        #define TRIGGER6_ID 6 // Trigger 6
        #define TRIGGER7_ID 7 // Trigger 7 
        #define TRIGGER8_ID 8 // Trigger 8
        #define TRIGGER9_ID 9 // Trigger 9
        */
        
        // Child ID s - Actuators - Relays, LEDs,...
        /*
        #define ACTUATOR1_ID 11 // Actuator 1
        #define ACTUATOR2_ID 12 // Actuator 2
        #define ACTUATOR3_ID 13 // Actuator 3
        #define ACTUATOR4_ID 14 // Actuator 4
        #define ACTUATOR5_ID 15 // Actuator 5
        #define ACTUATOR6_ID 16 // Actuator 6
        #define ACTUATOR7_ID 17 // Actuator 7 
        #define ACTUATOR8_ID 18 // Actuator 8
        #define ACTUATOR9_ID 19 // Actuator 9
        */
        
        // Child ID s - Continuous Sensors - Temperature Sensors, Lightlevel Sensors,...
        /*
        #define SENSOR1_ID 21 // Sensor 1
        #define SENSOR2_ID 22 // Sensor 2
        #define SENSOR3_ID 23 // Sensor 3
        #define SENSOR4_ID 24 // Sensor 4
        #define SENSOR5_ID 25 // Sensor 5
        #define SENSOR6_ID 26 // Sensor 6
        #define SENSOR7_ID 27 // Sensor 7
        #define SENSOR8_ID 28 // Sensor 8
        #define SENSOR9_ID 29 // Sensor 9
        */
        
        // Child ID s - Dimmers, ...
        /*
        #define DIMMER1_ID 31 // dimmer 1
        #define DIMMER2_ID 32 // dimmer 2
        #define DIMMER3_ID 33 // dimmer 3
        #define DIMMER4_ID 34 // dimmer 4
        #define DIMMER5_ID 35 // dimmer 5
        #define DIMMER6_ID 36 // dimmer 6
        #define DIMMER7_ID 37 // dimmer 7 
        #define DIMMER8_ID 38 // dimmer 8
        #define DIMMER9_ID 39 // dimmer 9
        */
        
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // DEFINITIONS
        //-----------------------------------------------------------------------------------------------------------
        
          //------------------------------------------------------------------------
          // Triggers - Triggers - Triggers - Triggers - Triggers - Triggers
          //------------------------------------------------------------------------
          // DEFINITIONS - Push Buttons, Door Sensors,...
          #include <Bounce2.h>
          
          // Trigger PINS - Digital PIN -> Button -> GND
            /*
            #define TRIGGER1_PIN  7  
            #define TRIGGER2_PIN  7 
            #define TRIGGER3_PIN  7
            #define TRIGGER4_PIN  7
            #define TRIGGER5_PIN  7
            #define TRIGGER6_PIN  7
            #define TRIGGER7_PIN  7
            #define TRIGGER8_PIN  7
            #define TRIGGER9_PIN  7
            */
        
          // Bounce
            /* 
            Bounce debouncer1 = Bounce(); 
            Bounce debouncer2 = Bounce();
            Bounce debouncer3 = Bounce();
            Bounce debouncer4 = Bounce(); 
            Bounce debouncer5 = Bounce();
            Bounce debouncer6 = Bounce();
            Bounce debouncer7 = Bounce(); 
            Bounce debouncer8 = Bounce();
            Bounce debouncer9 = Bounce();
            */
          
          // MyMessage
            /*
            MyMessage msg01(TRIGGER1_ID,V_TRIPPED);
            MyMessage msg02(TRIGGER2_ID,V_TRIPPED);
            MyMessage msg03(TRIGGER3_ID,V_TRIPPED);
            MyMessage msg04(TRIGGER4_ID,V_TRIPPED);
            MyMessage msg05(TRIGGER5_ID,V_TRIPPED);
            MyMessage msg06(TRIGGER6_ID,V_TRIPPED);
            MyMessage msg07(TRIGGER7_ID,V_TRIPPED);
            MyMessage msg08(TRIGGER8_ID,V_TRIPPED);
            MyMessage msg09(TRIGGER9_ID,V_TRIPPED);
            */
           
          // Some Variables
            /*
            int oldTrigValue1=-1;
            int oldTrigValue2=-1;
            int oldTrigValue3=-1;
            int oldTrigValue4=-1;
            int oldTrigValue5=-1;
            int oldTrigValue6=-1;
            int oldTrigValue7=-1;
            int oldTrigValue8=-1;
            int oldTrigValue9=-1;
            */
               
          //------------------------------------------------------------------------
          // Actuators - Actuators - Actuators - Actuators - Actuators - Actuators
          //------------------------------------------------------------------------
          // DEFINITIONS - LEDs, Relays, ...
          
          // Actuators PINS - DIGITAL PIN to Actuator
            /*
            #define ACTUATOR1_PIN  3
            #define ACTUATOR2_PIN  7
            #define ACTUATOR3_PIN  7
            #define ACTUATOR4_PIN  7 
            #define ACTUATOR5_PIN  7
            #define ACTUATOR6_PIN  7
            #define ACTUATOR7_PIN  7 
            #define ACTUATOR8_PIN  7
            #define ACTUATOR9_PIN  7
            */
            
            #define ACTUATOR_ON 1
            #define ACTUATOR_OFF 0
            
            bool state;
            
          //------------------------------------------------------------------------
          // Sensors - Sensors - Sensors - Sensors - Sensors - Sensors - Sensors
          //------------------------------------------------------------------------
          // DEFINITIONS - Temperatur, Humidity, Lightlevel,...
          
          // Sensor PINS 
            /*
            #define SENSOR1_PIN  3
            #define SENSOR2_PIN  3
            #define SENSOR3_PIN  3
            #define SENSOR4_PIN  3
            #define SENSOR5_PIN  3
            #define SENSOR6_PIN  3
            #define SENSOR7_PIN  3
            #define SENSOR8_PIN  3
            #define SENSOR9_PIN  3
            */
            
          // MyMessage V_TEMP, V_LIGHT_LEVEL, ... depending on Sensor Type
            /*
            MyMessage msg11(SENSOR1_ID,V_TEMP);
            MyMessage msg12(SENSOR2_ID,V_TEMP);
            MyMessage msg13(SENSOR3_ID,V_TEMP);
            MyMessage msg14(SENSOR4_ID,V_TEMP);
            MyMessage msg15(SENSOR5_ID,V_TEMP);
            MyMessage msg16(SENSOR6_ID,V_TEMP);
            MyMessage msg17(SENSOR7_ID,V_TEMP);
            MyMessage msg18(SENSOR8_ID,V_TEMP);
            MyMessage msg19(SENSOR9_ID,V_TEMP);
            */
            
            // Interval for each Sensor Read (number of loops) 1000000 = ca. 60 Sec - Adruino Nano ATmega 328
              /*
              long int sensorInterval1 = 1000000;
              long int sensorInterval2 = 1000000;
              long int sensorInterval3 = 1000000;
              long int sensorInterval4 = 1000000;
              long int sensorInterval5 = 1000000;
              long int sensorInterval6 = 1000000;
              long int sensorInterval7 = 1000000;
              long int sensorInterval8 = 1000000;
              long int sensorInterval9 = 1000000;
              */
            
            // Special Config for each Sensor
              // SENSOR 1
              // SENSOR 2          
              // SENSOR 3
              // SENSOR 4
              // SENSOR 5
              // SENSOR 6
              // SENSOR 7
              // SENSOR 8
              // SENSOR 9
            
            //for sending only if value changed  
            float lastRead1;
            float lastRead2;
            float lastRead3;
            float lastRead4;
            float lastRead5;
            float lastRead6;
            float lastRead7;
            float lastRead8;
            float lastRead9;
            
            //for loops
            /*
            long int sI1 = sensorInterval1;
            long int sI2 = sensorInterval2;
            long int sI3 = sensorInterval3;
            long int sI4 = sensorInterval4;
            long int sI5 = sensorInterval5;
            long int sI6 = sensorInterval6;
            long int sI7 = sensorInterval7;
            long int sI8 = sensorInterval8;
            long int sI9 = sensorInterval9;
            */
          
          //------------------------------------------------------------------------
          // Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers 
          //------------------------------------------------------------------------
          // DEFINITIONS - DIMMERS, ...
          
          // Dimmer PINS - DIGITAL PIN to Actuator
            /*
            #define DIMMER1_PIN  5
            #define DIMMER2_PIN  6
            #define DIMMER3_PIN  7
            #define DIMMER4_PIN  7 
            #define DIMMER5_PIN  7
            #define DIMMER6_PIN  7
            #define DIMMER7_PIN  7 
            #define DIMMER8_PIN  7
            #define DIMMER9_PIN  7    
            */
    
            //store old lastlevel  
            static int currentLevel = 0;
            static int lastLevel1 = 0;
            static int lastLevel2 = 0;
            static int lastLevel3 = 0;
            static int lastLevel4 = 0;
            static int lastLevel5 = 0;
            static int lastLevel6 = 0;
            static int lastLevel7 = 0;
            static int lastLevel8 = 0;
            static int lastLevel9 = 0;
            
            #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)        
            
        
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // VOID SETUP - VOID SETUP - VOID SETUP - VOID SETUP - VOID SETUP
        //-----------------------------------------------------------------------------------------------------------
        
         void setup()  
        {  
          gw.begin(incomingMessage, NodeID, true);
        
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Framework sensor/actuator", "1.0");
        
          //------------------------------------------------------------------------
          // Triggers - Triggers - Triggers - Triggers - Triggers - Triggers
          //------------------------------------------------------------------------ 
          // VOID SETUP
          
              /*
              pinMode(TRIGGER1_PIN,INPUT);
              pinMode(TRIGGER2_PIN,INPUT);
              pinMode(TRIGGER3_PIN,INPUT);
              pinMode(TRIGGER4_PIN,INPUT);
              pinMode(TRIGGER5_PIN,INPUT);
              pinMode(TRIGGER6_PIN,INPUT);
              pinMode(TRIGGER7_PIN,INPUT);
              pinMode(TRIGGER8_PIN,INPUT);
              pinMode(TRIGGER9_PIN,INPUT);
              */
            
            // Activate internal pull-up
              /*
              digitalWrite(TRIGGER1_PIN,HIGH);
              digitalWrite(TRIGGER2_PIN,HIGH);
              digitalWrite(TRIGGER3_PIN,HIGH);
              digitalWrite(TRIGGER4_PIN,HIGH);
              digitalWrite(TRIGGER5_PIN,HIGH);
              digitalWrite(TRIGGER6_PIN,HIGH); 
              digitalWrite(TRIGGER7_PIN,HIGH);
              digitalWrite(TRIGGER8_PIN,HIGH);
              digitalWrite(TRIGGER9_PIN,HIGH); 
              */
            
          
            // After setting up the button, setup debouncer
              /*
              debouncer1.attach(TRIGGER1_PIN);
              debouncer2.attach(TRIGGER2_PIN);
              debouncer3.attach(TRIGGER3_PIN);
              debouncer4.attach(TRIGGER4_PIN);
              debouncer5.attach(TRIGGER5_PIN);
              debouncer6.attach(TRIGGER6_PIN);
              debouncer7.attach(TRIGGER7_PIN);
              debouncer8.attach(TRIGGER8_PIN);
              debouncer9.attach(TRIGGER9_PIN);
              */
              
              /*
              debouncer1.interval(5);
              debouncer2.interval(5);
              debouncer3.interval(5);
              debouncer4.interval(5);
              debouncer5.interval(5);
              debouncer6.interval(5);
              debouncer7.interval(5);
              debouncer8.interval(5);
              debouncer9.interval(5);
              */
            
            // present all Triggers to the Gateway
              /*
              gw.present(TRIGGER1_ID, S_DOOR);  
              gw.present(TRIGGER2_ID, S_DOOR);
              gw.present(TRIGGER3_ID, S_DOOR);
              gw.present(TRIGGER4_ID, S_DOOR);  
              gw.present(TRIGGER5_ID, S_DOOR);
              gw.present(TRIGGER6_ID, S_DOOR);
              gw.present(TRIGGER7_ID, S_DOOR);  
              gw.present(TRIGGER8_ID, S_DOOR);
              gw.present(TRIGGER9_ID, S_DOOR);
              */
              
          //------------------------------------------------------------------------
          // Actuators - Actuators - Actuators - Actuators - Actuators - Actuators
          //------------------------------------------------------------------------
          // VOID SETUP
        
              /*
              pinMode(ACTUATOR1_PIN, OUTPUT);
              pinMode(ACTUATOR2_PIN, OUTPUT);
              pinMode(ACTUATOR3_PIN, OUTPUT);
              pinMode(ACTUATOR4_PIN, OUTPUT);
              pinMode(ACTUATOR5_PIN, OUTPUT);
              pinMode(ACTUATOR6_PIN, OUTPUT);
              pinMode(ACTUATOR7_PIN, OUTPUT);
              pinMode(ACTUATOR8_PIN, OUTPUT);
              pinMode(ACTUATOR9_PIN, OUTPUT);
              */ 
              
            // present all Actuators to the Gateway
              /*
              gw.present(ACTUATOR1_ID, S_LIGHT);
              gw.present(ACTUATOR2_ID, S_LIGHT);
              gw.present(ACTUATOR3_ID, S_LIGHT);
              gw.present(ACTUATOR4_ID, S_LIGHT);
              gw.present(ACTUATOR5_ID, S_LIGHT);
              gw.present(ACTUATOR6_ID, S_LIGHT);
              gw.present(ACTUATOR7_ID, S_LIGHT);
              gw.present(ACTUATOR8_ID, S_LIGHT);
              gw.present(ACTUATOR9_ID, S_LIGHT);
              */
              
            // FETCH old State and SET old state
              /*
              bool state1 = gw.loadState(ACTUATOR1_ID); digitalWrite(ACTUATOR1_ID, state1?ACTUATOR_ON:ACTUATOR_OFF);
              bool state2 = gw.loadState(ACTUATOR2_ID); digitalWrite(ACTUATOR2_ID, state2?ACTUATOR_ON:ACTUATOR_OFF);
              bool state3 = gw.loadState(ACTUATOR3_ID); digitalWrite(ACTUATOR3_ID, state3?ACTUATOR_ON:ACTUATOR_OFF);
              bool state4 = gw.loadState(ACTUATOR4_ID); digitalWrite(ACTUATOR4_ID, state4?ACTUATOR_ON:ACTUATOR_OFF);
              bool state5 = gw.loadState(ACTUATOR5_ID); digitalWrite(ACTUATOR5_ID, state5?ACTUATOR_ON:ACTUATOR_OFF);
              bool state6 = gw.loadState(ACTUATOR6_ID); digitalWrite(ACTUATOR6_ID, state6?ACTUATOR_ON:ACTUATOR_OFF);
              bool state7 = gw.loadState(ACTUATOR7_ID); digitalWrite(ACTUATOR7_ID, state7?ACTUATOR_ON:ACTUATOR_OFF);
              bool state8 = gw.loadState(ACTUATOR8_ID); digitalWrite(ACTUATOR8_ID, state8?ACTUATOR_ON:ACTUATOR_OFF);
              bool state9 = gw.loadState(ACTUATOR9_ID); digitalWrite(ACTUATOR9_ID, state9?ACTUATOR_ON:ACTUATOR_OFF);
              */
              
          //------------------------------------------------------------------------
          // Sensors - Sensors - Sensors - Sensors - Sensors - Sensors - Sensors
          //------------------------------------------------------------------------
          // VOID SETUP  
              
            // present all Sensors to Gateway - S_TEMP, S_LIGHT_LEVEL,... depending an SENSOR type
              /*
              gw.present(SENSOR1_ID, S_TEMP);
              gw.present(SENSOR2_ID, S_TEMP); 
              gw.present(SENSOR3_ID, S_TEMP);
              gw.present(SENSOR4_ID, S_TEMP);
              gw.present(SENSOR5_ID, S_TEMP);
              gw.present(SENSOR6_ID, S_TEMP);
              gw.present(SENSOR7_ID, S_TEMP);
              gw.present(SENSOR8_ID, S_TEMP);
              gw.present(SENSOR9_ID, S_TEMP);
              */     
             
              
            // Special SETUP for each Sensor
              // SENSOR 1
              // SENSOR 2
              // SENSOR 3
              // SENSOR 4
              // SENSOR 5
              // SENSOR 6
              // SENSOR 7
              // SENSOR 8
              // SENSOR 9
        
    
    
          //------------------------------------------------------------------------
          // Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers 
          //------------------------------------------------------------------------
          // VOID SETUP
        
              /*
              pinMode(DIMMER1_PIN, OUTPUT);
              pinMode(DIMMER2_PIN, OUTPUT);
              pinMode(DIMMER3_PIN, OUTPUT);
              pinMode(DIMMER4_PIN, OUTPUT);
              pinMode(DIMMER5_PIN, OUTPUT);
              pinMode(DIMMER6_PIN, OUTPUT);
              pinMode(DIMMER7_PIN, OUTPUT);
              pinMode(DIMMER8_PIN, OUTPUT);
              pinMode(DIMMER9_PIN, OUTPUT);
              */ 
              
            // present all dimmers to the Gateway
              /*
              gw.present(DIMMER1_ID, S_DIMMER);
              gw.present(DIMMER2_ID, S_DIMMER);
              gw.present(DIMMER3_ID, S_DIMMER);
              gw.present(DIMMER4_ID, S_DIMMER);
              gw.present(DIMMER5_ID, S_DIMMER);
              gw.present(DIMMER6_ID, S_DIMMER);
              gw.present(DIMMER7_ID, S_DIMMER);
              gw.present(DIMMER8_ID, S_DIMMER);
              gw.present(DIMMER9_ID, S_DIMMER);
              */
        }  
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------------------------
        // VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP - VOID LOOP
        //-----------------------------------------------------------------------------------------------------------
        
        //  Check if digital input has changed and send in new value
        void loop() 
        {
          gw.process();
          
          //------------------------------------------------------------------------
          // Triggers - Triggers - Triggers - Triggers - Triggers - Triggers
          //------------------------------------------------------------------------ 
          // LOOP
          
              /*
              debouncer1.update();
              debouncer2.update();
              debouncer3.update();
              debouncer4.update();
              debouncer5.update();
              debouncer6.update();
              debouncer7.update();
              debouncer8.update();
              debouncer9.update();
              */
            
            // Get the update value
              /*
              int value1 = debouncer1.read(); 
              int value2 = debouncer2.read();
              int value3 = debouncer3.read();
              int value4 = debouncer4.read(); 
              int value5 = debouncer5.read();
              int value6 = debouncer6.read();
              int value7 = debouncer7.read(); 
              int value8 = debouncer8.read();
              int value9 = debouncer9.read();
              */
         
            // Send in the new value - if value changed 
              /*   
              if (value1 != oldTrigValue1) { gw.send(msg01.set(value1==LOW ? 1 : 0)); oldTrigValue1 = value1; }
              if (value2 != oldTrigValue2) { gw.send(msg02.set(value2==LOW ? 1 : 0)); oldTrigValue2 = value2; }
              if (value3 != oldTrigValue3) { gw.send(msg03.set(value3==LOW ? 1 : 0)); oldTrigValue3 = value3; }
              if (value4 != oldTrigValue4) { gw.send(msg04.set(value4==LOW ? 1 : 0)); oldTrigValue4 = value4; }
              if (value5 != oldTrigValue5) { gw.send(msg05.set(value5==LOW ? 1 : 0)); oldTrigValue5 = value5; }
              if (value6 != oldTrigValue6) { gw.send(msg06.set(value6==LOW ? 1 : 0)); oldTrigValue6 = value6; }
              if (value7 != oldTrigValue7) { gw.send(msg07.set(value7==LOW ? 1 : 0)); oldTrigValue7 = value7; }
              if (value8 != oldTrigValue8) { gw.send(msg08.set(value8==LOW ? 1 : 0)); oldTrigValue8 = value8; }
              if (value9 != oldTrigValue9) { gw.send(msg09.set(value9==LOW ? 1 : 0)); oldTrigValue9 = value9; }
              */
        
          //------------------------------------------------------------------------
          // Sensors - Sensors - Sensors - Sensors - Sensors - Sensors - Sensors
          //------------------------------------------------------------------------ 
          // LOOP
    
              /*
             // Sensor1
             if (sI1 == sensorInterval1){
               sI1 = 0;
               // put here the read sensor function like:
               // float sensorRead1 = analogRead(SENSOR1_PIN);
               if (lastRead1 != sensorRead1){
                 float sensorCalc = (sensorRead1 / 10) - 45.5;
                 gw.send(msg11.set(sensorCalc,1));
                 lastRead1 = sensorRead1;
               } 
             }
        
             // Sensor2
             if (sI2 == sensorInterval2){
               sI2 = 0;
               // put here the read sensor function like:
               // float sensorRead2 = analogRead(SENSOR2_PIN);
               if (lastRead2 != sensorRead2){
                 gw.send(msg12.set(sensorRead2,1));
                 lastRead2 = sensorRead2;
               }
             }
            
             // Sensor3
             if (sI3 == sensorInterval3){
               sI3 = 0;
               // put here the read sensor function like:
               // float sensorRead3 = analogRead(SENSOR3_PIN);
               if (lastRead3 != sensorRead3){
                 gw.send(msg13.set(sensorRead3,1));
                 lastRead3 = sensorRead3;
               }  
             }
             
             // Sensor4
             if (sI4 == sensorInterval4){
               sI4 = 0;
               // put here the read sensor function like:
               // float sensorRead4 = analogRead(SENSOR4_PIN);
               if (lastRead4 != sensorRead4){
                 gw.send(msg14.set(sensorRead4));
                 lastRead4 = sensorRead4;
               }
             }
             // Sensor5
             // Sensor6
             // Sensor7
             // Sensor8
             // Sensor9
             */
    
            /*
            sI1++;
            sI2++;
            sI3++;
            sI4++;
            sI5++;
            sI6++;
            sI7++;
            sI8++;
            sI9++;
            */
        } 
        
        
        
        //------------------------------------------------------------------------
        //------------------------------------------------------------------------
        //------------------------------------------------------------------------
        // VOID INCOMING MESSAGE - VOID INCOMING MESSAGE - VOID INCOMING MESSAGE
        //------------------------------------------------------------------------
        
        void incomingMessage(const MyMessage &message) {
        
          // We only expect one type of message from controller. But we better check anyway.
          if (message.isAck()) {
             Serial.println("This is an ack from gateway");
          }
          
          //------------------------------------------------------------------------
          // Actuators - Actuators - Actuators - Actuators - Actuators - Actuators
          //------------------------------------------------------------------------
          // VOID INCOMING MESSAGE
            if (message.type==V_LIGHT) {
              int actPin;
              
              /*    
              if (message.sensor == 11){ actPin = ACTUATOR1_PIN; }
              if (message.sensor == 12){ actPin = ACTUATOR2_PIN; }
              if (message.sensor == 13){ actPin = ACTUATOR3_PIN; }
              if (message.sensor == 14){ actPin = ACTUATOR4_PIN; }
              if (message.sensor == 15){ actPin = ACTUATOR5_PIN; }
              if (message.sensor == 16){ actPin = ACTUATOR6_PIN; }
              if (message.sensor == 17){ actPin = ACTUATOR7_PIN; }
              if (message.sensor == 18){ actPin = ACTUATOR8_PIN; }
              if (message.sensor == 19){ actPin = ACTUATOR9_PIN; }
              */
              
                //------------------------------------------------------------------------
                // switch Actuator
                //------------------------------------------------------------------------
                    state = message.getBool();
        
                    digitalWrite(actPin, state?ACTUATOR_ON:ACTUATOR_OFF);
                    gw.saveState(message.sensor, state);
                    Serial.println(); Serial.print("Change for sensor:"); Serial.print(message.sensor); Serial.print(", New status: "); Serial.println(message.getBool()); Serial.println();
                
                
                    // set state back to old state
                    gw.loadState(message.sensor);
                    
                    digitalWrite(actPin, state?ACTUATOR_ON:ACTUATOR_OFF);
                    MyMessage msgTmp(message.sensor,V_LIGHT);
                    gw.send(msgTmp.set(state));
                  }
                  
          //------------------------------------------------------------------------
          // Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - Dimmers - 
          //------------------------------------------------------------------------              
             if (message.type==V_DIMMER) {
              int pwmPin;
              
              //  Retrieve the power or dim level from the incoming request message
              int requestedLevel = atoi( message.data );          
              /*
              if (message.sensor == 31){ pwmPin = DIMMER1_PIN; currentLevel = lastLevel1; lastLevel1 = requestedLevel; }
              if (message.sensor == 32){ pwmPin = DIMMER2_PIN; currentLevel = lastLevel2; lastLevel2 = requestedLevel; }
              if (message.sensor == 33){ pwmPin = DIMMER3_PIN; currentLevel = lastLevel3; lastLevel3 = requestedLevel; }
              if (message.sensor == 34){ pwmPin = DIMMER4_PIN; currentLevel = lastLevel4; lastLevel4 = requestedLevel; }
              if (message.sensor == 35){ pwmPin = DIMMER5_PIN; currentLevel = lastLevel5; lastLevel5 = requestedLevel; }
              if (message.sensor == 36){ pwmPin = DIMMER6_PIN; currentLevel = lastLevel6; lastLevel6 = requestedLevel; }
              if (message.sensor == 37){ pwmPin = DIMMER7_PIN; currentLevel = lastLevel7; lastLevel7 = requestedLevel; }
              if (message.sensor == 38){ pwmPin = DIMMER8_PIN; currentLevel = lastLevel8; lastLevel8 = requestedLevel; }
              if (message.sensor == 39){ pwmPin = DIMMER9_PIN; currentLevel = lastLevel9; lastLevel9 = requestedLevel; }
              */
              
                //------------------------------------------------------------------------
                // set Dimmer
                //------------------------------------------------------------------------
        
                  // Clip incoming level to valid range of 0 to 100
                   requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
                   requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
     
                   Serial.println(); 
                   Serial.print( "Changing sensor " );
                   Serial.println( message.sensor );    
                   Serial.print( "Changing level to " );
                   Serial.print( requestedLevel );
                   Serial.print( ", from " ); 
                   Serial.println( currentLevel );
                   Serial.println();
    
                   fadeToLevel( requestedLevel, pwmPin );
        
                   // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                   MyMessage lightTmp(message.sensor,V_LIGHT);
                   gw.send(lightTmp.set(currentLevel > 0 ? 1 : 0));
                   
                   MyMessage msgTmp(message.sensor,V_DIMMER);
                   gw.send(msgTmp.set(currentLevel));
                  }           
            }
        
    /***
     *  This method provides a graceful fade up/down effect
     */
    void fadeToLevel( int toLevel, int pwmpin ) {
    
      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
      
      while ( currentLevel != toLevel ) {
        currentLevel += delta;
        analogWrite( pwmpin, (int)(currentLevel / 100. * 255) );
        delay( FADE_DELAY );
      }
    }```


  • any update? did it evolved? Thx in advance.


Log in to reply
 

Suggested Topics

  • 8
  • 1
  • 2
  • 5
  • 1
  • 3

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts