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)