Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. mrcage
    3. Posts
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Posts made by mrcage

    • Kitchen Node .. Messy Code..

      Hi There!

      Been playing around and used some great Sensors. Really love this sh#t 😄

      I try to combine some sensors for my kitchen:
      3 Dallas temperature sensors
      1 Motion Sensor
      1 RGB strip
      1 Door (refrigerator)

      I think I have somewhere messed up my code.
      Not sure what I've done.. Motion, Door and RGB Strip works but I don't see any Temps.

      /*
       PROJECT: MySensors / RGB test for Light & Sensor
       PROGRAMMER: AWI/GizMoCuz
       DATE: september 27, 2015/ last update: October 10, 2015
       FILE: AWI_RGB.ino
       LICENSE: Public domain
      
       Hardware: Nano and MySensors 1.5
          
       Special:
        uses Fastled library with NeoPixel (great & fast RBG/HSV universal library)       https://github.com/FastLED/FastLED
       
       Remarks:
        Fixed node-id
        Added option to request/apply last light state from gateway
        
       Domoticz typicals - 2015 10 10:
        - Domoticz is using HUE values internally, there might be a slight difference then using direct RGB colors.
      */
      
      #include <MySensor.h>
      #include <SPI.h>
      #include <FastLED.h>
      #include <DallasTemperature.h>
      #include <OneWire.h>
      #include <Bounce2.h>
      
      #define NODE_ID 1                      // fixed MySensors node id
      
      // Door
          #define CHILD_ID 6
          #define BUTTON_PIN  6  // Arduino Digital I/O pin for button/reed switch
      
      
      // Dallas temp
          #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
          #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
          #define MAX_ATTACHED_DS18B20 16
          //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
          OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
          DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
      
      // Motion Sensor
          unsigned long SLEEP_TIME = 5000; // Sleep time between reports (in milliseconds)
          #define DIGITAL_INPUT_SENSOR 4   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
          #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
          #define CHILDMotion_ID 5   // Id of the sensor child
      
      /// RGB Strip
          const int stripPin = 5;                  // pin where 2812 LED strip is connected
          const int numPixel = 39;                  // set to number of pixels
          #define CHILDRGB_ID 4                  // Child Id's
          CRGB leds[numPixel];
          char actRGBvalue[] = "000000";               // Current RGB value
          uint16_t actRGBbrightness = 0xFF ;         // Controller Brightness 
          int actRGBonoff=0;                        // OnOff flag
      
      
      // Start MySensor
      MySensor gw;
      
      //Dallas
          float lastTemperature[MAX_ATTACHED_DS18B20];
          int numSensors=3;
          boolean receivedConfig = false;
          boolean metric = true; 
          // Initialize temperature message
          MyMessage Tempmsg(0,V_TEMP);
      
      // Initialize RGB message
          MyMessage lastColorStatusMsg(CHILDRGB_ID,V_VAR1);
          
      // Initialize motion message
          MyMessage Motionmsg(CHILDMotion_ID, V_TRIPPED);
      
      // Door 
          Bounce debouncer = Bounce(); 
          int oldValue=-1;
          
          // Change to V_LIGHT if you use S_LIGHT in presentation below
          MyMessage Doormsg(CHILD_ID,V_TRIPPED);    
      
      
      void setup() {
      
        // Startup up the OneWire library
        sensors.begin();
        // requestTemperatures() will not block current thread
        sensors.setWaitForConversion(false);
      
        // 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);
        }
        
        // RGB Strip
        FastLED.addLeds<NEOPIXEL, stripPin >(leds, numPixel); // initialize led strip
      
        gw.begin(incomingMessage, NODE_ID, false);      // initialize MySensors
        gw.sendSketchInfo("Keuken Node", "1.4");
        gw.present(CHILDRGB_ID, S_RGB_LIGHT);        // present to controller
      
        // Flash the "hello" color sequence: R, G, B, black. 
        colorBars();
      
        //Request the last stored colors settings
        gw.request(CHILDRGB_ID, V_VAR1);
      
        // Door
      
           // Setup the button
          pinMode(BUTTON_PIN,INPUT);
          // Activate internal pull-up
          digitalWrite(BUTTON_PIN,HIGH);
          
          // After setting up the button, setup debouncer
          debouncer.attach(BUTTON_PIN);
          debouncer.interval(5);
          
          // Register binary input sensor to gw (they will be created as child devices)
          // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
          // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
          gw.present(CHILD_ID, S_DOOR); 
        
        // Motion Sensor
      
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        // Register all sensors to gw (they will be created as child devices)
        gw.present(CHILDMotion_ID, S_MOTION);
      }
      
      void loop() {
      
        gw.process();                       // wait for incoming messages
      
        // Door
              debouncer.update();
            // Get the update value
            int value = debouncer.read();
           
            if (value != oldValue) {
               // Send in the new value
               gw.send(Doormsg.set(value==HIGH ? 1 : 0));
               oldValue = value;
            }
      
        //Dallas Temp
      
            // Fetch temperatures from Dallas sensors
            sensors.requestTemperatures();
          
            // query conversion time and sleep until conversion completed
            int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
            // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
            gw.sleep(conversionTime);
          
            // 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 COMPARE_TEMP == 1
              if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
              #else
              if (temperature != -127.00 && temperature != 85.00) {
              #endif
           
                // Send in the new temperature
                gw.send(Tempmsg.setSensor(i).set(temperature,1));
                // Save new temperatures for next compare
                lastTemperature[i]=temperature;
              }
            }
        //gw.sleep(SLEEP_TIME); // Do I need this, as this is declared below?
        
        // Motion Sensor
            boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                  
            Serial.println(tripped);
            gw.send(Motionmsg.set(tripped?"1":"0"));  // Send tripped value to gw 
           
            // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
            gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      }
      
      void colorBars()
      {
        SendColor2AllLEDs( CRGB::Red );   FastLED.show(); delay(500);
        SendColor2AllLEDs( CRGB::Green ); FastLED.show(); delay(500);
        SendColor2AllLEDs( CRGB::Blue );  FastLED.show(); delay(500);
        SendColor2AllLEDs( CRGB::Black ); FastLED.show(); delay(500);
      } 
      
      void SendColor2AllLEDs(const CRGB lcolor)
      {
        for(int i = 0 ; i < numPixel ; i++) {
          leds[i] = lcolor;
        }
      }
      
      void SendLastColorStatus()
      {
        String cStatus=actRGBvalue+String("&")+String(actRGBbrightness)+String("&")+String(actRGBonoff);
        gw.send(lastColorStatusMsg.set(cStatus.c_str()));
      }
      
      String getValue(String data, char separator, int index)
      {
       int found = 0;
        int strIndex[] = {0, -1};
        int maxIndex = data.length()-1;
        for(int i=0; i<=maxIndex && found<=index; i++){
        if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
       }
        return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
      }
      
      void incomingMessage(const MyMessage &message) {
        if (message.type == V_RGB) {            // check for RGB type
          actRGBonoff=1;
          strcpy(actRGBvalue, message.getString());    // get the payload
          SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
          SendLastColorStatus();
        }
        else if (message.type == V_DIMMER) {           // if DIMMER type, adjust brightness
          actRGBonoff=1;
          actRGBbrightness = map(message.getLong(), 0, 100, 0, 255);
          FastLED.setBrightness( actRGBbrightness );
          SendLastColorStatus();
        }
        else if (message.type == V_STATUS) {           // if on/off type, toggle brightness
          actRGBonoff = message.getInt();
          FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
          SendLastColorStatus();
        }
        else if (message.type==V_VAR1) {            // color status
          String szMessage=message.getString();
          strcpy(actRGBvalue, getValue(szMessage,'&',0).c_str());
          actRGBbrightness=atoi(getValue(szMessage,'&',1).c_str());
          actRGBonoff=atoi(getValue(szMessage,'&',2).c_str());
          SendColor2AllLEDs(strtol(actRGBvalue, NULL, 16));
          FastLED.setBrightness((actRGBonoff == 1)?actRGBbrightness:0);
        }
        FastLED.show();
      }
      
      posted in Troubleshooting
      mrcage
      mrcage