generated from cepdnaclk/eYY-XXX-project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8f6d11
commit dbb8e1a
Showing
7 changed files
with
111 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,42 @@ | ||
import asyncio | ||
from modulefinder import AddPackagePath | ||
from pkgutil import get_loader | ||
from site import addpackage | ||
|
||
from src.SerialReader import * | ||
from src.BluetoothReader import * | ||
from src.WifiReader import * | ||
|
||
mode = 0 | ||
agent = None | ||
|
||
if __name__ == "__main__": | ||
#agent = ESPSerialReader() | ||
#async def bluetooth(): | ||
#agent = ESPBluetoothReader() | ||
#await agent.connect() | ||
#while True: | ||
#data = await agent.read() | ||
#if data: | ||
#print(data) | ||
def run(): | ||
global agent | ||
agent.connect() | ||
while True: | ||
data = agent.read() | ||
if data: | ||
print(data) | ||
|
||
async def runbluetooth(): | ||
global agent | ||
agent = ESPBluetoothReader() | ||
await agent.connect() | ||
while True: | ||
data = await agent.read() | ||
if data: | ||
print(data) | ||
""" | ||
if mode == 0: | ||
agent = ESPSerialReader() | ||
agent.connect() | ||
elif mode == 1: | ||
asyncio.run(runbluetooth()) | ||
elif mode == 2: | ||
agent = ESPWifiReader("0.0.0.0") | ||
run()""" | ||
agent = ESPSerialReader() | ||
agent.connect() | ||
|
||
|
||
#asyncio.run(bluetooth()) | ||
agent |
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
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,68 @@ | ||
# Author : Dasun Theekshana | ||
# Date : 19/09/2023 | ||
# File : SerialReader.py | ||
|
||
import socket | ||
|
||
#host = "0.0.0.0" | ||
port = 8888 | ||
|
||
class ESPWifiReader: | ||
def __init__(self, host): | ||
# Listen on all available network interfaces | ||
self.server = None | ||
self.clientsocket = None | ||
self.clientip = None | ||
self.host = host | ||
|
||
def scan(self): | ||
# Get the hostname of the local machine | ||
hostname = socket.gethostname() | ||
|
||
# Get the IP address associated with the hostname | ||
ip = socket.gethostbyname(hostname) | ||
|
||
print(f"Hostname: {hostname} IP Address: {ip}") | ||
return ip | ||
|
||
def connect(self): | ||
try: | ||
ip = self.scan() | ||
|
||
# Create a socket | ||
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
|
||
# Bind the socket to the address and port | ||
self.server.bind((self.host, port)) | ||
|
||
# Listen for incoming connections | ||
self.server.listen(5) | ||
|
||
print(f"Connected with: {ip} ({port})") | ||
return True | ||
|
||
except Exception as e: | ||
print(f"Error Connecting: {str(e)}") | ||
return False | ||
|
||
def disconnect(self): | ||
try: | ||
if self.server: | ||
self.clientsocket.close() | ||
self.server.close() | ||
return True | ||
return False | ||
except Exception as e: | ||
print(f"Error Disonnecting: {str(e)}") | ||
return False | ||
|
||
def read(self): | ||
try: | ||
self.clientsocket, self.clientip = self.server.accept() | ||
data = self.clientsocket.recv(1) | ||
if data: | ||
return data.decode('utf-8') | ||
return False | ||
except Exception as e: | ||
print(f"Error Reading: {str(e)}") | ||
return False |
Binary file not shown.
Binary file not shown.
Binary file not shown.