hi im having error with this code but it runs in python shell could any body help me
from machine import Pin
import time
import network
import urequests
p0 = Pin(0,Pin.OUT)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect('ssid', 'pass')
response = urequests.get('http://jsonplaceholder.typicode.com/albums/1')
while True:
ans = response.json()['userId']
p0.value(1)
time.sleep(1)
p0.off()
time.sleep(1)
print('ok')
and this is the error:
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
File "urequests.py", line 108, in get
File "urequests.py", line 53, in request
OSError: -202
Your issue (my guess) is that you begin to urequest.get() without connected to WiFi. Create function that do wifi connection and call it
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Explain: wlan.connect() is asynchronous function and you have to wait, while it connects to wifi and only then continue with urequest.get()
Related
This is a Python program to receive the data from an XBee module using the python-xBee library. I have installed both the xbee and pyserial modules.
import serial
from xbee import XBee
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
xbee = XBee(serial_port)
while True:
try:
print xbee.wait_read_frame()
except KeyboardInterrupt:
break
serial_port.close()
But when I run this and any kind of program with serial port, this is the error I am getting:
Traceback (most recent call last):
File "C:/Users/Manurajeev/PycharmProjects/untitled/one.py", line 4, in
<module>
serial_port = serial.Serial('/dev/ttyUSB0', 9600)
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 31, in
__init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Python27\lib\site-packages\serial\serialutil.py", line 240, in
__init__
self.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 62, in
open
raise SerialException("could not open port {!r}:
{!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port '/dev/ttyUSB0':
WindowsError(3, 'The system cannot find the path specified.')
Process finished with exit code 1
I don't understand what the problem is. I tried everything, but the same error keeps popping up every time.
In Linux, check the permissions on the tty device (ls -l /dev/ttyUSB0) to ensure that you have read/write access to it. Note that it might have a different name.
For Windows, have you been able to open COM5 with a terminal emulator and send/receive data on the XBee? Do you still have it open in another program when you're trying to open it in Python? Only one program can access a COM port at a time.
I have this python code which getting data from socket's and printing to console. But in working process I getting this error code
Traceback (most recent call last):
File "server.py", line 1, in <module>
import ssl, socket
File "/home/ssl.py", line 20, in <module>
returned from time.time())
AttributeError: 'module' object has no attribute 'wrap_socket'
maybe somebody has any idea how I can fix this issue?
import ssl, socket
sock = ssl.wrap_socket(socket.socket(), 'server.key', 'server.crt', True)
sock.bind( ('', 443) )
sock.listen(10)
while True:
conn, addr = sock.accept()
data = conn.recv(4)
print data
thank you
You most likely have a file named ssl.py in the same directory, as indicated by this part of the error:
File "/home/ssl.py", line 20, in
returned from time.time())
and the script is trying to import that instead of the system level module. If you cannot change the name of that file, try using absolute and relative imports.
I have the following script:
#!/usr/bin/env python
import nxt.locator
from nxt.motor import *
def spin_around(b):
m_left = Motor(b, PORT_B)
m_left.turn(100, 360)
m_right = Motor(b, PORT_C)
m_right.turn(-100, 360)
print("Script Starting")
b = nxt.locator.find_one_brick()
spin_around(b)
I have installed pyUSB and libUSB. I run the script with python spin.py
However, when I run the script I get the following exception:
Script Starting
Traceback (most recent call last):
File "spin.py", line 14, in <module>
spin_around(b)
File "spin.py", line 8, in spin_around
m_left.turn(100, 360)
File "/Library/Python/2.7/site-packages/nxt/motor.py", line 211, in turn
raise BlockedException("Blocked!")
nxt.motor.BlockedException: Blocked!
Why is this exception occurring?
The script is making calls to motor ports "b" and "c".
If you do not have a motor plugged into port "b" or "c" then this exception will be caused.
To fix this error plug a motor into port "b" and another motor into port "c".
Aloha, i'm creating a teamspeakbot and want to write and read to a telnet session created with a TeamSpeak3 server
import telnetlib
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.read_all()
What i'm expecting:
Connected to localhost
Escape character is '^]'.
TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a li...
But instead i get a time out after the 10 seconds:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/telnetlib.py", line 325, in read_all
self.fill_rawq()
File "/usr/lib/python2.7/telnetlib.py", line 516, in fill_rawq
buf = self.sock.recv(50)
socket.timeout: timed out
How can i read all stuff the telnet connection tells me and later write some stuff to it (like the login proces and submit commands and get the response ...)
The Solution is
tn.read_very_eager()
My code now looks like this:
import telnetlib, time
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.write('help\n')
time.sleep(0.05)
print(tn.read_very_eager())
read_all() blocks until EOF is reached*, or until your timeout is reached. While I've not used telnetlib much, I have assumed it's for the sort of service which displays something then closes the connection.
How do you get on with code like:
tn = telnetlib.Telnet('localhost', 10011, 10)
tn.read_some()
*https://docs.python.org/2/library/telnetlib.html#telnetlib.Telnet.read_all
I am trying to connect to some router using the above code and I am using juniperj2320.py module and in test file I have
am using code base from this http://subversion.assembla.com/svn/clauzsistel08/trunk/dist/
#!/usr/bin/python
from localconf import *
from juniperj2320 import *
import sys
import traceback
SERIALDEVICE = '/dev/ttyUSB0'
# Now configure the router
try:
router = JuniperJ2320(SERIALDEVICE)
except RouterConfigurationException, err:
print "Router configuration error: ", err
print "Please try again."
sys.exit(1)
but am getting this following error
./test.py
> /root/pyserial-2.6/examples/serialrouter.py(37)__init__()
-> serial.Serial.__init__(self, serialdevice, baudrate=baudrate, \
(Pdb) c
Traceback (most recent call last):
File "./test.py", line 30, in
router = JuniperJ2320(SERIALDEVICE)
File "/root/pyserial-2.6/examples/juniperj2320.py", line 32, in __init__
BYTESIZE, PARITY, STOPBITS, TIMEOUT)
File "/root/pyserial-2.6/examples/serialrouter.py", line 44, in __init__
fdpexpect.fdspawn.__init__(self, self.fileno())
File "/usr/lib/python2.6/site-packages/fdpexpect.py", line 40, in __init__
spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile )
File "/usr/lib/python2.6/site-packages/pexpect.py", line 412, in __init__
self.closed = True # File-like object.
AttributeError: can't set attribute
and absolutely clue less on wat happening here ! any help ll be greatly appreciated
thanks
This is a little bit of a shot in the dark, because I'm not familiar with the modules you're using, but based on the traceback, it looks like the constructor is expecting a file-like object, not just a file path. Try this instead.
SERIALDEVICE = '/dev/ttyUSB0'
# Now configure the router
try:
router = JuniperJ2320(open(SERIALDEVICE))
Instead of writing your own serial communications program wouldn't be easier to have pexpect drive a serial program like minicom?