Chat freely about anything...

User avatar
By Michael Bilenko
#12664 Hello Everyone!

I'm interested in maximum attainable speed of data transmission for esp to smartphone.
Now i'm using AT command mode, baud rate 921600, softap mode on esp.
ESP running tcp server in multiple connection mode.

using AT+CIPSEND=1024(or AT+CIPSEND=2048) for data transmission. Transmission of 2MB file takes about 5 minutes. Is there any tricks to speedup transmission? Maybe using unvarnished mode?

Thanks
User avatar
By Michael Bilenko
#12717 Here is my PC python code, for reference
Code: Select allimport struct
import time
import serial
import re
import pdb
import base64
import os,sys,traceback

class EspSerial:

    def __init__(self):
        if os.name == 'posix':
            portname='/dev/tty.usbserial'
        else:
            portname = 'COM16'
        self.ser = serial.Serial(port=portname, baudrate=921600, timeout=1.0)
        self.exit = False
        self.current_cmd = ""
        self.echo_suppressed = False
        self.result_string = ""
        self.result = False
        self.recv_buffer = ""
        self.binary_mode = False
        self.expected_bytes = 0
        self.current_conn = -1


    def process_line(self,line):
        if line != "":
            print "                       >> GOT", line
        if not self.echo_suppressed:
            if line == self.current_cmd:
                print "Found echo"
                self.echo_suppressed = True
                return

        if line == "":
            return
            #print "Empty line.. passing..."
        if line == "OK":
            self.exit = True
            self.result = True
            return
        if line == "no change":
            self.exit = True
            self.result = True
            return


        if line == "ERROR":
            self.exit = True
            self.result = False
            return

        self.result_string = line

    def handle_line(self,line):

            expr = "\+IPD,([0-9]+),([0-9]+):"
            groups = re.match(expr,line)
            if groups:
                print "Found IPD"
                v = groups.groups()
                self.current_conn = int(v[0])
                self.expected_bytes = int(v[1])
                #print port,self.expected_bytes
                line = re.sub(expr,"",line)
                self.binary_mode = True


            if self.binary_mode:
                print "Found binary mode string: ",line

                self.expected_bytes -= len(line)
                self.recv_buffer += line
                if self.expected_bytes <= 0:
                    self.binary_mode = False
                    self.poll_string("OK",2.0)
                    self.packet_received(self.current_conn)
                return ""
            else:
                line = line.replace("\n","")
                line = line.replace("\r","")
                return line

    def poll_string(self,string,timeout):
        self.exit = False
        self.echo_suppressed = False
        self.result = False
        self.result_string = ""

        start = time.clock()
        while True:
            line = self.ser.readline()
            line = self.handle_line(line)
            self.process_line(line)
            if line == string:
                return
            if (time.clock() - start) > timeout:
                self.exit = True
                raise Exception('Timeout', ':(')


    def poll_answer(self,timeout):
        self.exit = False
        self.echo_suppressed = False
        self.result = False
        self.result_string = ""

        start = time.clock()
        while not self.exit:
            line = self.ser.readline()
            line = self.handle_line(line)

            self.process_line(line)
            if (time.clock() - start) > timeout:
                self.exit = True
                raise Exception('Timeout', ':(')

    def send_cmd_ok(self, cmd,timeout=5.0):
        self.current_cmd = cmd
        self.ser.flush()
        self.ser.write(cmd+"\n")
        self.ser.flush()
        self.poll_answer(timeout)

        if not self.echo_suppressed:
            raise Exception('No echo', ':(')
        #time.sleep(0.5)
        return self.result

    def send_cmd_string(self, cmd, timeout=5.0):
        self.current_cmd = cmd
        self.ser.flush()
        self.ser.write(cmd+"\r\n")
        self.ser.flush()
        self.poll_answer(timeout)

        if not self.echo_suppressed:
            raise Exception('No echo', ':(')

        return self.result_string

    def send_cmd_wait_string(self, cmd, string, timeout=5.0):
            self.current_cmd = cmd
            self.ser.flush()
            self.ser.write(cmd+"\r\n")
            self.ser.flush()
            self.poll_string(string,timeout)

#            if not self.echo_suppressed:
#                raise Exception('No echo', ':(')

            return True

    def send_pkt(self,conn,packet):
        #packet = base64.b64encode(packet)
        self.ser.flushInput()
        cmd = "AT+CIPSEND="+str(conn)+','+str(len(packet))+"\r\n"
        print "                   >> SENDING", cmd
        self.ser.write(cmd)
        self.ser.flush()
        self.poll_string("AT+CIPSEND="+str(conn)+','+str(len(packet)),2.0)
        self.ser.read();self.ser.read();
        self.ser.write(packet)
        #print "Ref ",packet
        #print "Echo",\
        self.ser.read(len(packet))
        self.poll_string("SEND OK",10.0)
        print "Send finished"

    def send_pkt_nomux(self,conn,packet):
        #packet = base64.b64encode(packet)
        self.ser.flushInput()
        cmd = "AT+CIPSEND"
        print "                   >> SENDING", cmd
        self.ser.write(cmd)
        self.ser.flush()
        self.poll_string("AT+CIPSEND",2.0)
        self.ser.read();self.ser.read();
        self.ser.write(packet)
        #print "Ref ",packet
        #print "Echo",\
        self.ser.write("+++\n\r")
        self.ser.read(len(packet))
        self.poll_string("SEND OK",10.0)
        print "Send finished"


    def packet_received(self,conn):
        packet = self.recv_buffer
        self.recv_buffer = ""
        print "Receive callback", packet

        data = gen_coord()
        print len(data)
        self.send_pkt(conn,"LEN:%d\n" % len(data))
        #self.send_pkt(conn,gen_coord())
        for i in range(0,2500):
            print i
            self.send_pkt(conn,base64.b64encode(gen_coord())+"\n")



def gen_coord():
    s = ""
    startx = 43.0
    starty = 22.0

    for i in range(0,50):
        startx += 0.00002
        starty += 0.00002
        s += struct.pack("dd", startx, starty)
    print len(s)
    return s

gen_coord()

s = EspSerial()

print "Removing echo"
s.send_cmd_ok("ATE0")

print "Resetting ..."
s.send_cmd_wait_string("AT+RST", "ready",10.0)

print "Query version ..."
firmver = s.send_cmd_string("AT+GMR")
print "Firmware version ", firmver

print "Turninng into AP mode...."
s.send_cmd_ok("AT+CWMODE=3",10.0)
print "Setting credentials...."
s.send_cmd_ok("AT+CWSAP=\"test\",\"testtest\",11,0",10.0)

print "MUX setting...."
s.send_cmd_ok("AT+CIPMUX=0",10.0)
print "MUX setting...."
s.send_cmd_ok("AT+CIPMODE=1",10.0)
print "Starting client"
time.sleep(20.0)
s.send_cmd_ok("AT+CIPSTART=\"TCP\",\"192.168.4.100\",6000")
s.send_pkt_nomux(0,base64.b64encode(gen_coord())+"\n")


while True:
    try:
        s.poll_answer(5.0)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        print traceback.print_exc(file=sys.stdout)