bootmed

Untitled

Nov 30th, 2025
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. It's a big project (a switch board for a backup stream for my church's video and audio live streaming).
  2. 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.)
  3. 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.
  4. This is used in the sketch on the esp to handle serial input and output:
  5. void handleSerialInput() {
  6. String input_string;
  7. while (Serial.available() > 0) {
  8. //
  9. //
  10. // DON'T FORGET TO TERMINATE WITH A ;
  11. //
  12. //
  13. input_string = Serial.readStringUntil(';');
  14. }
  15. const char* input_char = input_string.c_str();
  16.  
  17.  
  18. if (input_char[0] == 'd') {
  19. // d=update display
  20. display.clearDisplay();
  21. display.setCursor(0, 0);
  22. const char* input_skip_first = &input_char[1];
  23. display.write(input_skip_first);
  24. display.display();
  25. } else if (input_char[0] == 's') {
  26. handleSerialOutput();
  27. } else if (input_char[0] == 'l') {
  28. handleLEDs(input_char[1], input_char[2]);
  29. }
  30. }
  31.  
  32.  
  33. void handleSerialOutput() {
  34. bool L0 = 0;
  35. bool R0 = 0;
  36. bool L1 = 0;
  37. bool R1 = 0;
  38. bool M = 0;
  39. L0 = digitalRead(13);
  40. R0 = digitalRead(32);
  41. L1 = digitalRead(14);
  42. R1 = digitalRead(27);
  43. M = digitalRead(26);
  44.  
  45.  
  46. Serial.print("{\"L0\":");
  47. Serial.print(L0);
  48. Serial.print(",");
  49. Serial.print("\"R0\":");
  50. Serial.print(R0);
  51. Serial.print(",");
  52. Serial.print("\"L1\":");
  53. Serial.print(L1);
  54. Serial.print(",");
  55. Serial.print("\"R1\":");
  56. Serial.print(R1);
  57. Serial.print(",");
  58. Serial.print("\"M\":");
  59. Serial.print(M);
  60. Serial.println("}");
  61. }
  62. Here are some python snippets:
  63.  
  64. This handles tasks that need to be done ever 50 milliseconds (updating the display with the cpu/memory info)
  65. while True:
  66.  
  67. # ...
  68.  
  69. lines = ["", "", "", ""]
  70.  
  71. # ...
  72.  
  73. # handle 50 millis tasks
  74. if time.time() * 1000 > fifty_millis_timer * 1000 + 50:
  75. fifty_millis_timer = time.time()
  76. lines[2] = get_cpu_mem_line()
  77. write_to_display(ser, lines)
  78. time.sleep(0.01) # a little time is needed between commands.
  79. ser.write("s;".encode("ascii"))
  80. time.sleep(0.01) # a little time is needed between commands.
  81.  
  82. Here's the function to get the cpu/memory data:
  83. import psutil
  84.  
  85. # ...
  86.  
  87. def get_cpu_mem_line():
  88. # https://stackoverflow.com/a/2468983/3814307
  89. # cpu = psutil.cpu_percent() # Current amount - with a lot of fluctuation.
  90. cpu = round(psutil.getloadavg()[0] / psutil.cpu_count() * 100, 1) # Average over 1 minute
  91. if cpu < 10:
  92. cpu = " " + str(cpu)
  93. ram = round(psutil.virtual_memory().available * 100 / psutil.virtual_memory().total, 1)
  94. if ram < 10:
  95. ram = " " + str(ram)
  96. return f"CPU: {cpu}% RAM: {ram}%"
  97.  
  98. This is some of the code used to start the serial connection and write/read to the display:
  99. ser = serial.Serial(port, 115200, timeout=1)
  100. # Wait for the connection to be established
  101. time.sleep(2)
  102. def write_to_display(ser, lines):
  103. text = "\n".join(lines)
  104.  
  105.  
  106. # Add special characters so the firmware knows this is for the display and where the end of the input is.
  107. text = "d" + text + ";"
  108. ser.write(text.encode("ascii"))
  109. def read_switch_status(ser):
  110. line = ser.readline()
  111. status = None
  112. success = False
  113. try:
  114. status = json.loads(line.decode())
  115. success = True
  116. except:
  117. print("failed retrieving status JSON from serial")
  118. return status, success
  119. excluded_ports = ["/dev/ttyS0"]
  120.  
  121.  
  122.  
  123. def find_serial_ports():
  124. """Returns a list of serial ports available on the system."""
  125. ports = serial.tools.list_ports.comports()
  126. return [port.device for port in ports if port.device not in excluded_ports]
  127.  
Advertisement
Add Comment
Please, Sign In to add comment