import json import modules.mqtt_module as mqtt import config_module as config import modules.power_module as power_sensor if __name__ == "__main__": client = mqtt.Create() voltage, current, power, shunt_voltage = power_sensor.read(0) jobject = {"v": voltage, "i": current, "p": power, "sv": shunt_voltage} client.publish(config.power_logic_topic, json.dumps(jobject)) voltage, current, power, shunt_voltage = power_sensor.read(1) jobject = {"v": voltage, "i": current, "p": power, "sv": shunt_voltage} client.publish(config.power_main_topic, json.dumps(jobject))
channel = int(message.topic.split("/")[2]) value = int(message.payload.decode("utf-8")) print(f"Channel:{channel} {value}") driver.writeSpeed(channel, value) def push_encoder_message(index): value = driver.readEncoder(index) client.publish(config.encoders_topic_template + str(index), value) if __name__ == "__main__": driver = dc_module.DcMotorDriver() version = driver.readVersion() client = mqtt.Create(config.motors_topic, on_message) print("Motors service ready") client.loop_start() # client.loop_forever() while True: for i in range(4): push_encoder_message(i) time.sleep(0.1) client.loop_stop(force=False) print("Motors service stopped")
import modules.mqtt_module as mqtt import config_module as config def speak(command): engine.say(command) engine.runAndWait() def on_message(client, userdata, message): value = message.payload.decode("utf-8") speak(value) if __name__ == "__main__": engine = pyttsx3.init() engine.setProperty('volume', 1) engine.setProperty('rate', 300) # voices = engine.getProperty('voices') # for voice in voices: # if voice.name.startswith('english'): # print(f"Name:{voice.name} Id:{voice.id} languages:{voice.languages} Age:{voice.age} Gender:{voice.gender} ") engine.setProperty('voice', 'english') client = mqtt.Create(config.audio_say, on_message) speak("Voice service ready") client.loop_forever()
import i2c_lcd import modules.network_module as network import modules.mqtt_module as mqtt import config_module as config lcd = i2c_lcd.lcd() def on_message(client, userdata, message): line = int(message.topic.split("/")[3]) value = message.payload.decode("utf-8") #print(f"Line:{line} Value:{value}") lcd.lcd_display_string(" ", line) lcd.lcd_display_string(value, line) if __name__ == "__main__": lcd.lcd_clear() ip, host = network.get_interface() lcd.lcd_display_string(f"IP: {ip}", 1) lcd.lcd_display_string(f"{host}", 2) client = mqtt.Create(config.lcd_lines, on_message) print("LCD service ready") client.loop_forever() print("LCD service stopped")
if h == 240: # max if w == 320: motor_y = -0.5 # go back, too close else: motor_y = 0.5 # go closer, too far print("X value: " + str(motor_x)) print("Y value: " + str(motor_y)) steering.updateMotors(client, motor_x, motor_y) # time.sleep(200) # steering.updateMotors(client, 0, 0) def on_message(client, userdata, message): raw = message.payload.decode("utf-8") print(raw) objects = json.loads(message.payload.decode("utf-8")) for obj in objects: index = obj['classid'] if index == 14: x = obj['x'] y = obj['y'] w = obj['w'] h = obj['h'] on_person(x, y, w, h) if __name__ == "__main__": client = mqtt.Create(config.machine_vision_objects, on_message) client.loop_forever()
def on_message(client, userdata, message): channel = int(message.topic.split("/")[2]) name = str(message.topic.split("/")[3]) value = int(message.payload.decode("utf-8")) index = int(channel / 16) channel = channel % 16 print(f"Device:{index} Channel:{channel} {name}:{value}") if (name == 'duty_cycle'): c = devices[index].channels[channel] # c = pca.channels[channel] c.duty_cycle = value elif (name == 'angle'): s = servo.Servo(devices[index].channels[channel], min_pulse=600, max_pulse=2600) s.angle = value if __name__ == "__main__": client = mqtt.Create(config.pwm_topic, on_message) print("PWM service ready") client.loop_forever() print("PWM service stopped")