The last version of this post is here.
The text bellow is obsolete and stays here only for historical purposes.
The third post, concerning this subject, consists only of a description of php script.
It does what it is supposed to do but I am convinced that there is a better (and elegant) solution to complete the task.
I choose to use an independent php script (that means that it runs by a command line and not under the web server control) similarly with a pearl or shell script.
In fact it is a combination of two different php scripts because I need to sent data to two IoT platforms and, for this reason, I actually use two different methods stuck together in one script (no classes).
I am not happy with that solution so I made to myself the promise to change that code.
Here is that code.
You have to take care to sign-up for an account either with Thingspeak or Exosite or both and put the keys in the rightful place as says the comments within code (line 24 for Thingspeak key and line 144 for Exosite cik).
Comments about the code:
Line 1: #! /bin/php
So called shebang (#!) followed by the absolute path of php interpreter.
As I told before, this is a script that runs under system and not under web server control.
Lines 4 to 48:
There is definition of the function that sends data to Thingspeak platform. It puts the header’s elements in the appropriate position and form.
You have to take care to sign-up for an account either with Thingspeak or Exosite or both and put the keys in the rightful place as says the comments within code (line 24 for Thingspeak key and line 144 for Exosite cik)
Here is the conversion factor to transform the Counts-per-minute (CPM) value, sent by radiation detector, in microSievert-per-hour (μSv/h); you can google for Sievert and figure out about.
The variable defined here ($timestamp is not yet used but might be as a time marker)
Here is defined the local file where the data is kept. You can renounce to use it and keep your data only on IoT platform, where you send it.
The variable $stty_init keeps the system command that initiate the serial port (named /dev/ttyS0 on the Linux system). The next line executes this command.
Here begin the main loop of the script that keeps running for ever if there is no error or no break command or no power failure or no tornado or no tsunami or no end of the word! Each time the loop is executed it opens the serial port (line 79), wait for data to arrive from it (during the nested do-while loop – lines 99 to 113), close the serial port (line 115), for each six consecutive values calculate the sum of them (line 118), because the radiation detector sent a value each 10 seconds, to obtain the CPM, open the local file (line 127), send data to local file together with the Date-Time information (line 128), close the local file (line 134), send some message to local console (from where the script was launched) – you can comment the line 136 -.
The variable $thingspeak_data keeps, as array, the values of CPM (in $sum) and Radiation Dose (in $SV).
Here is called the function that sends the data to Thingspeak platform.
That’s a tricky one: here is the system command that sends the data to Exosite platform (see line 57) but here is used another technique: the whole command (from nc … until “) is framed between ticks (backwards single-quotes: `…`) that is usually found on the upper left key on the US keyboards, under Esc and at left of 1. The php interpreter execute (or try to) whatever is found between ticks.
If something goes wrong then the script is brought to an end with some message.
That’s never reached!
Here is another page
where this can be seen.