Skip to content

Instantly share code, notes, and snippets.

@cameroncros
Last active May 30, 2022 13:33
Show Gist options
  • Save cameroncros/1254d271318896e1243cb0778a27e1af to your computer and use it in GitHub Desktop.
Save cameroncros/1254d271318896e1243cb0778a27e1af to your computer and use it in GitHub Desktop.
#-
- Example of I2C driver written in Berry
-
- Support for DockerPiSensorHub device found in M5Stack
-#
class DockerPiSensorHub : Driver
var wire #- if wire == nil then the module is not initialized -#
var temp1
var temp2
var temp3
var light
var humid
var press
var motio
def init()
self.wire = tasmota.wire_scan(0x17, 0)
if self.wire
print("I2C: DockerPi SensorHub detected on bus "+str(self.wire.bus))
end
end
#- trigger a read every second -#
def every_second()
if !self.wire return nil end #- exit if not initialized -#
self.temp1 = self.wire.read(0x17, 0x01, 1)
self.light = self.wire.read(0x17, 0x02, 2)
self.temp2 = self.wire.read(0x17, 0x05, 1)
self.humid = self.wire.read(0x17, 0x06, 1)
self.temp3 = self.wire.read(0x17, 0x08, 1)
var pd1 = self.wire.read(0x17, 0x09, 1)
var pd2 = self.wire.read(0x17, 0x0a, 1)
var pd3 = self.wire.read(0x17, 0x0b, 1)
self.press = (pd3 << 16) | (pd2 << 8) | pd1
self.motio = self.wire.read(0x17, 0x0D, 1)
end
#- display sensor value in the web UI -#
def web_sensor()
if !self.wire return nil end #- exit if not initialized -#
import string
var msg = string.format(
"{s}DockerPiSensorHub temp1{m}%i C{e}"..
"{s}DockerPiSensorHub temp2{m}%i C{e}"..
"{s}DockerPiSensorHub temp3{m}%i C{e}"..
"{s}DockerPiSensorHub light{m}%i Lx{e}"..
"{s}DockerPiSensorHub humid{m}%i %%{e}"..
"{s}DockerPiSensorHub press{m}%0.2f hPa{e}"..
"{s}DockerPiSensorHub motio{m}%i{e}",
self.temp1,
self.temp2,
self.temp3,
self.light,
self.humid,
self.press/100,
self.motio
)
tasmota.web_send_decimal(msg)
end
#- add sensor value to teleperiod -#
def json_append()
if !self.wire return nil end #- exit if not initialized -#
import string
var msg = string.format(",\"SensorHub Board\":{"..
"\"Temperature\":%i,"..
"\"Light\":%i,"..
"\"Humidity\":%i,"..
"\"Motion\":%i"..
"}",
self.temp1,
self.light,
self.humid,
self.motio
)
tasmota.response_append(msg)
msg = string.format(",\"SensorHub BMP280\":{"..
"\"Temperature\":%i,"..
"\"Pressure\":%f"..
"}, \"PressureUnit\": \"hPa\"",
self.temp3,
self.press/100
)
tasmota.response_append(msg)
msg = string.format(",\"SensorHub Ext\":{\"Temperature\":%i}", self.temp2)
tasmota.response_append(msg)
end
end
sensorhub = DockerPiSensorHub()
tasmota.add_driver(sensorhub)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment