Problem with Node RED

0

I am connecting an Arduino through MQTT to Node RED. This arduino sends me the following string "Temperature: 28.0". Then I have another plate that gets the humidity and dows it in the form of string "Humidity: 50". When receiving data by Node RED I get the string, but I'm not able to convert them to JSON because I have to deliver dweet.io to display the data, and this program asks me JSON.

Let's see if you can help me out!

    
asked by J. Salguero 05.06.2017 в 01:42
source

1 answer

1

I do not really know much about arduinos, but in nodejs there is a way to do what you want, I have devised a method that returns an object given an array of strings of the form 'abcd: 123'

 function getValue (arrayString) {
        var obj = {};
        arrayString.forEach((string) => {
            var elementIndex = 0
            for(var i = 0; i < string.length; i++) {
                if(string[i] == ':') {
                    elementIndex = i;
                    break;
                }
            }
            str = string.substring(0, elementIndex);
            val = string.substring(elementIndex+1, string.length)
            obj[str] =  parseFloat(val, 10);
        });
        return obj;
    }

    arrayString = [
        'Temperatura:28.0',
        'Humedad:50',
    ]
    
    var obj = getValue(arrayString);
    console.log(obj);
    console.log(JSON.stringify(obj));

If you call getValue with your string array, you should return an object the way you want, if you want it as a string, call JSON.stringify(obj) . I hope my answer will help you

    
answered by 05.06.2017 в 21:37