1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

#!/bin/bash export SSL_CERT_DIR = /etc/ssl/certs # InitialState Configuration: INITIALSTATE_ACCESS_KEY = "YOUR ACCESS KEY" INITIALSTATE_BUCKET_KEY = "YOUR BUCKET KEY" register_names =( "KWH" "V1N" "V2N" "V3N" "I1" "I2" "I3" "FREQ" "KW_TOTAL" "KVA_TOTAL" ) array_len = ${# register_names[@] } logger 'Reading Registers from Selec MFM383C over RS485/Modbus' echo 'Reading Registers from Selec MFM383C over RS485/Modbus' echo 'Baud 9600 | Data bits 8 | Parity bits N | Stop bits 1 | Port /dev/ttyUSB0' echo 'Slave ID 1 | Response timeone 1 second | Byte timeout 1 second' echo '--------------------------------------------------------------------------' echo '' # ------ Initial State ------------------------------------------------------------- # Prepare cURL HTTP PUT data DATA_JSON = "[" for (( i = 0; i< ${ array_len } ; i++ )) ; do # Send two Ping request packets and 4 seconds timeout register_value = $( /root/mfm383c.out -b 9600 -r ${ register_names[$i] }) return_value = $? case $return_value in 0 ) #Register read successfully logger "Selec MFM383C register read successfully ${register_names[$i]} = $register_value" DATA_JSON = "$DATA_JSON"$'

'" { \"key\" : \"Electricity_${register_names[$i]}\"," DATA_JSON = "$DATA_JSON"$'

'" \"value\" : \"$register_value\"" if [ $(( i )) = $(( array_len-1 )) ] ; then DATA_JSON = "$DATA_JSON"$'

'" }" else DATA_JSON = "$DATA_JSON"$'

'" }," fi ;; 1 ) #Reading register failed: Incorrect command line parameters logger "Selec MFM383C register read error: Incorrect command line parameters" ;; 2 ) #Reading register failed: Modbus library error logger "Selec MFM383C register read error: Modbus library error" ;; 3 ) #Reading register failed: Modbus serial error logger "Selec MFM383C register read error: Modbus serial error" ;; * ) #Reading register failed logger "Selec MFM383C register read error" ;; esac done DATA_JSON = "$DATA_JSON"$'

'" ]" echo $DATA_JSON # execute cURL curl --max-time 5 \ --include \ --request POST \ --header "Content-Type: application/json" \ --header "X-IS-AccessKey: $INITIALSTATE_ACCESS_KEY" \ --header "X-IS-BucketKey: $INITIALSTATE_BUCKET_KEY" \ --header "Accept-Version: 0.0.4" \ --data-binary "$DATA_JSON" \ 'https://groker.initialstate.com/api/events'