Skip to content

Commit

Permalink
[ADDED][PYTHONDEV][OWNER]
Browse files Browse the repository at this point in the history
Python Files to get local ip with port serial
  • Loading branch information
JohnKun136NVCP committed Nov 6, 2023
1 parent 06c5c2a commit 717a6d6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Introduccion a IoT/Proyecto/pyfiles/getips.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import serial
import serialpports as serport
import time
import os.path
def getIPs():
ips = []
try:
nameport = serport.ports()
esp = serial.Serial(nameport,115200)
print("REBOOT your ESP32 to get IPS")
while (len(ips)<1):
line = esp.readline().decode('utf-8').rstrip()
ips.append(line)
esp.close()
return ips
except:
return []

def generateTxtFile(ipSta):
path = '~/.ips/ips.txt'
if(os.path.isfile(path)!=True):
with open("ips.txt","a") as file:
file.write(ipSta)
else:
print("Do you want to overwrite it? [Y/N]")
option = input()
if(option=="Y" or option=="yes" or option == "YES"):
with open("ips.txt","w") as file:
file.write(ipSta)
else:
print("Nothing to do here")


while (True):
iplist = getIPs()
if(iplist==[]):
time.sleep(0.5)
print("Fist, connect your esp32 to port serial,please")
elif(iplist!=[]):
ipsta = iplist[0]
print("La IP publica STA es: ",ipsta)
print("Now you can unplugged your ESP32")
break
generateTxtFile(ipsta)
13 changes: 13 additions & 0 deletions Introduccion a IoT/Proyecto/pyfiles/serialpports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import serial
import serial.tools.list_ports as list_ports
def ports():
# code by michalmonday onhttps://github.com/michalmonday/CSV-Parser-for-Arduino/tree/master/examples/reading_from_computer_python/arduino_serial.py
try:
inputport = []
for i, p in enumerate(list_ports.comports()):
print(f'{i}. device={p.device} name={p.name} description={p.description} manufacturer={p.manufacturer}')
inputport.append(p.device)
nameport = int(input("Choose your port: "))
return inputport[nameport]
except:
return -1

0 comments on commit 717a6d6

Please sign in to comment.