Jan-02-2023, 06:09 AM
Hi All, I am hoping someone can tell me why this error is happening with my line:
Here is my complete code:
x = json.loads(sonoff01.text) status = x['relays']This code 'was' working fine, but I don't understand enough about json arrays and how they need to be called/used. My error is a Key Error for 'relays'. I updated my relay IP address and using a Sonoff Relay.
Here is my complete code:
from tkinter import * from tkinter import messagebox import json import requests import time import os ws = Tk() ws.title('Relay Control') ws.geometry("600x400") ws.configure(background='white') on = PhotoImage(file = "frPorch_on.png") off = PhotoImage(file = "frPorch_off.png") sonoff01 = 'http://192.168.0.113/cm?cmnd=status+10' sonURL = 'NOT_INIT' sonURL = sonoff01 sonoff01 = requests.get(sonURL) x = json.loads(sonoff01.text) status = x['relays'] a = status is_on = a[0]['ison'] print(is_on) def Switch(): global is_on if is_on: button.config(image = off) label.config(text = "Switch is Off", fg = "grey", bg='white') requests.post('http://192.168.0.113/relay/0/?turn=off') is_on = False else: button.config(image = on) label.config(text = "Switch is On", fg = "green", bg='white') requests.post('http://192.168.0.113/relay/0/?turn=on') is_on = True if is_on == True: button = Button(ws, image = on, bg='white', bd = 0, command = Switch) button.pack(pady = 30) label = Label(ws, text = "The Switch Is On!", bg='white', fg = "green", font = ("Helvetica", 32)) label.pack(pady = 20) else: button = Button(ws, image = off, bg='white', bd = 0, command = Switch) button.pack(pady = 30) label = Label(ws, text = "The Switch Is Off!", bg='white', fg = "grey", font = ("Helvetica", 32)) label.pack(pady = 20) ws.mainloop()