combined SPI and WIFI code on nodemcu blocking SPI

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: 7
Joined: Thu Dec 17, 2020 9:20 pm

combined SPI and WIFI code on nodemcu blocking SPI

Post by yefj » Mon Dec 21, 2020 12:47 pm

Hello,I have built a wifi network which worked fine. then i added SPI commands to transfer data simultaniosly with wifi. if i use spi.write after conn=s.accept() my SPI sends nothing. If i write spi.write before conn=s.accept() it sends SPI sequence as shown in the photo bellow.I thought maybe it waiting for me to connect to the WIFI network,But when i connected to the wifi network still it continued to block SPI commands.
Why the Socket commands block SPI commands? Thanks.

Code: Select all

from machine import Pin,SPI
from time import sleep
import network
import usocket as socket
spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0)
ap_if = network.WLAN(network.AP_IF)
ssid = 'Sensor network4'
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:
    spi.write(b'1234')
    spi.write(b'1234')
    conn=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()

yefj
Posts: 7
Joined: Thu Dec 17, 2020 9:20 pm

Re: combined SPI and WIFI code on nodemcu blocking SPI

Post by yefj » Wed Dec 23, 2020 9:36 am

I think its because the socket is waiting for me to connect to it.
I entered the wifi network,then i went to a webpage and typed 192.168.4.1 (it presented a text)
but its not cnnection to a socket.
how do i make conn=s.accept() to return something so my code would go on?
Thanks.

Code: Select all

from machine import Pin,SPI
import network
import usocket as socket
ap_if = network.WLAN(network.AP_IF)
ssid = 'sensor_net'
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)
spi=SPI(1, baudrate=1000000, polarity=0, phase=0)
while True:
    spi.write(b'1234')
    conn, addre=s.accept()
    request=conn.recv(1024)
    request='<p style="color:red"><u><h1>The temperature is:</u></p>'
    request2=str(12.7)
    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()
    spi.write(b'1234')

Post Reply