Read string from serial port and send it to gateway
-
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
-
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 :-(
-
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
-
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; } }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login