Skip to content

Commit 461bd2f

Browse files
authored
Merge pull request #36
TCP/IP Gateway
2 parents 2fd3a8a + ba41cca commit 461bd2f

11 files changed

Lines changed: 615 additions & 3 deletions

File tree

python_banyan/banyan_base_aio/banyan_base_aio.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def __init__(self, back_plane_ip_address=None, subscriber_port='43125',
103103
# fix for "not implemented" bugs in Python 3.8
104104
if sys.platform == 'win32':
105105
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
106-
self.event_loop = asyncio.get_event_loop()
106+
self.event_loop = asyncio.new_event_loop()
107+
asyncio.set_event_loop(self.event_loop)
107108

108109
# if using numpy apply the msgpack_numpy monkey patch
109110
if numpy:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Running The Demos
2+
3+
There are two demos available. Currently, data can only be sent in one
4+
direction at a time since I have not figured out how to perform concurrency in
5+
MicroPython. The threading
6+
library is said to be experimental. However, I did locate [this code](https://github.com/fadushin/esp8266/blob/790958fa332592c80a0f81f25cdaa9513d596f64/micropython/uhttpd/uhttpd/__init__.py#L354) which may solve
7+
the concurrency issue. I have yet to have a chance to see if the code works.
8+
9+
## Sending Messages To The Local Simulated Server From The Pico
10+
Here are the steps used:
11+
12+
1. Start the backplane.
13+
2. Start the [messages_from_pico.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/pico_micropython_scripts/messages_from_pico.py) MicroPython script.
14+
3. Start [sim_messages_from_pico.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/simulated_local_station/sim_messages_from_pico.py)
15+
4. Start the [tcp_gateway.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/tcp_gateway.py)
16+
17+
You should see the messages if you look at the console window for sim_messages_from_pico.py.
18+
19+
20+
## Sending Messages From the Local Simulated Server To The Pico
21+
1. Start the backplane.
22+
2. Start the [messages_to_pico.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/pico_micropython_scripts/messages_to_pico.py) MicroPython script.
23+
3. Start the [tcp_gateway.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/tcp_gateway.py)
24+
4. Start the [sim_messages_to_pico.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/simulated_local_station/sim_messages_to_pico.py) script.
25+
26+
Check the Thonny shell for incoming messages.

python_banyan/utils/tcp_gateway/__init__.py

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import network, rp2, time
2+
import socket
3+
import umsgpack
4+
from machine import Pin
5+
6+
led = Pin("LED", Pin.OUT)
7+
led.on()
8+
9+
# set your WiFi Country
10+
rp2.country('US')
11+
12+
wlan = network.WLAN(network.STA_IF)
13+
wlan.active(True)
14+
15+
# set power mode to get WiFi power-saving off (if needed)
16+
wlan.config(pm = 0xa11140)
17+
18+
wlan.connect('YOUR_SSID', 'YOUR_PASSWORD')
19+
20+
21+
while not wlan.isconnected() and wlan.status() >= 0:
22+
print("Waiting to connect:")
23+
time.sleep(1)
24+
25+
print(wlan.ifconfig())
26+
27+
# create a socket server
28+
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29+
30+
addr = ("",31335)
31+
serversocket.bind(addr)
32+
serversocket.listen()
33+
(clientsocket, address) = serversocket.accept()
34+
print((clientsocket, address))
35+
led.off()
36+
while True:
37+
data = clientsocket.recv(1024)
38+
if data:
39+
z = umsgpack.loads(data)
40+
print(z)
41+
42+
43+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import network
2+
import rp2
3+
import time
4+
import socket
5+
import umsgpack
6+
from machine import Pin
7+
8+
led = Pin("LED", Pin.OUT)
9+
led.on()
10+
11+
# set your Wi-Fi Country
12+
rp2.country('US')
13+
14+
wlan = network.WLAN(network.STA_IF)
15+
wlan.active(True)
16+
17+
# set power mode to get Wi-Fi power-saving off (if needed)
18+
wlan.config(pm=0xa11140)
19+
20+
wlan.connect('A-Net', 'Sam2Curly')
21+
22+
while not wlan.isconnected() and wlan.status() >= 0:
23+
print("Waiting to connect:")
24+
time.sleep(1)
25+
26+
print(wlan.ifconfig())
27+
28+
# create a socket server
29+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
30+
31+
addr = ("", 31335)
32+
server_socket.bind(addr)
33+
server_socket.listen()
34+
(client_socket, address) = server_socket.accept()
35+
print((client_socket, address))
36+
led.off()
37+
38+
count = 0
39+
while True:
40+
payload = {'pico_data': count}
41+
count += 1
42+
packed = umsgpack.dumps(payload)
43+
44+
# get the length of the payload and express as a bytearray
45+
p_length = bytearray(len(packed).to_bytes(1, 'big'))
46+
47+
# append the length to the packed bytarray
48+
p_length.extend(packed)
49+
50+
# convert from bytearray to bytes
51+
packed = bytes(p_length)
52+
53+
print(f'packed: {packed} length: {len(packed)}')
54+
# bpacked = bytearray(packed)
55+
# l = len(packed)
56+
# print(f'l = {l}')
57+
# lenn = bytearray(l)
58+
# z = lenn.extend(bpacked)
59+
# q = bytes(z)
60+
# print(f'z = {z}')
61+
client_socket.sendall(packed)
62+
time.sleep(.01)
63+
print(f'sending {count}')
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import network, rp2, time
2+
import socket
3+
import umsgpack
4+
from machine import Pin
5+
6+
led = Pin("LED", Pin.OUT)
7+
led.on()
8+
9+
# set your WiFi Country
10+
rp2.country('US')
11+
12+
wlan = network.WLAN(network.STA_IF)
13+
wlan.active(True)
14+
15+
# set power mode to get WiFi power-saving off (if needed)
16+
wlan.config(pm = 0xa11140)
17+
18+
wlan.connect('A-Net', 'Sam2Curly')
19+
20+
21+
while not wlan.isconnected() and wlan.status() >= 0:
22+
print("Waiting to connect:")
23+
time.sleep(1)
24+
25+
print(wlan.ifconfig())
26+
27+
# create a socket server
28+
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29+
30+
addr = ("",31335)
31+
serversocket.bind(addr)
32+
serversocket.listen()
33+
(clientsocket, address) = serversocket.accept()
34+
print((clientsocket, address))
35+
led.off()
36+
37+
count = 0
38+
while True:
39+
data = clientsocket.recv(1024)
40+
if data:
41+
z = umsgpack.loads(data)
42+
print(z)
43+
# payload = {'from_the_pico': count}
44+
# count += 1
45+
# packed = umsgpack.dumps(payload)
46+
# clientsocket.sendall(packed)
47+
# print('sending')
48+
49+
50+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# zmq_banyan_local_station_simulator.py
2+
3+
# requires Banyan 'backplane'
4+
5+
"""
6+
Python-Banyan Providence:
7+
8+
Copyright (c) 2018-2019 Alan Yorinks All right reserved.
9+
10+
Python Banyan is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12+
Version 3 as published by the Free Software Foundation; either
13+
or (at your option) any later version.
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
General Public License for more details.
18+
19+
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
20+
along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
23+
"""
24+
25+
import time
26+
27+
from python_banyan.banyan_base import BanyanBase
28+
29+
30+
class Bpub(BanyanBase):
31+
32+
def __init__(self):
33+
34+
# initialize the base class
35+
super(Bpub, self).__init__(process_name='Bpub')
36+
self.set_subscriber_topic('from_pico')
37+
38+
# send a message
39+
# self.publish_payload({'from_banyan': 'hello_world'}, 'figura')
40+
# self.count = 0
41+
42+
# get the reply messages
43+
try:
44+
self.receive_loop()
45+
except KeyboardInterrupt:
46+
self.clean_up()
47+
sys.exit(0)
48+
49+
while True:
50+
# send command to turn Led_0 ON
51+
# self.publish_payload({'Led_0': self.count}, 'figura')
52+
# self.count += 1
53+
time.sleep(0.01)
54+
# print('published ON')
55+
56+
# send command to turn Led_0 OFF
57+
# self.publish_payload({'Led_0': self.count}, 'figura')
58+
# self.count += 1
59+
60+
# time.sleep(0.01)
61+
# print('published OFF')
62+
63+
def incoming_message_processing(self, topic, payload):
64+
"""
65+
Process incoming messages received from the echo client
66+
:param topic: Message Topic string
67+
:param payload: Message Data
68+
"""
69+
70+
# When a message is received and its number is zero, finish up.
71+
print(f'topic: {topic} payload: {payload}')
72+
73+
74+
b = Bpub()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# zmq_banyan_local_station_simulator.py
2+
3+
# requires Banyan 'backplane'
4+
5+
"""
6+
Python-Banyan Providence:
7+
8+
Copyright (c) 2018-2019 Alan Yorinks All right reserved.
9+
10+
Python Banyan is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12+
Version 3 as published by the Free Software Foundation; either
13+
or (at your option) any later version.
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
General Public License for more details.
18+
19+
You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
20+
along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
23+
"""
24+
25+
26+
import time
27+
28+
from python_banyan.banyan_base import BanyanBase
29+
30+
31+
class Bpub(BanyanBase):
32+
33+
34+
def __init__(self):
35+
36+
# initialize the base class
37+
super(Bpub, self).__init__(process_name='Bpub')
38+
39+
# send a message
40+
self.publish_payload({'from_banyan': 'hello_world'}, 'figura')
41+
self.count = 0
42+
43+
while True:
44+
45+
# send command to turn Led_0 ON
46+
self.publish_payload({'Led_0': self.count}, 'figura')
47+
self.count += 1
48+
time.sleep(0.01)
49+
#print('published ON')
50+
51+
# send command to turn Led_0 OFF
52+
self.publish_payload({'Led_0': self.count}, 'figura')
53+
self.count += 1
54+
55+
time.sleep(0.01)
56+
#print('published OFF')
57+
58+
# exit
59+
self.clean_up()
60+
61+
62+
b = Bpub()

0 commit comments

Comments
 (0)