stromzaehler gpiod
This commit is contained in:
		
							parent
							
								
									1066ca50ab
								
							
						
					
					
						commit
						594ae6df66
					
				
					 1 changed files with 22 additions and 30 deletions
				
			
		| 
						 | 
					@ -1,35 +1,27 @@
 | 
				
			||||||
#!/bin/bash
 | 
					#!/bin/bash
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# gpiochip number + pin number
 | 
					impulse_per_watthour=2
 | 
				
			||||||
gpio=$(expr 458 + 3)
 | 
					ms_per_hour=3600000000000
 | 
				
			||||||
 | 
					gpio=$(gpiofind SCL1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# prepare
 | 
					while gpiomon --num-events=1 --rising-edge $gpio
 | 
				
			||||||
if ! [[ -e "/sys/class/gpio/gpio$gpio" ]]; then
 | 
					do
 | 
				
			||||||
  echo $gpio > "/sys/class/gpio/export"
 | 
					    last_timestamp=$timestamp
 | 
				
			||||||
fi
 | 
					    timestamp=$(date --utc +%s%N)
 | 
				
			||||||
echo in > "/sys/class/gpio/gpio$gpio/direction"
 | 
					
 | 
				
			||||||
 | 
					    [ -z $last_timestamp ] && continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    delay=$(expr $timestamp - $last_timestamp)
 | 
				
			||||||
 | 
					    impulse_per_hour=$(expr $ms_per_hour / $delay)
 | 
				
			||||||
 | 
					    watt=$(expr $impulse_per_hour / $impulse_per_watthour)
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    echo "$delay $impulse_per_hour: $watt watts"
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
# loop
 | 
					 | 
				
			||||||
previous_state=false
 | 
					 | 
				
			||||||
while sleep 0.001; do
 | 
					 | 
				
			||||||
  state=$(cat "/sys/class/gpio/gpio$gpio/value")
 | 
					 | 
				
			||||||
  if ! [[ "$state" = "$previous_state" ]] && [[ "$state" = 0 ]]
 | 
					 | 
				
			||||||
  then
 | 
					 | 
				
			||||||
    if ! [ -z $previous_impulse ] # skip first iteration
 | 
					 | 
				
			||||||
    then
 | 
					 | 
				
			||||||
      delay=$(expr $(date --utc +%s%N) - $previous_impulse)
 | 
					 | 
				
			||||||
      impulse_per_hour=$(expr 3600000000000 / $delay)
 | 
					 | 
				
			||||||
      watt=$(expr $impulse_per_hour / $(expr 2000 / 1000))
 | 
					 | 
				
			||||||
      echo "$watt watts"
 | 
					 | 
				
			||||||
    curl "https://${influxdb_domain}/api/v2/write?org=${influxdb_org}&bucket=${influxdb_bucket}&precision=ns" ${'\\'}
 | 
					    curl "https://${influxdb_domain}/api/v2/write?org=${influxdb_org}&bucket=${influxdb_bucket}&precision=ns" ${'\\'}
 | 
				
			||||||
      -X POST ${'\\'}
 | 
					      -X POST ${'\\'}
 | 
				
			||||||
      --header "Authorization: Token ${influxdb_token}" ${'\\'}
 | 
					      --header "Authorization: Token ${influxdb_token}" ${'\\'}
 | 
				
			||||||
      --header "Content-Type: text/plain; charset=utf-8" ${'\\'}
 | 
					      --header "Content-Type: text/plain; charset=utf-8" ${'\\'}
 | 
				
			||||||
      --header "Accept: application/json" ${'\\'}
 | 
					      --header "Accept: application/json" ${'\\'}
 | 
				
			||||||
        --data-binary "powermeter,host=${node_name} watts=$watt" ${'\\'}
 | 
					      --data-binary "powermeter,host=${node_name} watts=$watt $timestamp" ${'\\'}
 | 
				
			||||||
      2&> /dev/null &
 | 
					      2&> /dev/null &
 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
    previous_impulse=$(date --utc +%s%N)
 | 
					 | 
				
			||||||
  fi
 | 
					 | 
				
			||||||
  previous_state=$state
 | 
					 | 
				
			||||||
done
 | 
					done
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue