Changes

280 bytes removed ,  15:05, 10 April 2023
m
Changed the html tags of the provided custom script code from <code> to <pre> for better readability.
Line 19: Line 19:  
Navigate to it, then create a new sh file using the “vi” command followed by the filename of your choice. (Don’t forget to add the .sh extension at the end).
 
Navigate to it, then create a new sh file using the “vi” command followed by the filename of your choice. (Don’t forget to add the .sh extension at the end).
   −
<code>vi 1wire_script.sh</code>
+
<pre>vi 1wire_script.sh</pre>
      Line 26: Line 26:  
Copy the provided code and paste it into your script file.
 
Copy the provided code and paste it into your script file.
   −
<code>#!/bin/sh</code>
+
<pre>#!/bin/sh
   −
<code># Capture output of ubus call ioman.gpio.onewire status command</code>
+
# Capture output of ubus call ioman.gpio.onewire status command
   −
<code>status=$(ubus call ioman.gpio.onewire status)</code>
+
status=$(ubus call ioman.gpio.onewire status)
   −
<code># Parse value of "value" parameters</code>
+
# Parse value of "value" parameters
 +
value=$(echo "$status" | grep -o '"value":[^,}]*' | sed 's/.*: *"\?\([^,"}]*\)"\?/\1/')
   −
<code>value=$(echo "$status" | grep -o '"value":[^,}]*' | sed 's/.*: *"\?\([^,"}]*\)"\?/\1/')</code>
+
if [ "$value" -eq 0 ]; then
   −
<code>if [ "$value" -eq 0 ]; then</code>
+
  ubus call ioman.gpio.onewire update '{"value":"1"}'
   −
<code>  ubus call ioman.gpio.onewire update '{"value":"1"}'</code>
+
fi
   −
<code>fi</code>
+
BASE_DIR="/sys/bus/w1/devices/"
   −
<code>BASE_DIR="/sys/bus/w1/devices/"</code>
+
do
 +
# Iterate through each sensor directory
 +
for sensor_dir in $BASE_DIR/28-*; do
   −
<code>do</code>
+
  sensor_id=$(basename $sensor_dir)
   −
<code># Iterate through each sensor directory</code>
+
  # Read temperature data from device file and filter it to display up to 2 decimal points
   −
<code>for sensor_dir in $BASE_DIR/28-*; do</code>
+
  temp_raw=$(cat $sensor_dir/w1_slave | grep "t=" | awk -F"=" '{print $2}')
   −
<code>  sensor_id=$(basename $sensor_dir)</code>
+
  temp=$(echo "$temp_raw" | awk '{printf "%.2f", $1/1000}')
   −
<code>  # Read temperature data from device file and filter it to display up to 2 decimal points</code>
+
  # Define HTTP server URL and payload data
   −
<code>  temp_raw=$(cat $sensor_dir/w1_slave | grep "t=" | awk -F"=" '{print $2}')</code>
+
  SERVER_URL="192.168.2.227"
   −
<code>  temp=$(echo "$temp_raw" | awk '{printf "%.2f", $1/1000}')</code>
+
  PAYLOAD="{\"SensorID\": \"$sensor_id\", \"temperature\": $temp}"
   −
<code>  # Define HTTP server URL and payload data</code>
+
  # Send temperature data to server using cURL
   −
<code>  SERVER_URL="192.168.2.227"</code>
+
  curl -X POST -H "Content-Type: application/json" -d "$PAYLOAD" $SERVER_URL
   −
<code>  PAYLOAD="{\"SensorID\": \"$sensor_id\", \"temperature\": $temp}"</code>
+
done</pre>
 
  −
<code>  # Send temperature data to server using cURL</code>
  −
 
  −
<code>  curl -X POST -H "Content-Type: application/json" -d "$PAYLOAD" $SERVER_URL</code>
  −
 
  −
<code>done</code>
      
''To use this script, replace the ‘DEVICE_FILE’, ‘SERVER_URL’, and ‘PAYLOAD’ variables with the appropriate values for your system and HTTP server.''
 
''To use this script, replace the ‘DEVICE_FILE’, ‘SERVER_URL’, and ‘PAYLOAD’ variables with the appropriate values for your system and HTTP server.''

Navigation menu