def returnData(): global setPoint templateData = { #'title' : 'HELLO!', #'time': timeString, relay17: not GPIO.input(17), relay22: not GPIO.input(22), relay23: not GPIO.input(23), relay27: not GPIO.input(27), temperature: tempSensor.getTemp("C"), setPoint: setPoint } return templateData
f = open(fileName, 'w') f.write('[]') # reset the data f.close() time.sleep(abs(time.time() % -5)) # wait till the next whole 5 seconds starttime = time.time() localCopy = [] # contains data of this session while True: t = time.localtime() t = ':'.join([str(i).zfill(2) for i in [t.tm_hour, t.tm_min, t.tm_sec] ]) # time in hh:mm:ss externalTemp = tempSensor.getTemp() # (water) temperature probe coreTemp = os.popen('/opt/vc/bin/vcgencmd measure_temp').read() coreTemp = coreTemp[coreTemp.index('=') + 1:-3] # raspberry pi core temperature # Gyro Sensors heading, roll, pitch = bno.read_euler() # direction sys, gyro, accel, mag = bno.get_calibration_status() # idk why this x, y, z = bno.read_magnetometer() # magnetic field magField = pow(x**2 + y**2 + z**2, 0.5) # absolute value magField = magField / 100 # 100 microTesla = 1 Gauss x, y, z = bno.read_linear_acceleration() # without gravity
def getDataFragment(): ''' Collects data from the various sensors and returns them as a list Return: data: A list of all the different sensor values ''' global gyroRunning # get the current time t = time.localtime() t = ':'.join([str(i).zfill(2) for i in [t.tm_hour, t.tm_min, t.tm_sec]]) # try to get the data from the temperature probe if it is enabled try: if TEMPSENSOR: externalTemp = round(tempSensor.getTemp(), 2) else: # no temperature probe data externalTemp = -1 except: # some error occurred externalTemp = 'Error' try: # try to get the core temperature of the raspberry pi coreTemp = os.popen('/opt/vc/bin/vcgencmd measure_temp').read() coreTemp = round(float(coreTemp[coreTemp.index('=') + 1:-3]), 2) except: # some error occurred coreTemp = 'Error' # Gyro Sensors try: heading, roll, pitch = bno.read_euler() # remove the initial state to get # orientation relative to its start point heading -= defaultRotation['heading'] roll -= defaultRotation['roll'] pitch -= defaultRotation['pitch'] # round data heading = round(heading, 2) roll = round(roll, 2) pitch = round(pitch, 2) # unused sys, gyro, accel, mag = bno.get_calibration_status() # read magnetic field x, y, z = bno.read_magnetometer() # take the absolute value of the magnetic field and turn it into gauss magField = pow(x**2 + y**2 + z**2, 0.5) magField = round(magField / 100, 3) # 100 microTesla = 1 Gauss # get linear acceleration x, y, z = bno.read_linear_acceleration() # for removing strange acceleration picked up when there is none # btw read_linear_acceleration() takes out gravity already x -= defaultAcc['x'] y -= defaultAcc['y'] z -= defaultAcc['z'] # round data x = round(x, 3) y = round(y, 3) z = round(z, 3) # round the temp too # this is the temperature of the gyro, # which shows internal ROV temperature internalTemp = round(bno.read_temp(), 2) # # all the data will be displayed on an html page so if the # values have too many decimal places it will mess up the html table # except: # exception in getting data heading, roll, pitch = 'Error', 'Error', 'Error' magField = 'Error' x, y, z = 'Error', 'Error', 'Error' internalTemp = 'Error' gyroRunning = False if not gyroRunning: # if the gyro has failed try to reboot it print('Attempting to restart gyro') gyroRunning = True setup() # assemble the data into a list fragment = [ t, externalTemp, coreTemp, internalTemp, heading, roll, pitch, magField, x, y, z ] # print the data if allowed if not silent: print(fragment) # return the data return fragment
''' if __name__ == '__main__': # Add a location for python to search for modules to import from sys import path path.append('/var/www/scripts') # Import tempSensor module located in "/var/www/scripts" try: import tempSensor except ModuleNotFoundError: raise ModuleNotFoundError( "tempSensor.py is missing in /var/www/scripts") # Print header specifying file type print("Content-type: text/html\n\n") # Open and read the html file f = open('temperature.html') txt = f.read() f.close() # Get the temperature using the tempSensor module data = tempSensor.getTemp() # Put the temperature into the html file where the comment "<!-- tag -->" is txt = txt.replace('<!-- tag -->', str(data)) # Print final html print(txt)
def getDataFragment(): global gyroRunning t = time.localtime() t = ':'.join([str(i).zfill(2) for i in [t.tm_hour, t.tm_min, t.tm_sec]]) try: if TEMPSENSOR: externalTemp = round(tempSensor.getTemp(), 2) else: externalTemp = -1 except: externalTemp = 'Error' try: coreTemp = os.popen('/opt/vc/bin/vcgencmd measure_temp').read() coreTemp = round(float(coreTemp[coreTemp.index('=') + 1:-3]), 2) except: coreTemp = 'Error' # Gyro Sensors try: heading, roll, pitch = bno.read_euler() heading -= defaultRotation['heading'] roll -= defaultRotation['roll'] pitch -= defaultRotation['pitch'] heading = round(heading, 2) roll = round(roll, 2) pitch = round(pitch, 2) sys, gyro, accel, mag = bno.get_calibration_status() x, y, z = bno.read_magnetometer() magField = pow(x**2 + y**2 + z**2, 0.5) magField = round(magField / 100, 3) # 100 microTesla = 1 Gauss x, y, z = bno.read_linear_acceleration() x -= defaultAcc['x'] y -= defaultAcc['y'] z -= defaultAcc['z'] x = round(x, 3) y = round(y, 3) z = round(z, 3) internalTemp = round(bno.read_temp(), 2) except: heading, roll, pitch = 'Error', 'Error', 'Error' magField = 'Error' x, y, z = 'Error', 'Error', 'Error' internalTemp = 'Error' gyroRunning = False if not gyroRunning: print('Attempting to restart gyro') gyroRunning = True setup() fragment = [t, externalTemp, coreTemp, internalTemp, \ heading, roll, pitch, \ magField, x, y, z] if not silent: print(fragment) #print(time.ctime()) #time.sleep(pause - ((time.time() - starttime) % pause)) return fragment