Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- It's a big project (a switch board for a backup stream for my church's video and audio live streaming).
- But here is some of the code - (but with the caveat that if I were to remake this, I would structure it differently and use interrupts to send switch changes as the happen, instead of python requesting switch status by sending an 's' character. The way it is below is too simplistic, imo, but also, it's working well.)
- tl;dr: This took a lot of troubleshooting, (this was before the LLM era), but if you don't give up, you should be able to figure it out. It's possible to be done reliably. The code below may be a good starting place for asking some questions to chatgpt about how it could work in your case.
- This is used in the sketch on the esp to handle serial input and output:
- void handleSerialInput() {
- String input_string;
- while (Serial.available() > 0) {
- //
- //
- // DON'T FORGET TO TERMINATE WITH A ;
- //
- //
- input_string = Serial.readStringUntil(';');
- }
- const char* input_char = input_string.c_str();
- if (input_char[0] == 'd') {
- // d=update display
- display.clearDisplay();
- display.setCursor(0, 0);
- const char* input_skip_first = &input_char[1];
- display.write(input_skip_first);
- display.display();
- } else if (input_char[0] == 's') {
- handleSerialOutput();
- } else if (input_char[0] == 'l') {
- handleLEDs(input_char[1], input_char[2]);
- }
- }
- void handleSerialOutput() {
- bool L0 = 0;
- bool R0 = 0;
- bool L1 = 0;
- bool R1 = 0;
- bool M = 0;
- L0 = digitalRead(13);
- R0 = digitalRead(32);
- L1 = digitalRead(14);
- R1 = digitalRead(27);
- M = digitalRead(26);
- Serial.print("{\"L0\":");
- Serial.print(L0);
- Serial.print(",");
- Serial.print("\"R0\":");
- Serial.print(R0);
- Serial.print(",");
- Serial.print("\"L1\":");
- Serial.print(L1);
- Serial.print(",");
- Serial.print("\"R1\":");
- Serial.print(R1);
- Serial.print(",");
- Serial.print("\"M\":");
- Serial.print(M);
- Serial.println("}");
- }
- Here are some python snippets:
- This handles tasks that need to be done ever 50 milliseconds (updating the display with the cpu/memory info)
- while True:
- # ...
- lines = ["", "", "", ""]
- # ...
- # handle 50 millis tasks
- if time.time() * 1000 > fifty_millis_timer * 1000 + 50:
- fifty_millis_timer = time.time()
- lines[2] = get_cpu_mem_line()
- write_to_display(ser, lines)
- time.sleep(0.01) # a little time is needed between commands.
- ser.write("s;".encode("ascii"))
- time.sleep(0.01) # a little time is needed between commands.
- Here's the function to get the cpu/memory data:
- import psutil
- # ...
- def get_cpu_mem_line():
- # https://stackoverflow.com/a/2468983/3814307
- # cpu = psutil.cpu_percent() # Current amount - with a lot of fluctuation.
- cpu = round(psutil.getloadavg()[0] / psutil.cpu_count() * 100, 1) # Average over 1 minute
- if cpu < 10:
- cpu = " " + str(cpu)
- ram = round(psutil.virtual_memory().available * 100 / psutil.virtual_memory().total, 1)
- if ram < 10:
- ram = " " + str(ram)
- return f"CPU: {cpu}% RAM: {ram}%"
- This is some of the code used to start the serial connection and write/read to the display:
- ser = serial.Serial(port, 115200, timeout=1)
- # Wait for the connection to be established
- time.sleep(2)
- def write_to_display(ser, lines):
- text = "\n".join(lines)
- # Add special characters so the firmware knows this is for the display and where the end of the input is.
- text = "d" + text + ";"
- ser.write(text.encode("ascii"))
- def read_switch_status(ser):
- line = ser.readline()
- status = None
- success = False
- try:
- status = json.loads(line.decode())
- success = True
- except:
- print("failed retrieving status JSON from serial")
- return status, success
- excluded_ports = ["/dev/ttyS0"]
- def find_serial_ports():
- """Returns a list of serial ports available on the system."""
- ports = serial.tools.list_ports.comports()
- return [port.device for port in ports if port.device not in excluded_ports]
Advertisement
Add Comment
Please, Sign In to add comment