### Install and start emoncms_mqtt service Source: https://github.com/emoncms/emoncms/blob/master/scripts/services/install-service-emoncmsmqtt.md Installs and starts the emoncms_mqtt systemd service by symlinking its unit file, enabling it, starting it, and checking its status. ```bash sudo ln -s /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.service /lib/systemd/system sudo systemctl enable emoncms_mqtt.service sudo systemctl start emoncms_mqtt.service systemctl status emoncms_mqtt.service ``` -------------------------------- ### Run installation script Source: https://github.com/emoncms/emoncms/blob/master/scripts/logger/readme.md Execute the installation script with sudo. After running, a system reboot is necessary if /var/log is mounted in tmpfs. ```bash sudo ./install.sh ``` -------------------------------- ### Install feedwriter service with systemd Source: https://github.com/emoncms/emoncms/blob/master/scripts/services/install-service-feedwriter.md Commands to symlink the service file, enable, start, and check the status of the feedwriter service. ```bash sudo ln -s /var/www/emoncms/scripts/services/feedwriter/feedwriter.service /lib/systemd/system sudo systemctl enable feedwriter.service sudo systemctl start feedwriter.service systemctl status feedwriter.service ``` -------------------------------- ### Install mosquitto-clients package Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/mqtt.md Installs the mosquitto-clients package required for command-line MQTT testing. ```shell sudo apt install -y mosquitto-clients ``` -------------------------------- ### Example URL to test the script Source: https://github.com/emoncms/emoncms/blob/master/docs/design/input-processing.md Example URL to invoke the PHP script with a JSON-like string containing two variables: power and temperature. This should be entered in the browser after setting up the script and database. ```text 02.php?json=power:100,temperature:16.5 ``` -------------------------------- ### Successful import log example Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/import.md Example log output from a successful backup import, showing the sequence of operations and the final success message. This is useful for verifying a successful import. ```text === Emoncms import start === 2019-10-18-08:21:15 Backup module version: cat: /opt/emoncms/modules/backup/backup/module.json: No such file or directory EUID: 1000 Reading /opt/emoncms/modules/backup/config.cfg.... Location of data databases: /var/opt/emoncms Location of emonhub.conf: /etc/emonhub Location of Emoncms: /var/www/emoncms Backup destination: /opt/openenergymonitor/data Backup source path: /opt/openenergymonitor/data/uploads Starting import from /opt/openenergymonitor/data/uploads to /opt/openenergymonitor/data... Image version: emonSD-17Oct19 new image Backup found: emoncms-backup-2019-10-18.tar.gz starting import.. Read MYSQL authentication details from settings.php Decompressing backup.. emoncms.sql emonhub.conf settings.ini phpfina/ phpfina/165119.meta phpfina/165146.dat phpfiwa/ phptimeseries/ Removing compressed backup to save disk space.. Stopping services.. Emoncms MYSQL database import... Import feed meta data.. Restore phpfina and phptimeseries data folders... Import emonhub.conf > /etc/emonhub/emohub.conf OK Restarting emonhub... Restarting feedwriter... 2019-10-18-08:26:56 === Emoncms import complete! === ``` -------------------------------- ### Install Composer dependencies Source: https://github.com/emoncms/emoncms/blob/master/tests/readme.md Command to install PHP dependencies required for running the tests. ```bash php composer.phar install ``` -------------------------------- ### Example URL: 01.php?json=power:100,temperature:16.5 Source: https://github.com/emoncms/emoncms/blob/master/docs/design/input-processing.md Invoke the 01.php script with a JSON-like query string to see the parsed input pairs displayed in the browser. The example sends two variables: power with value 100 and temperature with value 16.5. ```text 01.php?json=power:100,temperature:16.5 ``` -------------------------------- ### Install REDIS on Linux (terminal commands) Source: https://github.com/emoncms/emoncms/blob/master/docs/design/developing-a-new-module.md Commands to install REDIS server and the PHP redis extension on Linux. Run these in the terminal as root (sudo). The commands add the redis.so extension to both Apache and CLI PHP configurations. ```bash sudo apt-get install redis-server sudo pecl install channel://pecl.php.net/dio-0.0.6 redis sudo sh -c 'echo "extension=redis.so" > /etc/php5/apache2/conf.d/20-redis.ini' sudo sh -c 'echo "extension=redis.so" > /etc/php5/cli/conf.d/20-redis.ini' ``` -------------------------------- ### Add menu item to module (feed_menu.php) Source: https://github.com/emoncms/emoncms/blob/master/Theme/menu/readme.md Example of adding a menu item to the level 2 sidebar of the setup menu. The session write check hides the item if the user lacks write access. ```php "Feeds", "href"=>"feed/view", "order"=>2, "icon"=>"format_list_bulleted" ); } ``` -------------------------------- ### MQTT Publish Example Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/postingdata.md Publishes a message to the emon/ base topic. The topic is emon/mynode/power1 and the message is 100. ```mqtt topic: emon/mynode/power1 message: 100 ``` -------------------------------- ### Make installation script executable Source: https://github.com/emoncms/emoncms/blob/master/scripts/logger/readme.md Change the permissions of install.sh to make it executable. Requires sudo privileges. ```bash cd /var/www/html/emoncms/scripts/logger/ && sudo chmod +x install.sh ``` -------------------------------- ### Example output of Emoncms password reset script Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/troubleshooting.md Shows the expected interactive output when running the password reset script, including the auto-generated password example. ```text ======================================= EMONCMS PASSWORD RESET ======================================= Select userid, or press enter for default: Using default user 1 Enter new password, or press enter to auto generate: Auto generated password: 9f7599c8da ``` -------------------------------- ### Example URL for $route properties Source: https://github.com/emoncms/emoncms/blob/master/docs/design/global-variables.md This URL format is used to illustrate how $route properties are set after URL decoding. ```text http://server/controller/action.subaction.format?attribute=blabla ``` -------------------------------- ### Expected output of index.php example Source: https://github.com/emoncms/emoncms/blob/master/docs/design/architecture.md The output displayed in the browser when navigating to http://localhost/framework/feed/list.json, showing the value of the 'q' parameter. ```text q=feed/list.json ``` -------------------------------- ### JSON translation format example Source: https://github.com/emoncms/emoncms/blob/master/scripts/translation/readme.md Shows the key-value structure of a translation JSON file. ```json { "Hello World": "Bonjour le monde", "Welcome to our site": "Bienvenue sur notre site", "User not found": "Utilisateur introuvable", "Save": "Sauvegarder" } ``` -------------------------------- ### Load all inputs at once Source: https://github.com/emoncms/emoncms/blob/master/docs/design/input-processing.md SQL query to load id, name, and processList for all inputs of a user at the start of the request, reducing the number of queries. ```sql SELECT id,name,processList FROM input WHERE userid='$userid' ``` -------------------------------- ### Log info with EmonLogger Source: https://github.com/emoncms/emoncms/blob/master/docs/design/developing-a-new-module.md Log an informational message using the info method. The example shows a PHPTimestore post message. ```php $this->log->info("PHPTimestore:post id=$feedid timestamp=$timestamp value=$value"); ``` -------------------------------- ### Install service-runner systemd service Source: https://github.com/emoncms/emoncms/blob/master/scripts/services/install-service-runner-update.md Install the Python service runner on Raspbian. Skips the redis install if already present. Enables and starts the systemd service, then checks its status. ```bash sudo pip3 install redis sudo ln -s /var/www/emoncms/scripts/services/service-runner/service-runner.service /lib/systemd/system sudo systemctl enable service-runner.service sudo systemctl start service-runner.service systemctl status service-runner.service ``` -------------------------------- ### Step 1: Fetch metadata and open data file Source: https://github.com/emoncms/emoncms/blob/master/docs/timeseries/Fixed-interval.md This step retrieves the start_time and interval from the meta file and opens the .dat file for reading. ```php $meta = $this->get_meta($id); $fh = fopen($this->dir.$id.".dat", 'rb'); ``` -------------------------------- ### PHP script to store inputs (pre-March 2013) Source: https://github.com/emoncms/emoncms/blob/master/docs/design/input-processing.md PHP script that connects to MySQL, parses a JSON-like string from the 'json' GET parameter, sanitizes input names and values, and either updates an existing input or creates a new one. It outputs an HTML table showing whether each input was updated or created. Requires a MySQL database named 'example' and a table 'input'. ```php "; foreach ($datapairs as $datapair) { $datapair = explode(":", $datapair); // sanitize input by ensuring it is of correct format $inputname = preg_replace('/[^\w\s-.]/','',$datapair[0]); $value = floatval($datapair[1]); $time = time(); $datetime = date("Y-n-j H:i:s", $time); // Check if the input already exists by trying to select it: $result = $mysqli->query("SELECT id FROM input WHERE name='$inputname' AND userid='$userid'"); $array = $result->fetch_array(); $id = $array['id']; echo ""; if ($id) { // if it does exist then update the input echo "Update input: "; $mysqli->query("UPDATE input SET time='$datetime', value = '$value' WHERE id = '$id'"); $inputs[] = array($id,$datetime,$value); } else { // Otherwise create a new input echo "Created input: "; $mysqli->query("INSERT INTO input (userid,name,time,value) VALUES ('$userid','$inputname','$datetime','$value')"); } echo "$datapair[0]$datapair[1]"; } echo ""; ?> ``` -------------------------------- ### index.php front controller example Source: https://github.com/emoncms/emoncms/blob/master/docs/design/architecture.md Simple PHP script that echoes the value of the 'q' query parameter, demonstrating how the front controller receives the rewritten URL. ```php controller); print $output; ``` -------------------------------- ### JSON API example requests and responses Source: https://github.com/emoncms/emoncms/blob/master/docs/design/architecture.md Example URLs and their JSON responses for the feed API: create, list, and delete actions. Shows success and error responses. ```json {"success":true,"feedid":1} ``` ```json [{"id":"1","name":"power"},{"id":"2","name":"temperature"}] ``` ```json {"success":true,"message":"feed deleted"} or {"success":false,"message":"feed does not exist"} ``` -------------------------------- ### Example Development Cycle: Convert HTML to use translation functions and extract keys Source: https://github.com/emoncms/emoncms/blob/master/scripts/translation/readme.md This bash script demonstrates the development cycle: converting existing HTML text to use PHP translation functions, then extracting translation keys for each target language. It includes comments showing the before and after states and the extraction commands. ```bash # 1. Convert existing text to use translation functions # Before:

Hello World

Welcome to our application

# After:

# 2. Extract keys for all supported languages php extract_translation_keys.php fr_FR php extract_translation_keys.php de_DE php extract_translation_keys.php es_ES # 3. Edit the JSON files to add translations # Edit: Modules/MyModule/locale/fr_FR.json # Edit: Modules/MyModule/locale/de_DE.json # etc. # 4. Test your translations in the application ``` -------------------------------- ### basic_formula example formula Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/postprocess.md An example formula for the basic_formula post processor. Feed IDs are referenced with 'f' followed by the feed ID number, e.g., f1 for feed id 1. Use the feed selection helper when entering the formula. ```text f1+2*f2-f3/12 ``` -------------------------------- ### Run Unit suite Source: https://github.com/emoncms/emoncms/blob/master/tests/readme.md Runs the Unit test suite, which is fast and requires no infrastructure. ```bash php vendor/bin/phpunit --configuration tests/phpunit.xml ``` -------------------------------- ### Binary search function in phptimeseries engine Source: https://github.com/emoncms/emoncms/blob/master/docs/timeseries/Variable-interval.md Performs a binary search on the timeseries file to find the datapoint closest to a given time. Uses up to 30 iterations, with the midpoint calculated as $start + round(($end-$start)/18)*9. Returns an array with position, time, and value. ```php private function binarysearch($fh,$time,$filesize) { $start = 0; $end = $filesize-9; // 30 here is our max number of itterations // the position should usually be found within // 20 itterations. for ($i=0; $i<30; $i++) { // Get the value in the middle of our range $mid = $start + round(($end-$start)/18)*9; fseek($fh,$mid); $d = fread($fh,9); $array = unpack("x/Itime/fvalue",$d); // If it is the value we want then exit if ($time==$array['time']) return array('pos'=>$mid,'time'=>$array['time'],'value'=>$array['value']); // If the query range is as small as it can be 1 datapoint wide: exit if (($end-$start)==9) return array('pos'=>($mid-9),'time'=>$array['time'],'value'=>$array['value']); // If the time of the last middle of the range is // more than our query time then next itteration is lower half // less than our query time then nest ittereation is higher half if ($time>$array['time']) $start = $mid; else $end = $mid; } } ``` -------------------------------- ### Create and set permissions on Emoncms logfile Source: https://github.com/emoncms/emoncms/wiki/Home Command to create the logfile and set permissions in its new location. Only needs to be run once. Applies to existing emonbase users, not emonPi. ```bash touch /var/log/emoncms.log && chmod 666 /var/log/emoncms.log ``` -------------------------------- ### Example emonhub.log showing valid MQTT posting Source: https://github.com/emoncms/emoncms/blob/master/docs/user_guide/troubleshooting.md This log excerpt shows a valid emonhub log with DEBUG lines for RFM2Pi and MQTT publishing. It is used to verify that the emonhub configuration is correct when inputs are not updating. ```text 2020-02-21 16:02:33,236 INFO MainThread EmonHub emonHub emon-pi variant v3-beta 2020-02-21 16:02:33,236 INFO MainThread Opening hub... 2020-02-21 16:02:33,237 INFO MainThread Logging level set to DEBUG 2020-02-21 16:02:33,237 INFO MainThread Creating EmonHubJeeInterfacer 'RFM2Pi' 2020-02-21 16:02:33,238 DEBUG MainThread Opening serial port: /dev/ttyAMA0 @ 38400 bits/s 2020-02-21 16:02:35,243 INFO MainThread RFM2Pi device firmware version: [RF12demo.14] 2020-02-21 16:02:35,244 INFO MainThread RFM2Pi device current settings: E i5 g210 @ 433 MHz q1 2020-02-21 16:02:35,245 INFO MainThread Setting RFM2Pi calibration: 230V (1p) 2020-02-21 16:02:36,247 DEBUG MainThread Setting RFM2Pi interval: 60 2020-02-21 16:02:36,247 DEBUG MainThread Setting RFM2Pi pubchannels: ['ToEmonCMS'] 2020-02-21 16:02:36,248 DEBUG MainThread Setting RFM2Pi subchannels: ['ToRFM12'] 2020-02-21 16:02:36,249 INFO MainThread Creating EmonHubMqttInterfacer 'MQTT' 2020-02-21 16:02:36,251 DEBUG RFM2Pi acknowledged command: > 1p 2020-02-21 16:02:36,253 DEBUG MainThread Setting MQTT subchannels: ['ToEmonCMS'] 2020-02-21 16:02:36,254 INFO MainThread Setting MQTT node_format_enable: 1 2020-02-21 16:02:36,254 INFO MainThread Setting MQTT nodevar_format_enable: 1 2020-02-21 16:02:36,255 INFO MainThread Setting MQTT nodevar_format_basetopic: emon/ 2020-02-21 16:02:36,256 INFO MainThread Creating EmonHubEmoncmsHTTPInterfacer 'emoncmsorg' 2020-02-21 16:02:36,257 DEBUG MainThread Setting emoncmsorg pubchannels: ['ToRFM12'] 2020-02-21 16:02:36,258 DEBUG MainThread Setting emoncmsorg subchannels: ['ToEmonCMS'] 2020-02-21 16:02:36,258 WARNING MainThread Setting emoncmsorg apikey: obscured 2020-02-21 16:02:36,258 INFO MainThread Setting emoncmsorg url: https://emoncms.org 2020-02-21 16:02:36,259 INFO MainThread Setting emoncmsorg senddata: 0 2020-02-21 16:02:36,259 INFO MainThread Setting emoncmsorg sendstatus: 1 2020-02-21 16:02:36,352 DEBUG RFM2Pi RFM2Pi broadcasting time: 16:02 2020-02-21 16:02:38,456 DEBUG RFM2Pi device settings updated: E i5 g210 @ 433 MHz q1 2020-02-21 16:02:38,559 DEBUG RFM2Pi 7 NEW FRAME : OK 24 164 0 0 0 151 2 27 0 1 0 0 0 (-45) 2020-02-21 16:02:38,561 DEBUG RFM2Pi 7 Timestamp : 1582300958.5596843 2020-02-21 16:02:38,561 DEBUG RFM2Pi 7 From Node : 24 2020-02-21 16:02:38,562 DEBUG RFM2Pi 7 Values : [16.400000000000002, 0, 66.3, 2.7, 1] 2020-02-21 16:02:38,562 DEBUG RFM2Pi 7 RSSI : -45 ```