-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python Files to get local ip with port serial
- Loading branch information
1 parent
06c5c2a
commit 717a6d6
Showing
2 changed files
with
57 additions
and
0 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 |
---|---|---|
@@ -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) |
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,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 |