importing SPi from machine error with wifi

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
yefj
Posts: 6
Joined: Thu Dec 17, 2020 9:20 pm

importing SPi from machine error with wifi

Post by yefj » Sun Dec 20, 2020 11:25 am

Hello,I have built a code which creates wifi network.
then i tried to simultaniosly send data over SPI and as shown bellow
and it gave me an error of :
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'machine' isn't defined
But when i did SPI alone ,It sent SPI just fine.
Why SPI and wifi are conflicting on thonny micropython?
Thanks.

Code: Select all

from machine import Pin,SPI
import network
import usocket as socket
spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0)
spi.write(b'1234')
ap_if = network.WLAN(network.AP_IF)
ssid = 'Sensor network3'
password = '123456789'
ap_if.active(True)
ap_if.config(essid=ssid, password=password)
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('',80))
s.listen(5)
while True:
    conn, addre=s.accept()
    request=conn.recv(1024)
    request='<p style="color:red"><u><h1>The temperature is:</u></p>'
    request2=str(12.9)
    response='gyhkjgku'
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(request)
    conn.sendall(request2)
    conn.close()
    

User avatar
Roberthh
Posts: 2272
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: importing SPi from machine error with wifi

Post by Roberthh » Sun Dec 20, 2020 11:39 am

That#s because you did not import machine, but SPI from machine. So the line should read:

spi = SPI(1, baudrate=1000000, polarity=0, phase=0)

That's the way it works in Python.

Post Reply