async def update_pump(self): with PubSub.Subscription(self.hub, topics.TOPIC_PUMP_ON) as queue: while True: if await queue.get(): print("Starting pump") await self.pi.write(self.pump_pin, 1) else: print("Stopping pump") await self.pi.write(self.pump_pin, 0)
async def start_stop_brewing(self): with PubSub.Subscription(self.hub, topics.TOPIC_COFFEE_BUTTON) as queue: while True: coffee_button = await queue.get() if coffee_button: self.hub.publish(topics.TOPIC_START_BREW, None) else: self.hub.publish(topics.TOPIC_STOP_BREW, None)
async def update_solenoid(self): with PubSub.Subscription(self.hub, topics.TOPIC_SOLENOID_OPEN) as queue: while True: if await queue.get(): print("Opening solenoid") await self.pi.write(self.solenoid_pin, 1) else: print("Closing solenoid") await self.pi.write(self.solenoid_pin, 0)
async def update_he_from_steam(self): with PubSub.Subscription(self.hub, topics.TOPIC_STEAM_HE_ON) as queue: while True: steam_he_on = await queue.get() if self.steam_mode: if steam_he_on and self.he_enabled: self.boiler.heat_on() else: self.boiler.heat_off()
async def maybe_update_button(self): with PubSub.Subscription(self.hub, topics.TOPIC_BUTTON_PROXY) as queue: while True: (gpio, level, tick, topic) = await queue.get() await asyncio.sleep(0.005) new_level = await self.pi.read(gpio) if new_level == level: self.hub.publish(topic, level == 1) else: print("False button " + topic)
async def stop_brew_timer(self): with PubSub.Subscription(self.hub, topics.TOPIC_STOP_BREW) as queue: while True: await queue.get() try: duration = time.time() - self.start_time except TypeError: continue self.start_time = None self.hub.publish(topics.TOPIC_CURRENT_BREW_START_TIME, None) self.hub.publish(topics.TOPIC_LAST_BREW_DURATION, duration)
async def temperature_update(self): with PubSub.Subscription(self.hub, topics.TOPIC_ALL_TEMPERATURES) as queue: while True: temp_boiler, temp_group = await queue.get() if self.profiling: self.current_brew_data.append([ time.time(), temp_boiler, temp_group, self.current_weight, self.current_avgpid, self.solenoid, self.pump ])
async def start_brew(self): with PubSub.Subscription(self.hub, topics.TOPIC_START_BREW) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_SOLENOID_OPEN, True) self.hub.publish(topics.TOPIC_PUMP_ON, True) if self.use_preinfusion: await asyncio.sleep(self.preinfusion_time) self.hub.publish(topics.TOPIC_SOLENOID_OPEN, False) self.hub.publish(topics.TOPIC_PUMP_ON, False) await asyncio.sleep(self.dwell_time) self.hub.publish(topics.TOPIC_SOLENOID_OPEN, True) self.hub.publish(topics.TOPIC_PUMP_ON, True)
async def update_pid_control(self): pid = simple_pid_fork.PID(setpoint=self.setpoint, windup_limits=(-20, 20)) pid.tunings = self.tunings pid.sample_time = self.temperature_update_interval loop = asyncio.get_running_loop() pidhist = ResizableRingBuffer(self.responsiveness) last_advice = 0 try: with PubSub.Subscription( self.hub, topics.TOPIC_AVERAGE_BOILER_TEMPERATURE) as queue: while True: avgtemp = await queue.get() if self.setpoint != pid.setpoint: pid.setpoint = self.setpoint if self.tunings != pid.tunings: pid.tunings = self.tunings if self.responsiveness != pidhist.size_max: pidhist.resize(self.responsiveness) pidout = pid(avgtemp) pidhist.append(pidout) avgpid = pidhist.avg() self.hub.publish(topics.TOPIC_PID_VALUE, pidout) self.hub.publish(topics.TOPIC_PID_TERMS, pid.factored_components) now = loop.time() # We only want to advise the HE Controller to update once per second if now > last_advice + 1: last_advice = now self.hub.publish(topics.TOPIC_PID_AVERAGE_VALUE, avgpid) finally: pid.reset()
async def start_profiling(self): with PubSub.Subscription(self.hub, topics.TOPIC_START_BREW) as queue: while True: await queue.get() if self.enable_weighted_shots and self.scale_connected: self.profiling = True self.current_brew_metadata[ 'target_weight'] = self.target_weight self.current_brew_metadata['start_time'] = time.time() self.current_brew_metadata[ 'preinfusion'] = self.preinfusion self.current_brew_metadata[ 'preinfusion_time'] = self.preinfusion_time self.current_brew_metadata['dwell_time'] = self.dwell_time self.current_brew_metadata['tunings'] = self.tunings self.current_brew_metadata[ 'responsiveness'] = self.responsiveness
async def weight_update(self): with PubSub.Subscription(self.hub, topics.TOPIC_SCALE_WEIGHT) as queue: while True: weight = await queue.get() self.current_weight = weight if self.brewing and self.enable_weighted_shots: self.previous_measurements.append((time(), weight)) self.previous_flow_rates.append(self.flow_rate()) reaction_compensation = self.previous_flow_rates.avg( ) * self.weighted_shot_reaction_time if reaction_compensation < 0: reaction_compensation = 0 # Disregard reaction compensations larger than 5 grams, something is obviously up here if reaction_compensation > 5: reaction_compensation = 0 nominal_weight = weight + reaction_compensation - self.tare_weight if nominal_weight >= self.target_weight: self.hub.publish(topics.TOPIC_STOP_BREW, None)
async def stop_profiling(self): with PubSub.Subscription(self.hub, topics.TOPIC_STOP_BREW) as queue: while True: await queue.get() if self.profiling: self.current_brew_metadata['stop_time'] = time.time() self.current_brew_metadata[ 'stop_weight'] = self.current_weight if self.dose_weight is not None: self.current_brew_metadata['dose'] = self.dose_weight await asyncio.sleep(self.post_brew_profiling_time) self.current_brew_metadata[ 'final_weight'] = self.current_weight brew_data = self.current_brew_data metadata = self.current_brew_metadata self.reset() self.store(brew_data, metadata)
coro.publish_authoritative() async def safe_actuators(pi, solenoid_pin, pump_pin, he_pin): print("Safing actuators") await pi.write(he_pin, 0) await pi.write(pump_pin, 0) await pi.write(solenoid_pin, 0) if __name__ == '__main__': bleak_logger = logging.getLogger('bleak') bleak_logger.setLevel(logging.WARN) loop = asyncio.get_event_loop() hub = PubSub.Hub() if conf.test_hardware: s = temperature_sensor.EmulatedSensor({}) b = boiler.EmulatedBoiler(s) p = pump.EmulatedPump() else: s = temperature_sensor.Max31865Sensor(conf.boiler_temp_sensor_cs_pin, conf.group_temp_sensor_cs_pin, rtd_nominal_boiler=100.65, rtd_nominal_group=101.5) b = boiler.GpioBoiler(conf.he_pin) p = pump.GpioPump(conf.pump_pin) pi = apigpio_fork.Pi(loop) address = (conf.pigpio_host, conf.pigpio_port)
async def update_ivar(self, topic, key): with PubSub.Subscription(self.hub, topic) as queue: while True: setattr(self, key, await queue.get())
async def on_stop_brew(self): with PubSub.Subscription(self.hub, topics.TOPIC_STOP_BREW) as queue: while True: await queue.get() self.tare_weight = 0.0 self.brewing = False
async def stop_brew(self): with PubSub.Subscription(self.hub, topics.TOPIC_STOP_BREW) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_SOLENOID_OPEN, False) self.hub.publish(topics.TOPIC_PUMP_ON, False)
async def start_stop_pump(self): with PubSub.Subscription(self.hub, topics.TOPIC_WATER_BUTTON) as queue: while True: water_button = await queue.get() self.hub.publish(topics.TOPIC_PUMP_ON, water_button)
async def white_button_press(self): with PubSub.Subscription(self.hub, topics.TOPIC_WHITE_BUTTON) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_USE_PREINFUSION, not self.enable_preinfusion)
async def blue_button_press(self): with PubSub.Subscription(self.hub, topics.TOPIC_BLUE_BUTTON) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_ENABLE_WEIGHTED_SHOT, not self.enable_weighted_shot)
async def red_button_press(self): with PubSub.Subscription(self.hub, topics.TOPIC_RED_BUTTON) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_CONNECT_TO_SCALE, not self.keep_scale_connected)
async def printer(hub, ignored_topics=frozenset()): with PubSub.Listener(hub) as queue: while True: key, msg = await queue.get() if key not in ignored_topics: print(f'Reader for key {key} got message: {msg}')
async def capture_dose(self): with PubSub.Subscription(self.hub, topics.TOPIC_CAPTURE_DOSE) as queue: while True: await queue.get() self.hub.publish(topics.TOPIC_DOSE, self.current_weight)
async def update_steam_mode(self): with PubSub.Subscription(self.hub, topics.TOPIC_STEAM_BUTTON) as queue: while True: steam_button = await queue.get() self.hub.publish(topics.TOPIC_STEAM_MODE, steam_button)