From f4632267a41487746da5fb768bb696629a538722 Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Mon, 17 Jun 2024 16:06:33 -0700 Subject: [PATCH] update python examples for python3 --- examples/addons/mqttLogger.py | 11 +++-------- examples/clients/PythonClient/pyClient.py | 19 +++++++++---------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/examples/addons/mqttLogger.py b/examples/addons/mqttLogger.py index 682b3a2..d3e5bbc 100644 --- a/examples/addons/mqttLogger.py +++ b/examples/addons/mqttLogger.py @@ -10,13 +10,9 @@ RF24NetworkFrame frame = RF24NetworkFrame(header, buf, size); gw.sendUDP(mesh.getNodeID(header.from_node), frame); """ -import paho.mqtt.client as mqtt -import socket -try: #python 2 to 3 hack - unicode("") # all strings are unicode in python3 -except NameError: - unicode = str # does the same thing as python2's builtin unicode() +import socket +import paho.mqtt.client as mqtt ### Setup the MQTT host IP & topic to publish to mqttHost = "10.10.2.2" @@ -29,7 +25,6 @@ sock.bind(server_address) while True: - data, address = sock.recvfrom(2048) print("received {} bytes from {}".format(len(data), address)) @@ -40,6 +35,6 @@ # TODO: Sort, Display and Analyze the data mqttc = mqtt.Client() mqttc.connect(mqttHost, 1883) - data = unicode(data, errors="replace") + data = data.decode(errors="replace") mqttc.publish(topic, data) mqttc.loop(2) diff --git a/examples/clients/PythonClient/pyClient.py b/examples/clients/PythonClient/pyClient.py index e49d19c..6f26bca 100644 --- a/examples/clients/PythonClient/pyClient.py +++ b/examples/clients/PythonClient/pyClient.py @@ -5,10 +5,11 @@ Turn a light on in the evening and off in the morning according to a schedule """ -import urllib2 from datetime import datetime -import time import syslog +import time +from urllib.error import HTTPError, URLError +from urllib.request import urlopen ######### Configuration ######### @@ -23,21 +24,19 @@ lightState = 2 while 1: - # Get the current hour currentHour = datetime.now().hour # Check to see if the system should be off if (currentHour >= scheduleON or currentHour < scheduleOFF) and lightState != 1: - result = 0 # Connect to our sensor at 10.10.3.44:1000/ and request OFF try: - response = urllib2.urlopen(requestOFF, None, 15) # 15 second time-out + response = urlopen(requestOFF, None, 15) # 15 second time-out result = response.getcode() - except urllib2.HTTPError, e: + except HTTPError as e: syslog.syslog("HTTPError = " + str(e.code)) - except urllib2.URLError, e: + except URLError as e: syslog.syslog("URLError = " + str(e.reason)) except Exception: import traceback @@ -54,11 +53,11 @@ result = 0 try: - response = urllib2.urlopen(requestON, None, 15) # 15 second time-out + response = urlopen(requestON, None, 15) # 15 second time-out result = response.getcode() - except urllib2.HTTPError, e: + except HTTPError as e: syslog.syslog("HTTPError = " + str(e.code)) - except urllib2.URLError, e: + except URLError as e: syslog.syslog("URLError = " + str(e.reason)) except Exception: import traceback