im trying to connect my esp32 microcontroller via wifi. But it doesnt work. I followed the tutorial on https://docs.micropython.org/en/latest/esp32/quickref.html#networking step by step and watched a lot of youtubevideos.
my code looks like this:
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)
print(wlan.scan())
wlan.connect('my_wlan_ssid', 'my_wlan_password')
print(wlan.isconnected())
print("Wlan is connected: ", wlan.isconnected())
print("My Wlan config: ", wlan.ifconfig())
here i add a picture from my command linde from the Thonny editor
Command line from Thonny editor
The funny thing is, the webinterface from my router shows me the connection to the esp32 controller with his ip-address.
I also tried it with a mobile-hotspot from my mobilephone. My mobilephone shows me the connection with the esp32, but the esp32 doesn't recognize the wlan connection
So why is it so?
Do i something wrong?
Seems like you are not letting the network actually make the connection.
wlan.connect('my_wlan_ssid', 'my_wlan_password')
takes time and as shown in the linked reference, wlan.isconnected() should be called in a while loop to ensure that it exits only if it is connected. (you could of-course do better management)
So how about you do this:
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('<essid>', '<password>')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
As per your own link
Related
I am working on IB Gateway and want to get the historical data.
As i have completed the steps on IB Gateway software to enable the API.
I am using python notebook for this.
For now i am running this code and i am able to import the given library but rest of the code giving me this error. Important thing is connection is established as I have mention client id 1. then it is created and can be seen on IB Gateway application.
My code is here.
from ib_insync import *
#util.startLoop() # uncomment this line when in a notebook
ib = IB()
ib.connect('127.0.0.1', 5021, clientId=1)
bars = ib.reqHistoricalData(
contract=Stock('TSLA', 'SMART', 'USD'),
endDateTime='',
durationStr='30 D',
barSizeSetting='1 hour',
whatToShow='TRADES',
useRTH=True)
print(bars)
Here is the error.
Peer closed connection
clientId 1 already in use?
API connection failed: CancelledError()
As i am using notebook if i uncomment the second line (util.startLoop()) it adds one more error about timeout..
Need help to get this done.
Big Thanks
Assign a different clientID to this connecion:
ib.connect('127.0.0.1', 5021, clientId=2)
Apparently you already have another connection with clientId=1.
I'm trying to connect to an RN42, module through python. When the RN42 pairs with W10 it creates two virtual COM ports(outgoing and incoming). I need to connect to the outgoing port.
I'm trying to do this automatically. I've tried:
import serial
import serial.tools.list_ports as port_lst
ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
if 'Bluetooth' in p.description:
bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)
I thought that the first port was usually the outgoing one, but I've paired the module to another computer, and this didn't apply (the second port was the outgoing one). Is there a way to find out the direction of the COM ports?
Thanks!!!
Although this is an antique question, I have been searching for the answer to this for some time myself and since I finally figured it out I wanted others to be able to find the answer. With help from a blog entry at in the hand and its accompanying gist:
The trick is to acquire the hwid using pySerial, then parse the address. The incoming port in a pair has an address of zero and the outgoing port has a nonzero address. Here is some ugly Python code that decodes it:
import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
if "BTHENUM" in p.hwid:
start_of_address=p.hwid.rfind("&")
end_of_address=p.hwid.rfind("_")
address=p.hwid[start_of_address+1:end_of_address]
if int(address,16)==0:
port_type="incoming"
else:
port_type="outgoing"
print(p.hwid)
print(p.name, address, port_type)
And the output:
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\7&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\7&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing
I am currently working with a Pycom device and try to connect it to an IoT platform (more precisely, Adafruit IO). I would like to make the platform and my device communicate. It used to work perfectly good, I could publish and subscribe using MQTT to topics/widgets configured on the website but lately, I have been given this error message when trying to connect to Adafruit with this protocol : OSError: Available Interfaces are down. I do not know why all of a sudden, this happens and I have no idea how to deal with. Sometimes, after a while or after numerous attempts, it works again but I would like to know more precisely what this is due to.
import umqtt
from umqtt import MQTTClient
import ubinascii
import micropython
import time
import machine
import pycom
pycom.heartbeat(False)
IO_SERVER = "io.adafruit.com"
AIO_SERVER = "io.adafruit.com"
AIO_PORT = 1883
AIO_USER = "user"
AIO_KEY = "key"
AIO_CLIENT_ID = ubinascii.hexlify(machine.unique_id()) # Can be anything
client = MQTTClient(AIO_CLIENT_ID, AIO_SERVER, AIO_PORT, AIO_USER, AIO_KEY)
import network
from network import WLAN
wlan=WLAN(mode=WLAN.STA)
pw='pw'
nets=wlan.scan()
for net in nets:
if net.ssid == 'myssid':
wlan.connect(net.ssid,auth=(None,pw),timeout=5000)
if wlan.isconnected() == True:
pycom.rgbled(0x007f00)
else:
pycom.rgbled(0x7f0000)
client.connect()
pycom.rgbled(0x7f7f00)
I used the umqtt module located here: https://github.com/micropython/micropython-lib/blob/master/umqtt.simple/umqtt/simple.py. I can connect to my wifi with no problem, the error happens at client.connect().
I had faced similar issue, what I realised is that there is some time required for the client to connect.
Add 2 sec delay before connect & like 10 secs
Today I installed NodeMCU on one ESP8266 module.
I made a simple telnet server (logging in to the AP is already done and connected)
srv=net.createServer(net.TCP,7200)
srv:listen(23,function(conn)
conn:on("receive", function(conn,telnetdata)
print(telnetdata)
conn:send("Got it\r\n")
end)
conn:on("sent",function(conn)
print("[Sent]");
--conn:close();
--collectgarbage();
end)
end)
I can telnet into the ESP8266 using Putty and I can send static data back to Putty (conn:send("Got it\r\n")).
What I want to do is to send data to Putty from the esp serial port (UART). I've googled for an example without success. BTW I'm using ESPlorer to program the ESP and to send data back to putty.
Instead of using conn:send("Got it\r\n") I want to dynamically type the response. I've tried uart:on callback but I haven't fully understood how it works.
NodeMCU has an "official" Telnet example in the repository. However, that one is also WiFi/socket-based.
If you're connected to the device through ESPlorer (i.e. serial/UART) you can't open another serial connection. See https://stackoverflow.com/a/36779799/131929 for details.
Marcel thank you for your answer.
The link posted is not exactly what I need but it offered good info.
I finally came up with this code that works as a simple bridge between putty and a micro controller (arduino mega 2560 in this case)
socket = net.createServer(net.TCP,7200)
socket:listen(23,function(c)
c:on("receive",function(c,l)
uart.write(0, l)
end)
c:on("disconnection",function(c)
-- not yet implemented
end)
uart.on("data", 0,
function(data)
c:send(data)
end, 0)
end)
I have a software that prints out to a thermal printer connected to the comm port, the code below works well.
Open "COM3:" For Output Access Write As #1
Print #1, Space(8) & "FRAKAS LTD"
Print #1, Space(7) & "P.O. Box 7678 SHIMONI"
...
...
Close #1
Now I want to print on the same comm port but from a different computer on the network, is it possible?
You can redirect the serial port across the network by running software at each end. There is some info on Wikipedia here about some solutions - I haven't tried any.
We have used ethernet to serial converters from Moxa - these redirect a local COM port to a remote one without any PC at the remote end. One example is here
if the printer is availabe then you can use the following to prepare text to be printed :
printer.print "text"
to start the actual printing you use the following :
printer.enddoc