0

I've stumbled upon a ERNOMEM error while trying to send data from two ESP8266 to PC through sockets. It happens ONLY when im sending data more than 5 times a second. One of ESP's is and access point that another esp and PC connect to. PC has a client thats recieves all data and saves it into csv file. During a run there are only 30 lines in csv and then ERNOMEM happens with some random E:M656 (no idea what that means). Does anybody know how to solve this problem or how to send data every 10ms in other way? Here are codes for access point esp another esp as station and client (on pc): to avoid questions - closing and opening sockets is to achieve data from both esp's in parallel for comparison

import usocket as socket 
from machine import I2C, Pin
import mpu6050
from utime import sleep_ms

ssid = 'DRFT'
passwd = 'fiberteam'


ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, authmode=3, password=passwd, hidden=0) 
AP_IP = ap.ifconfig()[0]
print("AP's IP:",AP_IP)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((AP_IP, 8888))
s.listen(1)

i2c = I2C(scl=Pin(5), sda=Pin(4))
accelerometer = mpu6050.accel(i2c)
print(accelerometer.get_values())

while True:
    if not ap.isconnected():
      print("waiting for connection...")        
      sleep_ms(5000)
    else:

      while True:
        clientsocket, addr = s.accept()
        values = accelerometer.get_values()
        data_string = str(values['AcX'])+','+str(values['AcY'])+','+str(values['AcZ'])+','
        clientsocket.send(bytes(data_string, 'utf-8'))
        print(data_string)
        clientsocket.close()
        sleep_ms(100) 

Another ESP:

import network
import usocket as socket
from machine import I2C, Pin
import mpu6050
from utime import sleep_ms

ssid = 'DRFT'
passwd = 'fiberteam'


station = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF) # do not fucin delete dis
ap_if.active(False) # its to turn off access point mode (cuz its a station duh)
station.active(True)
station.connect(ssid, passwd)
station.ifconfig(('192.168.4.69', '255.255.255.0', '192.168.4.1', '8.8.8.8'))
STA_IP = '192.168.4.69'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((STA_IP, 8888))
s.listen(1)


i2c = I2C(scl=Pin(5), sda=Pin(4))
accelerometer = mpu6050.accel(i2c)
print(accelerometer.get_values())

while True:
    if not station.isconnected():
      print("waiting for connection...")        
      sleep_ms(5000)
    else:

      while True:
        clientsocket, addr = s.accept()  
        values = accelerometer.get_values()
        data_string = str(values['AcX'])+','+str(values['AcY'])+','+str(values['AcZ'])+','
        clientsocket.send(bytes(data_string, 'utf-8'))
        print(data_string)
        clientsocket.close()
        sleep_ms(100)

Client:

import socket
import time
import os

time_now = time.strftime("%Y-%m-%d_%H-%M")
f = open(os.path.join(os.getcwd(), 'DRFT_ride_data'+time_now+'.csv'), 'w+')
f.close()

last_ip_dot = ['1', '69']  # last parts of IP's of boards

curr = 0
last = 0
file = os.path.join(os.getcwd(), 'DRFT_ride_data'+time_now+'.csv')
f = open(file, 'a+')
f.write('fAcX,fAcY,fAcZ,fGyX,fGyY,fGyZ,rAcX,rAcY,rAcZ,rGyX,rGyY,rGyZ,\n')
f.close()
while True:

    for dot in last_ip_dot:
        curr = int(dot)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.connect(('192.168.4.'+dot, 8888))

        data = s.recv(128)
        if len(data) <= 0:
            s.close()
            continue
        if curr == last:
            continue
        with open(file, 'a+') as f:
            f.write(data.decode('utf-8'))
            if curr == 69:
                f.write('\n')
            f.close()
        last = curr
        s.close()

1 Answer 1

0

use socket.print_pcbs() and see what it reports.

Most likely you are running out of sockets despite they are being closed. For some time sockets are in TIME_WAIT state after close. Usually it is not a problem with big computing platforms which has a lot of memory.

But in case of memory constrained devices you can't have unlimited number of connections.

MEMP_NUM_NETCONN macro (with default value 8) of lwip is responsible for number of concurrent sockets which you can use. You can tweak it to increase number of simultaneous connections but you will lose some part of RAM to lwip.

Prior to accepting new connection or opening new one you can try to cleanup stale sockets this way:

https://gist.github.com/d-a-v/ed67f7a6f476a043d1c7f347c829087e

// https://github.com/esp8266/Arduino/issues/1923
// https://github.com/esp8266/Arduino/issues/4213
// compatible with lwip-v1 and -v2
// no need for #include

struct tcp_pcb;
extern struct tcp_pcb* tcp_tw_pcbs;
extern "C" void tcp_abort (struct tcp_pcb* pcb);

void tcpCleanup ()
{
  while (tcp_tw_pcbs != NULL)
  {
    tcp_abort(tcp_tw_pcbs);
  }
}

Also you could switch from TCP to UDP. This way you will speed up data transmissions but will lose delivery guarantee.

2
  • The OP is using MicroPython, and your code looks like C. Are you suggesting they modify and recompile their MicroPython firmware?
    – nekomatic
    Dec 17, 2019 at 9:16
  • @nekomatic, Sure. Why not? He has only two options: either meddle with TIME_WAIT or switch to UDP Dec 17, 2019 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.