Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP/IP Gateway #36

Merged
merged 11 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python_banyan/banyan_base_aio/banyan_base_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def __init__(self, back_plane_ip_address=None, subscriber_port='43125',
# fix for "not implemented" bugs in Python 3.8
if sys.platform == 'win32':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
self.event_loop = asyncio.get_event_loop()
self.event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.event_loop)

# if using numpy apply the msgpack_numpy monkey patch
if numpy:
Expand Down
26 changes: 26 additions & 0 deletions python_banyan/utils/tcp_gateway/Running_Demos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Running The Demos

There are two demos available. Currently, data can only be sent in one
direction at a time since I have not figured out how to perform concurrency in
MicroPython. The threading
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
the concurrency issue. I have yet to have a chance to see if the code works.

## Sending Messages To The Local Simulated Server From The Pico
Here are the steps used:

1. Start the backplane.
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.
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)
4. Start the [tcp_gateway.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/tcp_gateway.py)

You should see the messages if you look at the console window for sim_messages_from_pico.py.


## Sending Messages From the Local Simulated Server To The Pico
1. Start the backplane.
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.
3. Start the [tcp_gateway.py](https://github.com/MrYsLab/python_banyan/blob/tcp_gateway/python_banyan/utils/tcp_gateway/tcp_gateway.py)
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.

Check the Thonny shell for incoming messages.
Empty file.
43 changes: 43 additions & 0 deletions python_banyan/utils/tcp_gateway/micropython_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import network, rp2, time
import socket
import umsgpack
from machine import Pin

led = Pin("LED", Pin.OUT)
led.on()

# set your WiFi Country
rp2.country('US')

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)

wlan.connect('YOUR_SSID', 'YOUR_PASSWORD')


while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
time.sleep(1)

print(wlan.ifconfig())

# create a socket server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

addr = ("",31335)
serversocket.bind(addr)
serversocket.listen()
(clientsocket, address) = serversocket.accept()
print((clientsocket, address))
led.off()
while True:
data = clientsocket.recv(1024)
if data:
z = umsgpack.loads(data)
print(z)



Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import network
import rp2
import time
import socket
import umsgpack
from machine import Pin

led = Pin("LED", Pin.OUT)
led.on()

# set your Wi-Fi Country
rp2.country('US')

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# set power mode to get Wi-Fi power-saving off (if needed)
wlan.config(pm=0xa11140)

wlan.connect('A-Net', 'Sam2Curly')

while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
time.sleep(1)

print(wlan.ifconfig())

# create a socket server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

addr = ("", 31335)
server_socket.bind(addr)
server_socket.listen()
(client_socket, address) = server_socket.accept()
print((client_socket, address))
led.off()

count = 0
while True:
payload = {'pico_data': count}
count += 1
packed = umsgpack.dumps(payload)

# get the length of the payload and express as a bytearray
p_length = bytearray(len(packed).to_bytes(1, 'big'))

# append the length to the packed bytarray
p_length.extend(packed)

# convert from bytearray to bytes
packed = bytes(p_length)

print(f'packed: {packed} length: {len(packed)}')
# bpacked = bytearray(packed)
# l = len(packed)
# print(f'l = {l}')
# lenn = bytearray(l)
# z = lenn.extend(bpacked)
# q = bytes(z)
# print(f'z = {z}')
client_socket.sendall(packed)
time.sleep(.01)
print(f'sending {count}')
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import network, rp2, time
import socket
import umsgpack
from machine import Pin

led = Pin("LED", Pin.OUT)
led.on()

# set your WiFi Country
rp2.country('US')

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

# set power mode to get WiFi power-saving off (if needed)
wlan.config(pm = 0xa11140)

wlan.connect('A-Net', 'Sam2Curly')


while not wlan.isconnected() and wlan.status() >= 0:
print("Waiting to connect:")
time.sleep(1)

print(wlan.ifconfig())

# create a socket server
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

addr = ("",31335)
serversocket.bind(addr)
serversocket.listen()
(clientsocket, address) = serversocket.accept()
print((clientsocket, address))
led.off()

count = 0
while True:
data = clientsocket.recv(1024)
if data:
z = umsgpack.loads(data)
print(z)
# payload = {'from_the_pico': count}
# count += 1
# packed = umsgpack.dumps(payload)
# clientsocket.sendall(packed)
# print('sending')



Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# zmq_banyan_local_station_simulator.py

# requires Banyan 'backplane'

"""
Python-Banyan Providence:

Copyright (c) 2018-2019 Alan Yorinks All right reserved.

Python Banyan is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation; either
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

"""

import time

from python_banyan.banyan_base import BanyanBase


class Bpub(BanyanBase):

def __init__(self):

# initialize the base class
super(Bpub, self).__init__(process_name='Bpub')
self.set_subscriber_topic('from_pico')

# send a message
# self.publish_payload({'from_banyan': 'hello_world'}, 'figura')
# self.count = 0

# get the reply messages
try:
self.receive_loop()
except KeyboardInterrupt:
self.clean_up()
sys.exit(0)

while True:
# send command to turn Led_0 ON
# self.publish_payload({'Led_0': self.count}, 'figura')
# self.count += 1
time.sleep(0.01)
# print('published ON')

# send command to turn Led_0 OFF
# self.publish_payload({'Led_0': self.count}, 'figura')
# self.count += 1

# time.sleep(0.01)
# print('published OFF')

def incoming_message_processing(self, topic, payload):
"""
Process incoming messages received from the echo client
:param topic: Message Topic string
:param payload: Message Data
"""

# When a message is received and its number is zero, finish up.
print(f'topic: {topic} payload: {payload}')


b = Bpub()
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# zmq_banyan_local_station_simulator.py

# requires Banyan 'backplane'

"""
Python-Banyan Providence:

Copyright (c) 2018-2019 Alan Yorinks All right reserved.

Python Banyan is free software; you can redistribute it and/or
modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
Version 3 as published by the Free Software Foundation; either
or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

"""


import time

from python_banyan.banyan_base import BanyanBase


class Bpub(BanyanBase):


def __init__(self):

# initialize the base class
super(Bpub, self).__init__(process_name='Bpub')

# send a message
self.publish_payload({'from_banyan': 'hello_world'}, 'figura')
self.count = 0

while True:

# send command to turn Led_0 ON
self.publish_payload({'Led_0': self.count}, 'figura')
self.count += 1
time.sleep(0.01)
#print('published ON')

# send command to turn Led_0 OFF
self.publish_payload({'Led_0': self.count}, 'figura')
self.count += 1

time.sleep(0.01)
#print('published OFF')

# exit
self.clean_up()


b = Bpub()
Loading