This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
arduinohouse [2023/05/17 06:37] admin created |
arduinohouse [2023/05/17 09:28] (current) admin |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | you need the process that monitors your process to be the process' | ||
+ | |||
+ | until myserver; do | ||
+ | echo " | ||
+ | sleep 1 | ||
+ | done | ||
+ | |||
+ | The above piece of bash code runs myserver in an until loop. The first line starts myserver and waits for it to end. When it ends, until checks its exit status. If the exit status is 0, it means it ended gracefully (which means you asked it to shut down somehow, and it did so successfully). In that case we don't want to restart it (we just asked it to shut down!). If the exit status is not 0, until will run the loop body, which emits an error message on STDERR and restarts the loop (back to line 1) after 1 second. | ||
+ | |||
+ | Why do we wait a second? Because if something' | ||
+ | |||
+ | Now all you need to do is start this bash script (asynchronously, | ||
+ | |||
+ | crontab -e | ||
+ | |||
+ | Then add a rule to start your monitor script: | ||
+ | |||
+ | @reboot / | ||
+ | |||
+ | Alternatively; | ||
+ | |||
+ | ----------------------------------------------------------------------------------------- | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | ------------------------------------------------------------------------------------------------------ | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | |||
+ | |||
+ | https:// | ||
+ | |||
+ | int myInts[6]; | ||
+ | int myPins[] = {2, 4, 8, 3, 6}; | ||
+ | int mySensVals[5] = {2, 4, -8, 3, 2}; | ||
+ | char message[6] = " | ||
+ | | ||
+ | -------------------------------------------------------------------- | ||
+ | |||
+ | https:// | ||
+ | |||
+ | Arrays filling | ||
+ | ------------------------------------------------------------------------------------------ | ||
+ | |||
+ | |||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | |||
+ | https:// | ||
+ | currentcost and mqtt rtl433 | ||
+ | | ||
+ | | ||
+ | |||
+ | --------------------------------------------------------------------------------------------------------------- | ||
https:// | https:// | ||
Looking at the loop() in the ask_transmitter example | Looking at the loop() in the ask_transmitter example | ||
Line 72: | Line 150: | ||
===================================================================================================== | ===================================================================================================== | ||
+ | -------------------------------------------------------------------------------------------------------- | ||
+ | // ask_transmitter.pde | ||
+ | // -*- mode: C++ -*- | ||
+ | // Simple example of how to use RadioHead to transmit messages | ||
+ | // with a simple ASK transmitter in a very simple way. | ||
+ | // Implements a simplex (one-way) transmitter with an TX-C1 module | ||
+ | #include < | ||
+ | #include < | ||
- | <file c foo.c>/ | + | RH_ASK driver; |
- | * flashLED.c | + | |
- | * | + | |
- | | + | |
- | | + | |
- | * | + | |
- | */ | + | |
- | #include < | ||
- | #include < | ||
- | // This program will turn the LEDs on for 100ms, | + | struct dataStruct{ |
- | // then off for 200ms, endlessly. | + | float press_norm ; |
+ | float press_hg; | ||
+ | float temp; | ||
+ | unsigned long counter; | ||
+ | |||
+ | }myData; | ||
- | int main(void) | + | byte tx_buf[sizeof(myData)] = {0}; |
+ | |||
+ | void setup() | ||
{ | { | ||
- | // Set Port B pins for 3 and 4 as outputs | + | Serial.begin(9600); |
- | // PORTB bit 3 = physical pin #2 on the ATTINY45 | + | if (!driver.init()) |
- | // PORTB bit 4 = physical pin #3 on the ATTINY45 | + | |
+ | |||
+ | | ||
+ | myData.press_hg=0.59; | ||
+ | myData.temp=22.394; | ||
+ | |||
+ | } | ||
- | DDRB = 0x18; // In binary this is 0001 1000 (note that is bit 3 and 4) | + | void loop() |
- | // AVR-GCC also would accept 0b00011000, by the way. | + | { |
+ | |||
- | | + | |
- | for ( ; 1==1 ; ) // loop while 1 equals 1 | + | |
- | | + | |
- | // Set Port B pins for 3 and 4 as HIGH (i.e. turn the LEDs on) | + | |
- | PORTB = 0x18; // If we wanted only PB4 on, it'd be PORTB=0x10 | + | |
- | // Use a function (defined in delay.h) to pause 100 milliseconds | + | driver.send((uint8_t *)tx_buf, zize); |
- | _delay_ms(100); | + | |
- | | + | // driver.send((uint8_t *)msg, strlen(msg)); |
- | | + | driver.waitPacketSent(); |
+ | | ||
+ | delay(2000); | ||
+ | } | ||
+ | ---------------------------------------------------------------------------------------------------------- | ||
+ | |||
+ | <file js nestedstruct >/* | ||
+ | |||
+ | var 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: | ||
+ | |||
+ | { | ||
+ | " | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | 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: | ||
+ | |||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ------------------------------------------------------------------------------------------------------------ | ||
+ | |||
+ | Sorry to nitpick but Json object is not a thing. | ||
+ | JSON is a string representation of a JavaScript object. | ||
+ | |||
+ | Ok, enough of that : | ||
+ | |||
+ | Use a function node, loop through the elements & add properties to an object... | ||
- | | + | var arr = msg.payload; |
- | | + | var rv = {};// create |
+ | for (var i = 0; i < arr.length; ++i) { | ||
+ | | ||
+ | rv[key] = arr[i]; // set value in New object | ||
} | } | ||
+ | msg.payload = nv; | ||
+ | return msg; | ||
- | return | + | There are other, more modern/ |
+ | |||
+ | Disclaimer: the above code is untested/ | ||
+ | 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 = [" | ||
+ | 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 | ||
+ | |||
+ | 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:// | ||
+ | |||
+ | nodered publish json : video3 | ||
+ | |||
+ | --------------------------------------------------------------------------------------------------------- | ||
+ | |||
+ | https:// | ||
+ | |||
+ | Try using below code inside a function node. Of course you want to replace the data otherwise the API will not authenticate. | ||
+ | |||
+ | The HTTP request node should have the Method field configured as "-set by msg.method-" | ||
+ | |||
+ | msg.headers = {} | ||
+ | msg.method = " | ||
+ | msg.url = " | ||
+ | msg.headers[" | ||
+ | |||
+ | |||
+ | msg.payload = { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | " | ||
+ | }; | ||
+ | |||
+ | ---------------------------------------------------------------------------------------------------- | ||
+ | https:// | ||
+ | |||
+ | Example | ||
+ | |||
+ | The following example illustrates this − | ||
+ | |||
+ | struct student{ | ||
+ | | ||
+ | int age; | ||
+ | int roll_no; | ||
+ | }; | ||
+ | |||
+ | void setup() { | ||
+ | // put your setup code here, to run once: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | |||
+ | A.age = 27; | ||
+ | | ||
} | } | ||
- | </file> | + | |
+ | void loop() { | ||
+ | // put your main code here, to run repeatedly: | ||
+ | } | ||
+ | |||
+ | |||
+ | ------------------------------------------------------------------------------------------------- | ||
+ | https:// | ||
+ | |||
+ | Looking at the loop() in the ask_transmitter example | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | const char *msg = " | ||
+ | |||
+ | driver.send((uint8_t *)msg, strlen(msg)); | ||
+ | driver.waitPacketSent(); | ||
+ | delay(200); | ||
+ | } | ||
+ | |||
+ | There is a send() method; you can find that in C: | ||
+ | |||
+ | bool RH_ASK:: | ||
+ | |||
+ | and ' | ||
+ | |||
+ | The line can also be found (in a slightly different form) in C: | ||
+ | |||
+ | 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.send((uint8_t*)& | ||
+ | |||
+ | Because waterPressure is not an array, you need the & in front of it to get the address of the variable | ||
+ | |||
+ | ----------------------------------------------------------------------------------------- | ||
+ | arduino string example | ||
+ | |||
+ | https:// | ||
+ | |||
+ | --------------------------------------------------------------------------------------------------- | ||
+ |