Skip to content

Commit

Permalink
Create WiFiReader and update driver
Browse files Browse the repository at this point in the history
  • Loading branch information
dasuntheekshanagit committed Sep 20, 2023
1 parent b8f6d11 commit dbb8e1a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 13 deletions.
45 changes: 35 additions & 10 deletions Driver/main.py
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
10 changes: 7 additions & 3 deletions Driver/src/BluetoothReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def __init__(self):
self.client = None

async def scan(self):
self.devices = await BleakScanner.discover(return_adv=True)
for device,data in self.devices.items():
print(f"Name: {data[1].local_name} MAC: {device} RSSI: {data[1].rssi}")
try:
self.devices = await BleakScanner.discover(return_adv=True)
for device,data in self.devices.items():
print(f"Name: {data[1].local_name} MAC: {device} RSSI: {data[1].rssi}")
except Exception as e:
print(f"Error Turn On Bluetooth: {str(e)}")

async def isavalable(self):
await self.scan()
Expand All @@ -44,6 +47,7 @@ async def connect(self):
except Exception as e:
print(f"Error Connecting: {str(e)}")
return False
return True

async def disconnect(self):
try:
Expand Down
1 change: 1 addition & 0 deletions Driver/src/SerialReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def connect(self):
if self.isavalable():
self.ser = serial.Serial(self.port, 9600)
print(f"Key Board Connected via : {self.port}")
self.ser.write(0)
return True
return False
except Exception as e:
Expand Down
68 changes: 68 additions & 0 deletions Driver/src/WifiReader.py
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 modified Driver/src/__pycache__/BluetoothReader.cpython-311.pyc
Binary file not shown.
Binary file modified Driver/src/__pycache__/SerialReader.cpython-311.pyc
Binary file not shown.
Binary file added Driver/src/__pycache__/WifiReader.cpython-311.pyc
Binary file not shown.

0 comments on commit dbb8e1a

Please sign in to comment.