-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TCP/IP Gateway
- Loading branch information
Showing
11 changed files
with
615 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
||
|
63 changes: 63 additions & 0 deletions
63
python_banyan/utils/tcp_gateway/pico_micropython_scripts/messages_from_pico.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
50 changes: 50 additions & 0 deletions
50
python_banyan/utils/tcp_gateway/pico_micropython_scripts/messages_to_pico.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
||
|
||
|
74 changes: 74 additions & 0 deletions
74
python_banyan/utils/tcp_gateway/simulated_local_station/sim_messages_from_pico.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
62 changes: 62 additions & 0 deletions
62
python_banyan/utils/tcp_gateway/simulated_local_station/sim_messages_to_pico.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.