Advertisement
kk003

Temp room control with raspberry pi

Jan 5th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # by: kk003
  4. # Rig name: Your rig's name here (if you want)
  5.  
  6. # What's this about?:
  7. # This script is for rigs that have a raspberry pi (type 3 in my case) "attached" whith:
  8. # 1. Temp sensor type DS18B20
  9. # 2. Ability to hard reset the rig from rasberry pi
  10. # 3. Ability to hard poweroff/poweron the rig from rasberry pi
  11. # 4. Rasberry pi running ubuntu-mate-16.04.2-desktop-armhf-raspberry-pi
  12.  
  13. # Hardware notes:
  14. # Main the relay SRD-05VDC-SL-C beheivor is different than others
  15. # Read "The reset bit" and "The poweroff bit" down the code for more info
  16.  
  17. # Helpfull documentation:
  18. # http://bernaerts.dyndns.org/linux/75-debian/351-debian-send-telegram-notification
  19. # https://www.ibm.com/developerworks/community/blogs/aixpert/entry/Computer_Room_Temperature_Monitoring_with_a_Raspberry_Pi?lang=en
  20. # https://bitcointalk.org/index.php?topic=1854250.msg22615430#msg22615430
  21.  
  22. # Name script: check_health_system.sh
  23. # Poweroff the rig if room temperature too high
  24.  
  25. # If room temperature threshold is reached the script hard reset and hard poweroff the rig. Then it gets in to a loop state
  26. # which keeps it running "forever" until you manualy kill it (kill -9 number_id_check_health_system.sh)
  27. # You can get the id of this running script like this: ps aux | grep -v grep | grep check_health_system.sh
  28. # You will see two lines with the script name. Kill both using the numbres in the second column once you resolve the temp problem
  29. # Otherwise if (if not in loop) temperature does not drop at next check the rig will be turn on (keep in main that power switch meets both functions, on and off)
  30.  
  31. # Installation:
  32. # Login as root or su -l from your user
  33. # Save the script in root dir and give execution rights like this
  34. # chmod 700 /root/check_health_system.sh
  35. # Run the script from cron every 2 minutes (or whatever yopu want) like this
  36. # */2 * * * * /root/check_health_system.sh >> /root/temp_ambiente.log
  37. # log file's name: /root/temp_ambiente.log
  38.  
  39.  
  40. # Some vars
  41. RIG_NAME="your rig's name here" # I don't use it
  42. IP_RIG="your rig's IP here" # The ip of the rig "attached" to your raspberry pi goes here and must be possible to ping it from the raspi
  43. TEMP_THRESHOLD=47 # MUST be a INTEGER and is the max room temperature you permit. DON'T set it too low or the rig will be powered off
  44. THRESHOLD_REACHED=0 # 0=threshold NOT reached, 1=threshold reached ALERT!!!!. WARNING: If you set this here to 1 your rig will be turn off
  45. LF=$'\n' # New line
  46. NOMBRES_DIR="/root/nombres"
  47.  
  48. # Telegram vars
  49. CHATID="your telegram chat id here" # Your telegram chat id here
  50. APIKEY="your telegram apikey here" # Your telegram api key here
  51. TELEGRAM_TEMP_LOG_FILE="/root/telegram_temp_warning.log"
  52. SEND_TELEGRAM=1 # 1=send telegram, 0=Don't send telegram
  53.  
  54. START_TIME=`date "+%Y-%m-%d %H:%M:%S"`
  55. echo "Start time: " $START_TIME
  56.  
  57. # Me aseguro de que el script no se esta ejecutando. Solo quiero una instancia del script a la vez.
  58. # Si ya se esta ejecutando salgo sin mas
  59. if pidof -x $(basename $0); then
  60. for p in $(pidof -x $(basename $0)); do
  61. if [ $p -ne $$ ]; then
  62. echo "$0 Already running. Exiting..."
  63. echo
  64. exit
  65. fi
  66. done
  67. fi
  68.  
  69. # Clear the file's content if exists and is not empty
  70. if [[ -s $TELEGRAM_TEMP_LOG_FILE ]]; then
  71. > $TELEGRAM_TEMP_LOG_FILE
  72. fi
  73.  
  74. # Get the raspi's hostname
  75. SYSTEM_PI=$(hostname)
  76.  
  77. ls /sys/bus/w1/devices/ | grep 28* > $NOMBRES_DIR
  78. N=$(cat $NOMBRES_DIR | wc -l)
  79. echo "Number of sensors : " $N | tee -a $TELEGRAM_TEMP_LOG_FILE
  80.  
  81. for ((LINEAS=1; LINEAS <= $N ; LINEAS=LINEAS+1))
  82. do
  83. NOMBRE_FILE=`sed -n -e "${LINEAS}p" $NOMBRES_DIR`
  84. STRING_TEMP=$(cat /sys/bus/w1/devices/$NOMBRE_FILE/w1_slave | grep t= | cut -d"=" -f2)
  85. TEMP=$(echo "scale=2; $STRING_TEMP/1000" | bc)
  86. TEMP_INTEGER=$(echo $TEMP | cut -d. -f1)
  87.  
  88. echo -n "Sensor$LINEAS : $NOMBRE_FILE - Temp " | tee -a $TELEGRAM_TEMP_LOG_FILE
  89. if [[ $TEMP_INTEGER -ge $TEMP_THRESHOLD ]]; then
  90. echo -n $TEMP | tee -a $TELEGRAM_TEMP_LOG_FILE
  91. echo -n "ºC" | tee -a $TELEGRAM_TEMP_LOG_FILE
  92. echo -n " ----> threshold : $TEMP_THRESHOLD" | tee -a $TELEGRAM_TEMP_LOG_FILE
  93. echo "ºC" | tee -a $TELEGRAM_TEMP_LOG_FILE
  94. THRESHOLD_REACHED=1 # 1= reached temp limit ALERT!!!!!!
  95. else
  96. echo -n $TEMP | tee -a $TELEGRAM_TEMP_LOG_FILE
  97. echo "ºC" | tee -a $TELEGRAM_TEMP_LOG_FILE
  98. fi
  99. done
  100.  
  101.  
  102. if [[ $THRESHOLD_REACHED -eq 1 ]]; then # 1=reached threshold temp, 0=we are under threshold limit
  103.  
  104. if [[ SEND_TELEGRAM -eq 1 ]]; then
  105. # Send a telegram warning first
  106. PUBLIC_IP=$(curl -s checkip.dyndns.org | sed -e 's/.*Current IP Address: //' -e 's/<.*$//')
  107. WARNING="WARNING from $SYSTEM_PI $LF Warning: room temperature limit reached ($TEMP_THRESHOLD ºC) $LF Ip Raspi: $PUBLIC_IP $LF Attempting to turn off the rig.$LF"
  108. CONTENT=$(cat $TELEGRAM_TEMP_LOG_FILE)
  109. MSG="$WARNING$CONTENT"
  110.  
  111. curl -s -X POST --output /dev/null https://api.telegram.org/bot${APIKEY}/sendMessage -d "text=${MSG}" -d chat_id=${CHATID}
  112. CODE_CURL=$?
  113.  
  114. # Check if the mgs went out ok
  115. if [[ $CODE_CURL -eq 0 ]]; then
  116. echo "Msg send ok!!"
  117. else
  118. echo "Got a non zero exit code from curl : $CODE_CURL . The message probably has not reached its destination"
  119. fi
  120. fi
  121.  
  122. #####
  123. # Doing a reset and then a poweroff did not work for me, so de reset bit is commented
  124. #####
  125. # I do a hard reset first and then a hard poweroff directly. Don't risk to try a soft poweroff or whatever via software
  126. # Reset the rig using relay's channel 2
  127. # It does not work the same with all relays
  128. # With SONGLE SRD-05VDC-SL-C setting "0" in gpio shorts the reset and "1" frees the circuit
  129. # So you must check this before you put it into production
  130. #
  131. # The reset bit
  132. # echo "Reseting the rig..."
  133. # GPIO_RESET=23
  134. # gpio -g mode $GPIO_RESET out
  135. # sleep 1
  136. # Reset the rig
  137. # gpio -g write $GPIO_RESET 0
  138. # sleep 1 # Keep the short circuit for 1 seg
  139. # gpio -g write $GPIO_RESET 1
  140.  
  141. # sleep 1
  142.  
  143. # Poweroff the rig using relay's channel 1
  144. # It does not work the same with all relays
  145. # With SONGLE SRD-05VDC-SL-C setting "0" in gpio shorts the poweroff and "1" frees the circuit.
  146. # So you must check this before you put it into production
  147. #
  148. # The poweroff bit
  149. echo "Powering off the rig..."
  150. GPIO_POWER=24
  151. gpio -g mode $GPIO_POWER out
  152. sleep 2
  153. # Poweroff the rig according to the previous state (assume it is powered on)
  154. gpio -g write $GPIO_POWER 0
  155. sleep 1 # Keep the short circuit for 1 seg
  156. gpio -g write $GPIO_POWER 1
  157.  
  158.  
  159. #######
  160. # **** VERY IMPORTANT
  161. #######
  162. #
  163. # I keep the script in a infinite loop at this point because if the temp does not drop
  164. # the poweroff bit will startup the rig again, so this script will need a manual kill
  165. # and cron will automatically run it again
  166. #
  167. sleep 120 # I stop here for 2 minutes before enter the loop
  168. TELEGRAM_PING_WARNING=0 #0=I did not send a telegram warning notifying the rig is responding to ping (it should't), 1=The warning has been sent
  169. while true # It's allways true!!!
  170. do
  171. # For testing I do a ping just to make sure the rig is offline and I can see it in log file later
  172. ping -n -c 3 -i 2 -W 3 $IP_RIG
  173. if [[ $? -eq 0 ]]; then
  174. echo "WARNING: Rig $RIG_NAME is responding to ping. It should't. The rig's state should be poweroff"
  175.  
  176. if [[ $TELEGRAM_PING_WARNING -eq 0 ]]; then # I telegram the warning only once
  177.  
  178. if [[ SEND_TELEGRAM -eq 1 ]]; then
  179. WARNING="WARNING from $SYSTEM_PI $LF WARNING: room temperature limit reached ($TEMP_THRESHOLD ºC). $LF The rig responds to ping and should not. $LF The rig's
  180. state should be poweroff. $LF This is the last warning."
  181. MSG="$WARNING"
  182.  
  183. curl -s -X POST --output /dev/null https://api.telegram.org/bot${APIKEY}/sendMessage -d "text=${MSG}" -d chat_id=${CHATID}
  184. CODE_CURL=$?
  185.  
  186. # Check if the mgs went out ok
  187. if [[ $CODE_CURL -eq 0 ]]; then
  188. echo "Msg send ok!!"
  189. TELEGRAM_PING_WARNING=1
  190. else
  191. echo "Got a non zero exit code from curl : $CODE_CURL . The message probably has not reached its destination"
  192. fi
  193. fi
  194. fi
  195. else
  196. echo "OK: rig $RIG_NAME is NOT responding to ping. Its current state should be poweroff"
  197. fi
  198.  
  199. CURRENT_TIME=`date "+%Y-%m-%d %H:%M:%S"`
  200. echo "Current time: " $CURRENT_TIME
  201. echo "*"
  202. sleep 120 # 2 minutes delay until the loop starts again
  203.  
  204. done
  205.  
  206.  
  207.  
  208. else
  209.  
  210. echo -n "Ok, temp is under threshold limit ($TEMP_THRESHOLD"
  211. echo "ºC)"
  212. fi
  213.  
  214. END_TIME=`date "+%Y-%m-%d %H:%M:%S"`
  215. echo "End time: " $END_TIME
  216. echo "****"
  217. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement