This is an old revision of the document!
https://forum.arduino.cc/t/arduino-433-mhz-rf-sending-numbers-instead-of-strings/619707/4 Looking at the loop() in the ask_transmitter example
void loop() {
const char *msg = "hello";
driver.send((uint8_t *)msg, strlen(msg)); driver.waitPacketSent(); delay(200);
}
There is a send() method; you can find that in C:\Users\yourUserName\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp and the first line is important.
bool RH_ASK::send(const uint8_t* data, uint8_t len)
and 'states' that the send method expects a pointer to an uint8_t (aka byte) variable and a length.
The line can also be found (in a slightly different form) in C:\Users\yourUserName\Documents\Arduino\libraries\RadioHead\RH_ASK.h (that's the file that you include at the top of the sketch).
So if you now go back to the example code, you will see (uint8_t *)msg ; this is a so-called cast that tells the compiler to treat the char array (msg) as a byte array.
So now you know that you actually need to send bytes and not characters etc. and how that's achieved.
To send an int value, you can cast the address of the variable (the pointer) to an pointer to a byte. An int occupies a number of bytes; you can find that with the sizeof operator.
That would look like
int waterPressure = 1234; drive.send1);
Because waterPressure is not an array, you need the & in front of it to get the address of the variable
iminaries
JavaScript has only one data type which can contain multiple values: Object. An Array is a special form of object.
(Plain) Objects have the form
{key: value, key: value, …}
Arrays have the form
[value, value, …]
Both arrays and objects expose a key → value structure. Keys in an array must be numeric, whereas any string can be used as key in objects. The key-value pairs are also called the “properties”.
Properties can be accessed either using dot notation
const value = obj.someProperty;
or bracket notation, if the property name would not be a valid JavaScript identifier name [spec], or the name is the value of a variable:
the space is not a valid character in identifier names const value = obj[“some Property”]; property name as variable const name = “some Property”; const value = obj[name];
For that reason, array elements can only be accessed using bracket notation:
const value = arr[5]; arr.5 would be a syntax error property name / index as variable const x = 5; const value = arr[x];
/* var names = ["name1","another_name","any_anme_you_wish"];// list of names var hold_obj = {};// `temp object names.forEach ((v,i) => hold_obj[v] = msg.payload[i]) //loop through names and add to object msg.payload = hold_obj; // move back to payload return msg;
What JSON looks like
JSON is a human-readable format for storing and transmitting data. As the name implies, it was originally developed for JavaScript, but can be used in any language and is very popular in web applications. The basic structure is built from one or more keys and values:
{
"key": value
}
You’ll often see a collection of key:value pairs enclosed in brackets described as a JSON object. While the key is any string, the value can be a string, number, array, additional object, or the literals, false, true and null. For example, the following is valid JSON:
{
"key": "String", "Number": 1, "array": [1,2,3], "nested": { "literals": true }
}
Sorry to nitpick but Json object is not a thing. JSON is a string representation of a JavaScript object.
Ok, enough of that :slight_smile:
Use a function node, loop through the elements & add properties to an object…
var arr = msg.payload; // get the array var rv = {};// create a new empty object for (var i = 0; i < arr.length; ++i) { let key = "Val"+(i+1); //built a key rv[key] = arr[i]; // set value in New object } msg.payload = nv; return msg;
There are other, more modern/compact ways to achieve this but I kept it simple for clarity.
Disclaimer: the above code is untested/off the top of my head. :innocent: Solution jackie7 May '21
Thank you for the clarification! I'm able to parse the array and get the values but I want to assign names to them, each value has a different name. how can I change their names from Val1 Val2…? Steve-Mcl May '21
each value has a different name. how can I change their names from Val1 Val2.
unless you tell me what they are I cannot really help. an array is just a group of values.
If you know which element is which item & they are always in the same place & there are always a fixed number of elements in the array, you can simply set them manually in the object.
However, there may be a better solution - we should look at that first.
Where does this array come from? can you show me how this array is generated? E1cid jackie7 May '21
You would need to list the names
var names = [“name1”,“another_name”,“any_anme_you_wish”]; list of names var hold_obj = {}; `temp object names.forEach ((v,i) ⇒ hold_obj[v] = msg.payload[i]) loop through names and add to object msg.payload = hold_obj; move back to payload return msg;
assuming the array is in msg.payload.
JSON doesn’t have to have only key:value pairs; the specification allows to any value to be passed without a key. However, almost all of the JSON objects that you see will contain key:value pairs.
https://stevesnoderedguide.com/node-red-http-request-node-beginners
nodered publish json : video3