def __main__(): # initialize oled oledHelper.init(dirName) device = ubidots.UbidotsDevice(token, deviceName) if not oneWire.setupOneWire(str(oneWireGpio)): print "Kernel module could not be inserted. Please reboot and try again." return -1 # get the address of the temperature sensor # it should be the only device connected in this experiment sensorAddress = oneWire.scanOneAddress() # instantiate the temperature sensor object sensor = temperatureSensor.TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." return -1 # check and print the temperature temperature = sensor.readValue() dataPoint = {"temperature": temperature} device.pushDataPoint(dataPoint) # write to oled screen oledHelper.writeMeasurements(temperature)
def __main__(): # check if 1-Wire is setup in the kernel if not oneWire.setupOneWire(str(oneWireGpio)): print( "Kernel module could not be inserted. Please reboot and try again." ) return -1 # get the address of the temperature sensor # it should be the only device connected in this experiment sensorAddress = oneWire.scanOneAddress() # instantiate the temperature sensor object sensor = TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print( "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." ) return -1 # infinite loop - runs main program code continuously while 1: # check and print the temperature value = sensor.readValue() print("T = " + str(value) + " C") time.sleep(pollingInterval)
def __main__(): if not oneWire.setupOneWire(str(oneWireGpio)): print "Kernel module could not be inserted. Please reboot and try again." return -1 # get the address of the temperature sensor # it should be the only device connected in this experiment sensorAddress = oneWire.scanOneAddress() # instantiate the temperature sensor object sensor = temperatureSensor.TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." return -1 # check and print the temperature temperature = sensor.readValue() dataPoint = {"temperature": temperature} # temperature is in deg C # To change to deg F, add following line: temperature = temperature * (9.0 / 5.0) + 32.0 # Post to Twitter twitter.update_status(status="Current Omega2 temperature: " + str(temperature)) + "F"
def __init__(self, sensorAddress: str = None): self.logger = logging.getLogger(__name__) self.configs = Configurator.instance() self.oneWireStatus = oneWire.setupOneWire( self.configs.tempSensorPin) self.lastTemperature = None self.sensorAddress = sensorAddress self.sensor = None
def getTemp(): # check if 1-Wire was setup in the kernel if not oneWire.setupOneWire(str(oneWireGpio)): print "Kernel module could not be inserted. Please reboot and try again." return -1 # get the address of the temp sensor # it should be the only device connected to this experiment sensorAddress = oneWire.scanOneAddress() sensor = TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." return -1 return sensor.readValue()
def __main__(): # check if 1-Wire is setup in the kernel if not oneWire.setupOneWire(str(oneWireGpio)): print( "Kernel module could not be inserted. Please reboot and try again." ) return -1 # get the address of the temperature sensor # it should be the only device connected in this experiment sensorAddress = oneWire.scanOneAddress( ) # NEED TO CHANGE THIS FOR HANDLING MORE THAN ONE SERVICE # instantiate the temperature sensor object sensor = TemperatureSensor() sensor.add_sensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) ok_to_talk = sensor.ask_status(sensorAddress) if not ok_to_talk: print( "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." ) return -1 client = mqtt.Client(c_name) #client.on_message=on_message # attaching function to grab callback client.connect(b_name) client.loop_start() client.subscribe(mqtt_service) # a/b/c - sensor print('connected to {} and subscribed to {}'.format(b_name, mqtt_service)) print('loop started') # infinite loop - runs main program code continuously try: while True: # check and print the temperature value = sensor.read_sensor(sensorAddress) rp = {'sensor': mqtt_service, 'reading': str(value)} rpj = json.dumps(rp) client.publish(mqtt_service, rpj) print("T = " + str(value) + " C") time.sleep(pollingInterval) # should be at least 4-5 seconds except (ValueError, EOFError, KeyboardInterrupt) as e: print('encountered {}'.format(e)) client.loop_stop() client.disconnect() print('shutdown')
value = sensor.readValue() print("T= " + str(value) + " C") #I/O and PWM set up os.system('omega2-ctrl gpiomux set uart1 gpio') os.system('omega2-ctrl gpiomux set pwm0 pwm') LampPin = 1 FoggerPin = 45 StirPin = 15 LampObj = onionGpio.OnionGpio(LampPin) StirObj = onionGpio.OnionGpio(StirPin) FoggerObj = onionGpio.OnionGpio(FoggerPin) #1-Wire Init if not oneWire.setupOneWire(str(oneWireGpio)): print("Kernel mdule could not be inserted. Try again!") sensorAddress = oneWire.scanOneAddress() sensor = TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print("Sensore was not set up correctly.") os.system('onion pwm 0 0 1000') LampObj.setOutputDirection(0) FoggerObj.setOutputDirection(0) StirObj.setOutputDirection(1) foggSet = True
def __main__(): # initialize oled oledHelper.init(dirName) device = ubidots.UbidotsDevice(token, deviceName) if not oneWire.setupOneWire(str(oneWireGpio)): print "Kernel module could not be inserted. Please reboot and try again." return -1 # get the address of the temperature sensor # it should be the only device connected in this experiment sensorAddresses = oneWire.scanAddresses() # See https://ubidots.com/docs/hw/ for data point specs dataPoint = { 'timestamp': int(float(datetime.datetime.today().strftime('%s.%f')) * 1000.0) } i = 1 for sensorAddress in sensorAddresses: sensor = temperatureSensor.TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) if not sensor.ready: print "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." return -1 temperature = sensor.readValue() dataPoint[('temperature-%s' % sensorAddress)] = temperature # oledHelper.writeMeasurements(sensorAddress, temperature, i + 1) i = i + 1 # instantiate the temperature sensor object # sensor = temperatureSensor.TemperatureSensor("oneWire", { "address": sensorAddress, "gpio": oneWireGpio }) # if not sensor.ready: # print "Sensor was not set up correctly. Please make sure that your sensor is firmly connected to the GPIO specified above and try again." # return - # check and print the temperature # temperature = sensor.readValue() # dataPoint = { # "temperature": temperature # } ubidots_data_point = dataPoint.copy() ubidots_data_point.pop('timestamp', None) device.pushDataPoint(dataPoint) # Write data to USB key if attac keys = dataPoint.keys() if os.path.isdir(FILE_PATH): new_file = False if not os.path.isfile('%s/temperature-data.json' % FILE_PATH): new_file = True # Create the file if it does not exist so that we can open it with r+ if new_file: with open('%s/temperature-data.json' % FILE_PATH, 'w+') as f: f.write('') with open('%s/temperature-data.json' % FILE_PATH, 'r+') as f: if new_file: f.write('[') else: f.read() f.seek(-1, os.SEEK_END) f.write(',\n') f.write(json.dumps(dataPoint, indent=2)) f.write(']') with open('%s/temperature-data.csv' % FILE_PATH, 'a+') as f: dict_writer = csv.DictWriter(f, keys) dict_writer.writerows([dataPoint]) # write to oled screen oledHelper.writeMeasurements(temperature)