This code needs more documentation! I hope to add some in the fullness of time. Apologies for poor or inefficient coding! NB. This code plugin does nor wrap text. Use arrow keys, mouse, pad or scroll bars at the bottom to view text on extreme right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | # GUI to control RPI GPIO and monitor Feather # 11.8.19 # by Julian Rogers # now greenhouse7 # was feather_reset_etc20 # more data recieved from Feather in v21 # data displayed in v1 # # # # # #IP_FEATHER4 = "##.##.##.##" #remote ip address IP_FEATHER4 = "##.##.##.##" #remote ip address - actually Feather 3 DEST_PORT_FEATHER4 = #### THIS_PORT = #### #port on this computer BACKGROUND = "light gray" BUTTON_1 = "light yellow" #buttons: advance, load, refresh BUTTON_2 = "cornflower blue" #update buttons global watchdog watchdog = 0 global count1 count1 = 0 global count2 count2 = 0 global commnd commnd = "" global first first = True global wpress_hi wpress_hi = 0 global wpress_lo wpress_lo = 0 global rhumid_hi rhumid_hi = 0 global rhumid_lo rhumid_lo = 0 global intemp_hi intemp_hi = 0 global intemp_lo intemp_lo = 0 global outtemp_hi outtemp_hi = 0 global outtemp_lo outtemp_lo = 0 global theFirst theFirst = True global rst_press rst_press = False global rst_rh rst_rh = False global rst_in rst_in = False global rst_out rst_out = False global wpress_int global rhumid_int global intemp_int global outtemp_int global wpress_curr global rh_curr global in_curr global out_curr wpress_int = 0 rhumid_int = 0 intemp = 0 outtemp = 0 press_curr = 0 rh_curr = 0 in_curr = 0 out_curr = 0 from tkinter import * from meter import * import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings( False ) GPIO.setup( 23 , GPIO.OUT) GPIO.setup( 24 , GPIO.OUT) GPIO.setup( 25 , GPIO.OUT) GPIO.setup( 12 , GPIO.OUT) GPIO.setup( 16 , GPIO.OUT) GPIO.setup( 20 , GPIO.OUT) GPIO.setup( 21 , GPIO.OUT) GPIO.setup( 27 , GPIO.OUT) #Feather reset pin - not used now. Feather reset by usb power-off GPIO.setup( 22 , GPIO.OUT) #not used GPIO.setup( 19 , GPIO.OUT) #reset hardware watchdog GPIO.setup( 26 , GPIO.IN) #Feather pin 16 - "watchdog" GPIO.output( 23 , False ) GPIO.output( 24 , False ) GPIO.output( 25 , False ) GPIO.output( 12 , False ) GPIO.output( 16 , False ) GPIO.output( 20 , False ) GPIO.output( 21 , False ) GPIO.output( 19 , False ) import subprocess import os import datetime import time import socket #UDP sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) from time import sleep # create the root window root = Tk() # modify the window root.title( "9.8.19 V7" ) root.geometry( "900x700" ) root.configure(bg = BACKGROUND) #root.attributes('-fullscreen', True) #eliminates the title bar # the following sets the window full screen #w, h = root.winfo_screenwidth(), root.winfo_screenheight() #root.geometry("%dx%d+0+0" % (w, h)) # create a frame app = Frame(root) app.configure(bg = BACKGROUND) app.grid() title_lab = Label(app, text = "Greenhouse Control Central" , font = ( "Arial Bold" , 20 ), fg = "maroon" , bg = BACKGROUND) title_lab.grid(row = 1 , column = 1 , columnspan = 8 ) blank_lab = Label(app, bg = BACKGROUND) blank_lab.grid(row = 2 , column = 1 ) sys_1_but = Button(app, text = " Water " , font = ( "Arial" , 12 ), fg = "maroon" , bg = "light blue" ) sys_1_but.grid(row = 3 , column = 1 ) def on_off_1(): but_col = sys_1_but.config( 'bg' )[ - 1 ] if but_col = = "light blue" : but_col = "yellow" GPIO.output( 23 , True ) else : but_col = "light blue" GPIO.output( 23 , False ) sys_1_but.config(bg = but_col) sys_1_but.config(command = on_off_1) sys_2_but = Button(app, text = "W/DG RST" , font = ( "Arial" , 12 ), fg = "maroon" , bg = "light blue" ) sys_2_but.grid(row = 3 , column = 2 ) def on_off_2(): but_col = sys_2_but.config( 'bg' )[ - 1 ] if but_col = = "light blue" : but_col = "yellow" GPIO.output( 19 , True ) else : but_col = "light blue" GPIO.output( 19 , False ) sys_2_but.config(bg = but_col) sys_2_but.config(command = on_off_2) loop_ind = Button(app, text = "X" , font = ( "Arial" , 8 ), fg = "white" , bg = "white" ) loop_ind.grid(row = 3 , column = 4 ) loop_lab = Label(app, text = "loop ind " , font = ( "Arial" , 12 ), fg = "maroon" , bg = BACKGROUND) loop_lab.grid(row = 4 , column = 4 ) sys_3_but = Button(app, text = "Rst Fthr" , font = ( "Arial" , 12 ), fg = "maroon" , bg = "pink" ) sys_3_but.grid(row = 3 , column = 3 ) def reset_feather(): #GPIO.output(27, True) #sleep(0.1) #GPIO.output(27, False) #now uses hub-ctrl to turn toggle usb power os.system( 'sudo /home/pi/hub-ctrl -h 1 -P 2 -p 0' ) sleep( 0.1 ) os.system( 'sudo /home/pi/hub-ctrl -h 1 -P 2 -p 1' ) sys_3_but.config(command = reset_feather) blank_lab1 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND) blank_lab1.grid(row = 4 , column = 1 ) blank_lab2 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND) blank_lab2.grid(row = 9 , column = 1 ) ind_lab = Label(app, text = "run ind" , font = ( "Arial" , 12 ), fg = "maroon" , bg = BACKGROUND) ind_lab.grid(row = 6 , column = 4 ) run_ind = Button(app, text = "X" , font = ( "Arial" , 8 ), fg = "white" , bg = "white" ) run_ind.grid(row = 5 , column = 4 ) watchdog_but = Button(app, text = " " , font = ( "Arial" , 8 ), fg = "black" , bg = "white" ) watchdog_but.grid(row = 1 , column = 4 ) time_lab = Label(app, text = " " , font = ( "Arial" , 14 ), fg = "maroon" , bg = BACKGROUND) time_lab.grid(row = 1 , column = 1 ) system_lab0 = Label(app, text = "Remote data: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND) system_lab0.grid(row = 7 , column = 1 ) system_lab = Button(app, text = "xx:xx:xx:xx:xx:xx:xx:xx:xx" , font = ( "Arial" , 10 ), fg = "black" , bg = "white" ) system_lab.grid(row = 7 , column = 2 ) time_but = Button(app, text = "--:--" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND) time_but.grid(row = 7 , column = 3 ) rg_reset_but = Button(app, text = "Rst R/G" , font = ( "Arial" , 12 ), fg = "green" , bg = "white" ) rg_reset_but.grid(row = 8 , column = 1 ) def reset_raingauge(): but_col = rg_reset_but.config( 'bg' )[ - 1 ] if but_col = = "white" : but_col = "yellow" GPIO.output( 24 , True ) else : but_col = "white" GPIO.output( 24 , False ) rg_reset_but.config(bg = but_col) rg_reset_but.config(command = reset_raingauge) blank_lab3 = Label(app, text = "", fg = BACKGROUND, bg = BACKGROUND) blank_lab3.grid(row = 11 , column = 1 ) text_lab = Label(app, text = "Command entry " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND) text_lab.grid(row = 12 , column = 1 ) entry1 = Entry(app, font = ( "Arial" , 16 ), fg = "black" , bg = "white" ) entry1.grid(row = 12 , column = 2 ) feedback_lab = Label(app, text = " ", font = (" Arial ", 16), fg = " blue", bg = BACKGROUND) feedback_lab.grid(row = 13 , column = 2 ) conf_but = Button(app, text = "Confirm" , font = ( "Arial" , 16 ), fg = "blue" , bg = BACKGROUND) conf_but.grid(row = 12 , column = 3 ) canc_but = Button(app, text = "Cancel " , font = ( "Arial" , 16 ), fg = "blue" , bg = BACKGROUND) canc_but.grid(row = 13 , column = 3 ) fb_lab = Label(app, text = "Last command: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND) fb_lab.grid(row = 13 , column = 1 ) test_lab = Label(app, text = " ", font = (" Arial ", 16), fg = " maroon", bg = BACKGROUND) test_lab.grid(row = 14 , column = 1 ) def press_rst(): global rst_press rst_press = True #meter to register water pressure meter = Meter(app, width = 200 ) meter.setrange(start = 0 , end = 100 ) meter.blob( "red" ) meter. set ( 0 ) meter.grid(row = 15 , column = 1 ) meter.config(height = 200 ) meter_but = Button(app, text = " Water Pressure " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) meter_but.grid(row = 16 , column = 1 ) meter_but.config(command = press_rst) def rh_rst(): global rst_rh rst_rh = True #meter to register RH meter1 = Meter(app, width = 200 ) meter1.setrange(start = 0 , end = 100 ) meter1.blob( "green" ) meter1. set ( 0 ) meter1.grid(row = 15 , column = 2 ) meter1.config(height = 200 ) meter1_but = Button(app, text = "Relative Humidity" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) meter1_but.grid(row = 16 , column = 2 ) meter1_but.config(command = rh_rst) def in_rst(): global rst_in rst_in = True #meter to register inside temp meter2 = Meter(app, width = 200 ) meter2.setrange(start = 0 , end = 50 ) meter2.blob( "blue" ) meter2. set ( 0 ) meter2.grid(row = 15 , column = 3 ) meter2.config(height = 200 ) meter2_but = Button(app, text = " Inside Temp " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) meter2_but.grid(row = 16 , column = 3 ) meter2_but.config(command = in_rst) def out_rst(): global rst_out rst_out = True #meter to register outside temp meter3 = Meter(app, width = 200 ) meter3.setrange(start = 0 , end = 50 ) meter3.blob( "yellow" ) meter3. set ( 0 ) meter3.grid(row = 15 , column = 4 ) meter3.config(height = 200 ) meter_but3 = Button(app, text = " Outside Temp " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) meter_but3.grid(row = 16 , column = 4 ) meter_but3.config(command = out_rst) press_lab = Label(app, text = "H: " + "L: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) press_lab.grid(row = 17 , column = 1 ) rh_lab = Label(app, text = "H: " + "L: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) rh_lab.grid(row = 17 , column = 2 ) in_lab = Label(app, text = "H: " + "L: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) in_lab.grid(row = 17 , column = 3 ) out_lab = Label(app, text = "H: " + "L: " , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) out_lab.grid(row = 17 , column = 4 ) rg_but = Button(app, text = "rain" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) rg_but.grid(row = 10 , column = 1 ) damp_but = Button(app, text = "damp" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) damp_but.grid(row = 10 , column = 2 ) settemp_but = Button(app, text = "set temp" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) settemp_but.grid(row = 10 , column = 3 ) fanset_but = Button(app, text = "set fan" , font = ( "Arial" , 16 ), fg = "maroon" , bg = BACKGROUND ) fanset_but.grid(row = 10 , column = 4 ) def confirm(): global commnd commnd = entry1.get() conf_but.config(command = confirm) def cancel(): entry1.delete( 0 , "end" ) canc_but.config(command = cancel) def get_data_remote(): global wpress_hi global wpress_lo global rhumid_hi global rhumid_lo global intemp_hi global intemp_lo global outtemp_hi global outtemp_lo global theFirst global rst_press global rst_rh global rst_in global rst_out global wpress_int global rhumid_int global intemp_int global outtemp_int global wpress_curr global rh_curr global in_curr global out_curr sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try : sock.sendto(bytes( "r" , "utf-8" ) , (IP_FEATHER4, DEST_PORT_FEATHER4)) sock.settimeout( 10 ) #response = sock.recv(100) response = sock.recv( 200 ) except socket.error: #system_lab.config(text = "timed out!") #response = bytes("----T---/", "utf-8") response = bytes( "--:--:--:--:--:--:--:--:--" , "utf-8" ) sock.close() if response = = bytes( "r" , "utf-8" ): #response = bytes("rrrrTrrr/", "utf-8") response = bytes( "##:##:##:##:##:##:##:##:##" , "utf-8" ) response = str (response, "utf-8" ) system_lab.config(text = response) try : tim, intemp, settemp, outtemp, fansettemp, rhumid, tips, wpress, damp = response.split( ":" ) except ValueError: tim = "0" intemp = "0" settemp = "0" outtemp = "0" fansettemp = "0" rhumid = "0" tips = "0" wpress = "0" damp = "0" try : tim_int = int (tim) except ValueError: tim_int = 0 clock_hrs = tim_int / / 60 clock_mins = tim_int % 60 if clock_hrs < 10 : clock_hrs_str = "0" + str (clock_hrs) else : clock_hrs_str = str (clock_hrs) if clock_mins < 10 : clock_mins_str = "0" + str (clock_mins) else : clock_mins_str = str (clock_mins) time_but.config(text = clock_hrs_str + ":" + clock_mins_str) #RH is never zero, so this is quantity checked to see if data #has been received or not - so needs to be checked first. try : rhumid_int = int (rhumid) except ValueError: rhumid_int = 0 if rhumid_int ! = 0 : rhumid_curr = rhumid_int meter1. set (rhumid_int / 10 ) if rhumid = = 0 : meter1. set (rhumid_curr / 10 ) try : wpress_int = int (wpress) except ValueError: wpress_int = 0 if rhumid_int ! = 0 : wpress_curr = wpress_int meter. set (wpress_int) if rhumid = = 0 : meter. set (wpress_curr) try : intemp_int = int (intemp) except ValueError: intemp_int = 0 if rhumid_int ! = 0 : in_curr = intemp_int meter2. set (intemp_int / 10 ) if rhumid = = 0 : meter2. set (in_curr / 10 ) try : outtemp_int = int (outtemp) except ValueError: outtemp_int = 0 if rhumid_int ! = 0 : out_curr = outtemp_int meter3. set (outtemp_int / 10 ) if rhumid = = 0 : meter3. set (out_curr / 10 ) try : settemp_int = round ( int (settemp) / 10 ) except ValueError: settemp_int = 0 try : fansettemp_int = round ( int (fansettemp) / 10 ) except ValueError: fansettemp_int = 0 if wpress_int > wpress_hi: wpress_hi = wpress_int if theFirst = = True and wpress_int ! = 0 : wpress_lo = wpress_int elif wpress_int ! = 0 and wpress_int < wpress_lo: wpress_lo = wpress_int if rhumid_int > rhumid_hi: rhumid_hi = rhumid_int if theFirst = = True and rhumid_int ! = 0 : rhumid_lo = rhumid_int elif rhumid_int ! = 0 and rhumid_int < rhumid_lo: rhumid_lo = rhumid_int if intemp_int > intemp_hi: intemp_hi = intemp_int if theFirst = = True and intemp_int ! = 0 : intemp_lo = intemp_int elif intemp_int ! = 0 and intemp_int < intemp_lo: intemp_lo = intemp_int if outtemp_int > outtemp_hi: outtemp_hi = outtemp_int #NB outtemp_int could be legit zero deg C. Needs to be fixed if theFirst = = True and outtemp_int ! = 0 : outtemp_lo = outtemp_int elif outtemp_int ! = 0 and outtemp_int < outtemp_lo: outtemp_lo = outtemp_int if rhumid_int > 0 : theFirst = False press_lab.config(text = "Hi: " + str (wpress_hi) + " Lo: " + str (wpress_lo)) rh_lab.config(text = "Hi: " + str (rhumid_hi / 10 ) + " Lo: " + str (rhumid_lo / 10 )) in_lab.config(text = "Hi: " + str (intemp_hi / 10 ) + " Lo: " + str (intemp_lo / 10 )) out_lab.config(text = "Hi: " + str (outtemp_hi / 10 ) + " Lo: " + str (outtemp_lo / 10 )) rg_but.config(text = "rain: " + tips) damp_but.config(text = "soil damp: " + damp) settemp_but.config(text = "heat set: " + str (settemp_int)) fanset_but.config(text = "fan set: " + str (fansettemp_int)) # calls a function after given time def after( self , ms, func = None , * args): """call function after a given time""" # updates screen every 1 seconds def task(): global commnd global wpress_hi global wpress_lo global rhumid_hi global rhumid_lo global intemp_hi global intemp_lo global outtemp_hi global outtemp_lo global theFirst global rst_press global rst_rh global rst_in global rst_out global watchdog global count1 global count2 global first global wpress_int global rhumid_int global intemp_int global outtemp_int run_ind.config(fg = "red" ) if commnd = = "switch" or commnd = = "heaton" or commnd = = "heatoff" or commnd = = "staton" or commnd = = "statoff" : feedback_lab.config(text = commnd) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try : sock.sendto(bytes(commnd, "utf-8" ) , (IP_FEATHER4, DEST_PORT_FEATHER4)) sock.settimeout( 5 ) response = sock.recv( 100 ) entry1.delete( 0 ,END) commnd = "" except socket.error: system_lab.config(text = "timed out!" ) sock.close() elif len (commnd) = = 3 : try : int (commnd) feedback_lab.config(text = commnd) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try : sock.sendto(bytes(commnd, "utf-8" ) , (IP_FEATHER4, DEST_PORT_FEATHER4)) sock.settimeout( 5 ) response = sock.recv( 100 ) entry1.delete( 0 ,END) commnd = "" except socket.error: system_lab.config(text = "timed out!" ) sock.close() except ValueError: entry1.delete( 0 ,END) commnd = "" feedback_lab.config(text = "error" ) #feedback_lab.config(text = commnd) count2 = count2 + 1 #loop_ind.config(text = count2) if count2 % 2 = = 0 : but_col = loop_ind.config( 'bg' )[ - 1 ] if but_col = = "red" : loop_ind.config(bg = "white" , fg = "white" ) else : loop_ind.config(bg = "red" , fg = "red" ) if count2 = = 30 : get_data_remote() count2 = 0 #clear_feedback() ind = GPIO. input ( 26 ) if ind = = 1 : watchdog = watchdog + 1 run_ind.config(fg = "red" , bg = "red" ) else : run_ind.config(fg = "white" , bg = "white" ) count1 = count1 + 1 if count1 = = 30 : count1 = 0 if watchdog < 2 or watchdog > 28 : watchdog_but.config(text = "FAIL" , bg = "red" ) #if auto_reset_but.config('bg')[-1] == "red": #reset_feather() else : watchdog_but.config(text = "OK" , bg = "green" ) watchdog = 0 first = False if rst_press = = True : wpress_lo = wpress_int wpress_hi = wpress_int rst_press = False if rst_rh = = True : rhumid_lo = rhumid_int rhumid_hi = rhumid_int rst_rh = False if rst_in = = True : intemp_lo = intemp_int intemp_hi = intemp_int rst_in = False if rst_out = = True : outtemp_lo = outtemp_int outtemp_hi = outtemp_int rst_out = False time_lab.config(text = "Comp time " + time.strftime( '%H:%M' )) root.after( 1000 , task) # calls the screen update every 1 seconds root.after( 1000 , task) #--------------------------------------------------------------------------------------- # kick off the window's event-loop root.mainloop() |