New Battery API

This commit is contained in:
2021-01-12 10:57:18 -05:00
parent a102fd4b88
commit dbc3f7c507
+51
View File
@@ -1,8 +1,42 @@
from flask import Flask, json, request
import os
import struct
import smbus
import sys
import time
import json
import sched
import RPi.GPIO as GPIO
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
data = {}
s = sched.scheduler(time.time, time.sleep)
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
app = Flask(__name__)
@app.route('/battery',methods = ['POST','GET'])
def writeJSON():
capacity = readCapacity(bus)
voltage = readVoltage(bus)
if GPIO.input(6):
# Power Adapter plugged in
adapter = "in"
else:
# Power Adapter NOT plugged in
adapter = "out"
data['percentage'] = []
data['percentage'].append({
'level': capacity,
'voltage': voltage
})
data['adapter'] = []
data['adapter'].append({
'status': adapter
})
return data
@app.route('/brightness',methods = ['POST'])
def brightness():
if request.method == 'POST':
@@ -45,3 +79,20 @@ def after_request(response):
if __name__ == '__main__':
app.run()
def readVoltage(bus):
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 /1000/16
return voltage
def readCapacity(bus):
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity