urequest could not request the server after short downtime

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
mattm
Posts: 1
Joined: Fri Jul 22, 2022 8:01 am

urequest could not request the server after short downtime

Post by mattm » Fri Jul 22, 2022 8:22 am

Hello,

i have a Arduino Nano RP2040 Connect and a simple program to read a reed contact. In a loop the current reed contact value should be send to a server. Actually the server is a simple docker container.

The Arduino can send the data to the server as long as it is reachable. So far so good.
If the server fails briefly (docker container is stopped) and is running again, no more requests from the Arduino reach it. What can be the reason?

Code: Select all

import machine, time, socket
from machine import Pin
from network import WLAN, STA_IF

reed_pin = Pin(17, Pin.IN, Pin.PULL_UP)
led_pin = Pin(6, Pin.OUT)

# AP info
SSID=''        # Network SSID
KEY=''  # Network key

PORT = 3001
HOST = "192.168.178.53"

def connect_wifi():
    # Init wlan module and connect to network
    print("Trying to connect. Note this may take a while...")
    
    wlan = WLAN(STA_IF)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.active(True)
        wlan.connect(SSID, KEY)
        while not wlan.isconnected():
            pass

    # We should have a valid IP now via DHCP
    print("Wi-Fi Connected ", wlan.ifconfig())

def send_request(value):

    # Get addr info via DNS
    addr = socket.getaddrinfo(HOST, PORT)[0][4]
    print(addr)
    
    # Create a new socket and connect to addr
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(addr)

    # Set timeout
    client.settimeout(3.0)

    # Send HTTP request and recv response
    client.send(f"GET /?meterType=gas&meterId=1&value={value} HTTP/1.1\r\nHost: {HOST}\r\n\r\n")
    print(client.recv(1024))

    # Close socket
    client.close()

connect_wifi()

while True:
    print("Read: ", reed_pin.value())
    led_pin.value(reed_pin.value())
    
    if (reed_pin.value() == 0):
        send_request(0)
        time.sleep_ms(1000)
    else: 
        send_request(1)
        time.sleep_ms(1000)
        
    led_pin.value(1)
    time.sleep_ms(300)
    led_pin.value(0)
    time.sleep_ms(300)
The LED keeps blinking, so the loop keeps running.

User avatar
curt
Posts: 25
Joined: Thu Jul 29, 2021 3:52 am
Location: Big Lake, Alaska

Re: urequest could not request the server after short downtime

Post by curt » Fri Jul 22, 2022 2:50 pm

I would verify that the server is actually running (listening). If it was only down briefly, the restart could have failed trying to open the port because the port was in a TIME_WAIT state.

Curt

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: urequest could not request the server after short downtime

Post by karfas » Fri Jul 22, 2022 4:24 pm

Shouldn't settimeout() get called before connect() ?
I wonder that connect() doesn't raise an exception when the connect (obviously) is not working.

See e.g. https://github.com/micropython/micropyt ... equests.py:

Code: Select all

    s = usocket.socket(ai[0], usocket.SOCK_STREAM, ai[2])

    if timeout is not None:
        # Note: settimeout is not supported on all platforms, will raise
        # an AttributeError if not available.
        s.settimeout(timeout)

    try:
        s.connect(ai[-1])
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply