Navigation

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

    Best posts made by Andreas Henriksson

    • RE: Read string from serial port and send it to gateway

      Hello, i don't using this any more but i found this on my laptop.
      I can't confirm that i work, but it the latest version i have.

      #include <SPI.h>
      #include <MySensor.h>
      #include <DHT.h>
      
      #define CHILD_ID_INFO 0
      #define CHILD_ID_SOC 1
      #define CHILD_ID_STATE 2
      #define CHILD_ID_ERROR 3
      #define CHILD_ID_SET_STATE 4
      #define CHILD_ID_TEMP 5
      #define CHILD_ID_HUM 6
      
      //Pin
      #define HUMIDITY_SENSOR_DIGITAL_PIN 3
      
      MySensor gw;
      MyMessage info(CHILD_ID_INFO, V_VAR1);
      MyMessage soc(CHILD_ID_SOC, V_VAR2);
      MyMessage state(CHILD_ID_STATE, V_VAR3);
      MyMessage error(CHILD_ID_ERROR, V_VAR4);
      MyMessage temp(CHILD_ID_TEMP, V_TEMP);
      MyMessage hum(CHILD_ID_HUM, V_HUM);
      
      DHT dht;
      
      //DHT Sensor
      const byte numChars = 25;
      char receivedChars[numChars];
      float lastTemp;
      float lastHum;
      boolean metric = true;
      
      //MySensorr
      int mySensorType;
      int mySensorMsg;
      
      String mySensorStr;
      String mySensorRaw;
      
      String mySensorRawSoc;
      String mySensorStrSoc;
      
      //Serial Read
      boolean newData = false;
      
      void setup()
      {
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
      
        gw.begin(incomingMessage, 1, false);
        Serial.begin(115200);
      
        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Mover", "1.0");
      
        // Register all sensors to gateway (they will be created as child devices)
        gw.present(CHILD_ID_INFO, S_CUSTOM);
        gw.present(CHILD_ID_SOC, S_CUSTOM);
        gw.present(CHILD_ID_STATE, S_CUSTOM);
        gw.present(CHILD_ID_ERROR, S_CUSTOM);
        gw.present(CHILD_ID_SET_STATE, S_CUSTOM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        gw.present(CHILD_ID_HUM, S_HUM);
      
        metric = gw.getConfig().isMetric;
      }
      
      void loop()
      {
        recvWithStartEndMarkers();
        showNewData();
        gw.process();
      
      }
      
      void incomingMessage(const MyMessage &message)
      {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type == 28)
        {
          if (message.getInt() == 99)
          {
            float temperature = dht.getTemperature();
            //if (isnan(temperature))
            //{
            //  Serial.println("Failed reading temperature from DHT");
            //}
            
            if (temperature != lastTemp)
            {
              lastTemp = temperature;
              if (!metric)
              {
                temperature = dht.toFahrenheit(temperature);
              }
              
              //Serial.print("T: ");
              //Serial.println(temperature);
            }
            gw.send(temp.set(temperature, 1));
      
            float humidity = dht.getHumidity();
            //if (isnan(humidity))
            //{
            //  Serial.println("Failed reading humidity from DHT");
            //}
            if (humidity != lastHum)
            {
              lastHum = humidity;
              
              //Serial.print("H: ");
              //Serial.println(humidity);
            }
            gw.send(hum.set(humidity, 1));
      
      
          }
          else {
            //Serial.println(message.type);
            //Serial.print("Incoming change for sensor:");
            //Serial.println(message.sensor);
            //Serial.print(", New status: ");
            Serial.print(message.getInt());
          }  
        }
      
      
        //}
      }
      
      void recvWithStartEndMarkers()
      {
        static boolean recvInProgress = false;
        static byte ndx = 0;
        char startMarker = '<';
        char endMarker = '>';
        char rc;
      
        // if (Serial.available() > 0) {
        while (Serial.available() > 0 && newData == false)
        {
          rc = Serial.read();
      
          if (recvInProgress == true)
          {
            if (rc != endMarker)
            {
              receivedChars[ndx] = rc;
              ndx++;
              if (ndx >= numChars)
              {
                ndx = numChars - 1;
              }
      
            }
            else
            {
              receivedChars[ndx] = '\0'; // terminate the string
              recvInProgress = false;
              ndx = 0;
              newData = true;
            }
          }
      
          else if (rc == startMarker)
          {
            recvInProgress = true;
          }
        }
      }
      
      void showNewData()
      {
        if (newData == true)
        {
          mySensorRawSoc = receivedChars;
          mySensorStrSoc = mySensorRawSoc.substring(2, 6);
      
          mySensorRaw = receivedChars;
          mySensorStr = mySensorRaw.substring(2);
      
          switch (receivedChars[0])
          {
      
            case '1':
              gw.send(info.set(mySensorStr.c_str()));
              break;
            case '2':
              gw.send(soc.set(mySensorStrSoc.c_str()));
              break;
            case '3':
              gw.send(state.set(mySensorStr.c_str()));
              break;
            case '4':
              gw.send(error.set(mySensorStr.c_str()));
              break;
          }
      
          /*Serial.print ("type: ");
          Serial.println (receivedChars[0]);
          Serial.print ("Msg: ");
          Serial.println (receivedChars[2]);
          Serial.println (mySensorRaw);
          Serial.println (mySensorStr);
          */
          mySensorRawSoc = "";
          mySensorStrSoc = "";
          mySensorRaw = "";
          mySensorStr = "";
      
          newData = false;
        }
      }
      
      
      posted in Troubleshooting
      Andreas Henriksson
      Andreas Henriksson