def emitTempData(): while True: temp = 0.0 humidity = 0.0 try: humidity = dhtSensor.humidity temp = dhtSensor.temperature if temp is not None and humidity is not None: if not METRIC_UNITS: #convert to fahrenheit temp = format(((temp * 9.0) / 5.0) + 32.0, ".1f") data = {'temp': temp, 'humidity': humidity} sio.emit('cabinTempHumidity', json.dumps(data)) except Exception as ex: #logs any errors encountered during reading of gps. Also allows program to pick back up if node server connection is lost errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit( 'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection continue time.sleep(2)
def connect(): connectedLog = obdUtils.createLogMessage(INFO, SENSOR_TYPE, 'Connection', 'Established') print(connectedLog) sio.emit('log', json.dumps(connectedLog)) startApp()
def emitImuData(): global i2c global sensor while True: try: xA, yA, zA = sensor.acceleration xL, yL, zL = sensor.linear_acceleration heading, roll, pitch = sensor.euler temp = temperature() xQ, yQ, zQ, wQ = sensor.quaternion sys, gyro, accel, mag = sensor.calibration_status data = { "accelX": xA, "accelY": yA, "accelZ": zA, "linAccelX": xL, "linAccelY": yL, "linAccelZ": zL, "gForce": math.sqrt((xL * xL) + (yL * yL) + (zL * zL)) / GRAVITY, "heading": heading, "roll": roll, "pitch": pitch, "temp": temp, "quatX": xQ, #quatX and quatY seem to be reversed? x and y values will be swapped in javascript "quatY": yQ, "quatZ": zQ, "quatW": wQ, "calSys": sys, "calGyro": gyro, "calAccel": accel, "calMag": mag, } sio.emit('imuData', json.dumps(data)) except Exception as ex: errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit( 'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection i2c = board.I2C() #try to reconnect to BNO055 sensor sensor = adafruit_bno055.BNO055_I2C(i2c) continue time.sleep(POLL_INTERVAL)
def emitTelemetry(): global idleTime while True: try: speedCmd = obd.commands.SPEED # select an OBD command (sensor) response = connection.query( speedCmd) # send the command, and parse the response speed = str(response.value.to("mph").magnitude) rpmCmd = obd.commands.RPM response = connection.query(rpmCmd) rpm = response.value.magnitude throttleCmd = obd.commands.THROTTLE_POS response = connection.query(throttleCmd) throttle = str(response.value.magnitude) runTimeCmd = obd.commands.RUN_TIME response = connection.query(runTimeCmd) runTime = str(response.value) if (float(speed) < .1): idleTime += delay data = { 'speed': speed, 'rpm': rpm, 'throttle': throttle, 'runTime': runTime, 'idleTime': idleTime } sio.emit('data', json.dumps(data)) emitDtcCodes() time.sleep(delay) except Exception as ex: #logs any errors errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit( 'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection continue
def emitAirSensorData(): while True: time.sleep(DELAY) try: aqdata = pm25.read() data = {'pm10': aqdata["pm10 standard"], 'pm25': aqdata["pm25 standard"], 'pm100': aqdata["pm100 standard"], 'pm3um': aqdata["particles 03um"], 'pm5um': aqdata["particles 05um"], 'pm10um': aqdata["particles 10um"], 'pm25um': aqdata["particles 25um"], 'pm50um': aqdata["particles 50um"], 'pm100um': aqdata["particles 100um"]} sio.emit('airQualityData', json.dumps(data)) except Exception as ex: errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit('log', json.dumps(errorLog)) continue
GPIO.add_event_detect(20, GPIO.FALLING, callback=restartApps, bouncetime=2000) def startApp(): while True: pollSysInfo() time.sleep(POLL_INTERVAL) while True: #loop until a connection is made with the server instead of immediately exiting try: sio = socketio.Client() sio.connect('http://localhost:3000') startApp() #temp workaround. Originally not needed. Seems to be issue with nodejs socketio version not sending connect packet to trigger connect() event break except Exception as ex: errorLog = obdUtils.createLogMessage('asdf', 'asdf', type(ex).__name__, ex.args) print(errorLog) numTries += 1 print("buttons app unable to connect to node server, retrying attempt {0}".format(numTries)) time.sleep(RETRY_INTERVAL) continue @sio.event def connect(): connectedLog = obdUtils.createLogMessage(INFO, SENSOR_TYPE, 'Connection', 'Established') print(connectedLog) sio.emit('log', json.dumps(connectedLog))
'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection continue connection = obd.OBD() while True: #loop until a connection is made with the server instead of immediately exiting try: print("OBD connection established!") sio = socketio.Client() sio.connect('http://localhost:3000') emitTelemetry() break except Exception as ex: errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit( 'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection sleep(RETRY_INTERVAL) continue @sio.event def connect(): print("Connected to node server!") emitTelemetry()
def emitGpsData(): while True: time.sleep(DELAY) try: report = gpsd.next() if report['class'] == 'TPV': #time-position-velocity report object data = { 'fixType': GPS_STATUSES[getattr(report, 'status', '')], 'latitude': obdUtils.formatDecimalPlaces(getattr(report, 'lat', 0.0), LATITUDE_DEC_PLACES), 'longitude': obdUtils.formatDecimalPlaces(getattr(report, 'lon', 0.0), LONGITUDE_DEC_PLACES), 'time': getattr( report, 'time', '' ), #Time/date stamp in ISO8601 format, UTC. May have a fractional part of up to .001sec precision. May be absent if the mode is not 2D or 3D. 'altitude': getattr( report, 'alt', 'nan' ), #Altitude, height above ellipsoid, in meters. Probably WGS84. 'speed': obdUtils.formatDecimalPlaces( (getattr(report, 'speed', 0.0) * MPH_MULTIPLIER), SPEED_DEC_PLACES ), #mph converted from meters per second 'climb': getattr( report, 'climb', 'nan' ), #Climb (positive) or sink (negative) rate, meters per second. 'latitudeErr': getattr( report, 'epy', 'nan' ), #Latitude error estimate in meters. Certainty unknown. 'longitudeErr': getattr( report, 'epx', 'nan' ), #Longitude error estimate in meters. Certainty unknown. 'timeErr': getattr( report, 'ept', 'nan' ), #Estimated time stamp error in seconds. Certainty unknown. 'altitudeErr': getattr( report, 'epv', 'nan' ), #Estimated vertical error in meters. Certainty unknown. 'speedErr': obdUtils.formatDecimalPlaces( (getattr(report, 'eps', 0.0) * MPH_MULTIPLIER), SPEED_DEC_PLACES ), #Estimated speed error in meters per second. Certainty unknown. 'climbErr': getattr( report, 'epc', 'nan' ) #Estimated climb error in meters per second. Certainty unknown. } #print(data) sio.emit('gpsData', json.dumps(data)) except Exception as ex: #logs any errors encountered during reading of gps. Also allows program to pick back up if node server connection is lost errorLog = obdUtils.createLogMessage(ERROR, SENSOR_TYPE, type(ex).__name__, ex.args) print(errorLog) sio.emit( 'log', json.dumps(errorLog) ) #will only work if exception is unrelated to node server connection continue