Skip to content

Instantly share code, notes, and snippets.

@mikegreen
Created July 6, 2020 18:12
Show Gist options
  • Save mikegreen/5f51c30e27f78759b28708d63d2655fe to your computer and use it in GitHub Desktop.
Save mikegreen/5f51c30e27f78759b28708d63d2655fe to your computer and use it in GitHub Desktop.
Send ardunio voltage sensor to Stathat
import RPi.GPIO as gpio
import smbus
import time
import sys
import struct
from datetime import datetime
from stathat import StatHat
stathat = StatHat()
bus = smbus.SMBus(1)
address = 0x04
def main():
print(str(datetime.now()) + " Starting up...")
status = False
runTimes = 1
while runTimes > 0:
status = not status
bus.write_byte(address, 1 if status else 0)
time.sleep(3)
arduinoData = (bus.read_i2c_block_data(address, 0, 32)) # read 32 chars
teensyData = ("".join(map(chr, arduinoData)))
voltsStart = teensyData.find("Volts:",0)
voltsEnd = teensyData.find(":",voltsStart) + 1
voltsDataEnd = teensyData.find("|",voltsStart+6)
voltsVal = teensyData[voltsEnd:voltsDataEnd]
print(str(datetime.now()) + " " + "Volts: " + voltsVal)
analogStart = teensyData.find("RawAnalog:",0)
analogEnd = teensyData.find(":",analogStart) + 1
analogDataEnd = teensyData.find("|",analogStart+10)
analogVal = teensyData[analogEnd:analogDataEnd]
print(str(datetime.now()) + " " + "RawAnalog: " + analogVal)
print("Sending to stathat: " + voltsVal)
stathat.ez_post_value('[email protected]', 'rv.volts', voltsVal)
time.sleep(60)
runTimes = (runTimes - 1)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
gpio.cleanup()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment