简体   繁体   中英

Pyserial with Arduino and multiple ds18b20

I am building a thermostat GUI using Arduino and Python 3 Tkinter . I have multiple ds18b20 sensors hooked to an Arduino card. I can get all the sensors to read in one tkinter label, however, I need to just read one sensor and place it in a label, then read the second and place it in a different label etc. I can't figure out how to read each sensor separately. Any help is greatly appreciated.

You need to do a back-and-forth serial conversation where the python code sends "I want sensor {1}". The Arduino code must act on this input by first determining which sensor reading is required, reading the relevant sensor, converting signal to degF or degC or whatever, then sending the result back through serial.

This function can then be called from within GUI code.

Python pseudocode:

set up serial connection

def get_val(sensor):
    serial send sensor
    serial get value
    return value

sensors = [1,2,3]

for each in sensors:
    temp = get_val(each)

Arduino pseudocode:

SENSOR1_PIN = A4;
SENSOR2_PIN = A3;

setup {
    serial
    pins
}

main {
    get serial char
    if (char==1) {
        value = analog read SENSOR1_PIN
        - do some maths -
        serial send temp
    }
    if (char==2) { ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM