Skip to content

Commit

Permalink
update python examples for python3
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Jun 17, 2024
1 parent 9fde2d4 commit f463226
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
11 changes: 3 additions & 8 deletions examples/addons/mqttLogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -29,7 +25,6 @@
sock.bind(server_address)

while True:

data, address = sock.recvfrom(2048)

print("received {} bytes from {}".format(len(data), address))
Expand All @@ -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)
19 changes: 9 additions & 10 deletions examples/clients/PythonClient/pyClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 #########

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f463226

Please sign in to comment.