ESP-01S GPIO2 problem

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

ESP-01S GPIO2 problem

Post by davef » Sun Nov 22, 2020 6:58 pm

Following this tutorial https://pymotw.com/2/socket/udp.html and with my code

Code: Select all

import usocket
import sys
from machine import Pin


pin2 = Pin(2, Pin.OUT)
pin2.on()


#  create a TCP/IP socket
sock = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)

#  bind the socket to the port
server_address = ('192.168.10.104', 10000)
sock.bind(server_address)

while True:
    print('waiting for message')
    data, address = sock.recvfrom(4096)
    data = data.decode('utf-8')

    if (data == 'Off'):
         pin2.off()

    if (data == 'On'):
        pin2.on()

    if data:
        sent = sock.sendto(data, address)
I can turn the Blue LED on which is attached to GPIO2 but I can't turn it off. Appears when GPIO2 is high that I can't make it low again ... unless I reset the device. I know you have to handle GPIO2 and GPIO0 differently during boot-up but I don't see what I am doing wrong for this use case.

Thanks for any pointers.

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

Re: ESP-01S GPIO2 problem

Post by Roberthh » Mon Nov 23, 2020 10:48 am

I do not think it's a problem of the GPIO. I suspect it's a problem in the While loop, in that the expression "if (data == 'Off'):" never is True. Maybe it should read:
if (data == b'Off'):
What do you get when you print data?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-01S GPIO2 problem

Post by davef » Mon Nov 23, 2020 6:53 pm

Thanks for the response.

In the real code I did follow the decode line with a print statement, but it doesn't print out the data.

However, in the <if> statements I do print out a statement to confirm that the program responded properly to the client input. However, GPIO2 appears to "hang-up" in the low state.

The client data required:

Code: Select all

message = bytes(message, 'utf-8') # convert to bytes
I am waiting for some ESP-07 to arrive then I will be able to use other GPIO, but this unit only has GPIO0 and GPIO0 available.

I found this tutorial https://www.instructables.com/ESP8266-U ... as-inputs/ and tried adding the 3K3 resistors to GPIO0 and GPIO2 "pulled-up" to 3V3 but that made no difference.

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

Re: ESP-01S GPIO2 problem

Post by Roberthh » Mon Nov 23, 2020 7:01 pm

Sorry, I did not notice the decode. So in the print statement you get noting. That's most likely because there was nothing to read and data has the value None. So you should have that print at the point where you send data back.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-01S GPIO2 problem

Post by davef » Mon Nov 23, 2020 7:15 pm

If I do:

Code: Select all

    if data:
	print(data)
        sent = sock.sendto(data, address)
it doesn't print <data>

The client says it got data back ... so I printed that and it says the data is:

Code: Select all

b'On'
which is exactly what is sent.

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

Re: ESP-01S GPIO2 problem

Post by Roberthh » Mon Nov 23, 2020 7:27 pm

That's not consistent. If there is nothing to print, then there is nothing to send. Are you sure that you changed the code on the device?
Just to confirm the proper operation of GPIO2:
can you switch the LED from the REPL with:

from machine import Pin
pin2 = Pin(2, Pin.OUT)
pin2(0)
# and
pin2(1)

pin2(0) and pin2(1) are just shorthands for pin2.value(0) and pin2.value(1) or pin2.off() and pin2.on()

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-01S GPIO2 problem

Post by davef » Mon Nov 23, 2020 7:40 pm

Yes, that works.

I tried:

Code: Select all

while True:
    print('waiting for message')
    print('still waiting')
    data, address = sock.recvfrom(4096)
    data = data.decode('utf-8')
  
and the 2nd print('still waiting') doesn't print.

I modified the example in the linked tutorial to include those tests for <on> and <off> maybe that is not the right approach.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-01S GPIO2 problem

Post by davef » Mon Nov 23, 2020 7:45 pm

Oops, yes the code is changed on the device at each change. I check by getting the file back.

Is there some issue with handling the socket that I am not aware of?

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

Re: ESP-01S GPIO2 problem

Post by Roberthh » Mon Nov 23, 2020 7:55 pm

I assume the line " print('still waiting')" is after the recv_from() call.
Are you sure that you sorted the IP addresses right and that both server and client are connected Wifi-wise to the same network?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: ESP-01S GPIO2 problem

Post by davef » Mon Nov 23, 2020 8:09 pm

I made a wrong assumption ... I just made a spelling change in the server script and it did NOT update on the device.

I now see those extra 3K3 resistors I put in are not going to 3V3 but to my reset button ;( However, I had this "problem" before I put them in.

Thank you for your effort thus far, I'll come back when I sort my hardware.

Post Reply