Read string from serial port and send it to gateway



  • Hello

    I have play with my sensors a lot and it works perfect (only try standard sensors).
    But now i want to create a sensor that read/write on the serial port (pin 0 and 1) and send it to the gateway
    My C skills are very low but i try to learn 🙂

    This is my sketch:

    #include <SPI.h>
    #include <MySensor.h>  
    
    
    #define CHILD_ID 0
    String readString;
    
    MySensor gw;
    MyMessage msg(CHILD_ID,V_VAR1);
    
    void setup()  
    { 
      gw.begin();
      Serial.begin(115200);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("test", "1.0");
    
      // Register all sensors to gateway (they will be created as child devices)
      gw.present(CHILD_ID, S_CUSTOM);
    }
    
    void loop()   
    {
    	
      while (Serial.available()) {
        delay(3);  //delay to allow buffer to fill 
        if (Serial.available() >0) {
          char c = Serial.read();  //gets one byte from serial buffer
          readString += c; //makes the string readString
        } 
      }
    
      if (readString.length() >0) {
          Serial.println(readString); //see what was received
    
    	  gw.send(msg.set(readString));
    	  readString="";
        
      } 
    }
    

    The string from serial port looks like this: "4. ERO 11421"

    But when i verify the sketch in codebender i get this error:

    Test_sensor.ino:69:16: error: no matching member function for call to 'set'
    gw.send(msg.set(readString));
    ~~^

    Any idea how to fix this

    Thanks Andreas


  • Hero Member

    According to:

    https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/MyMessage.cpp

    There is no set method that accepts String type. You could use this one instead:

    MyMessage& MyMessage::set(const char* value)

    Greetings,
    Tim



  • Thanks
    Bu ti don't know how to fix this in my sketch 😞 , can you please point me to the right direction ??

    Andreas H


  • Hero Member

    Hmmm, I'm out, when it comes to pure C, so I'm not sure if I'm really able to help you.

    The first question that comes to my mind, when I read your sketch is: when does the loop end, that reads the characters?

    If the string you receive ends for example with a newline, you could use something like: https://www.arduino.cc/en/Serial/ReadBytesUntil
    where buffer is char[] and could be passed to msg.set().

    Please keep in mind that the message content must not exceed 25 bytes.



  • Opps . i missed that in this sketch 😞

    But the main problem is howto send a string och many chars 😞



  • @Adreas Henriksson
    I think that if you use String, you should call c_str() method to get char *.
    Your have then to change the line with : gw.send(msg.set(readString.c_str()));
    Hope this works.



  • Hello

    I dont get any error when i compile, so i will make a test later today.

    Thanks
    Andreas H



  • This did the trick.
    gw.send(msg.set(readString.c_str()));
    Thanks for this 🙂

    Andreas H



  • @Andreas-Henriksson
    Hello. I need something like this ...
    Can you show your functional version?
    Thanks...



  • 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;
      }
    }
    
    


  • OK. Thanks. I try...


Log in to reply
 

Suggested Topics

  • 3
  • 2
  • 2
  • 15
  • 3
  • 10

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts