ESP8266 UART question

Posted on
  • From http://www.espruino.com/USART

    "As software serial doesn't use dedicated hardware there are some compromises:

    Baud rates significantly above 9600 baud are unlikely to be reliable
    Sending more than one or two characters will block execution of other JavaScript code until completion (hardware serial ports have a ~100 byte transmit buffer)
    Software serial reception will become increasingly unreliable the higher the CPU load."

    In my application, I use soft serial (rx=D5, tx=D6). I get bursts of 60+ bytes (5x a second) from an external source. I find that anything beyond 2400 baud increases errors in the data received. What's the experience of others?

    Using an Amica 8266 board.

  • do you like to share your serial code section so others cant test and recommend some optimisation if needeed.

    have you tried to connect over tcp/ip and use the hardware serial for communication?

  • I am using two soft serials, taking input from one UART and passing it to the USB UART.

    var hubSerial = new Serial(); 
    	hubSerial.setup(2400, { // problematic when baud rate=9600
    		rx: NodeMCU.D5,
    		tx: NodeMCU.D6
    	});
        var usbSerial = new Serial(); 
    	usbSerial.setup(9600, {
    		rx: NodeMCU.D9,
    		tx: NodeMCU.D10
    	});
    
    	usbSerial.on('data', function (data) {
    		try {
    			hubSerial.print(data);
    		} catch (e) {
    			d0("failed to send on hub's data");
    		}
    	});
    	hubSerial.on('data', function (data) {
    		try {
    			usbSerial.print(data);
    		} catch (e) {
    			d0("failed to send on usb's data");
    		}
    	});
    

    d0() is essentially console.log over port 23.

    The PIN assignment cannot be changed hence the use of soft serial. I think the issue is that .on('data',..) is coming back 1 or 2 bytes at a time. I will experiment using .available().

  • Using hubSerial.available() - rather than hubSerial.data('on') - to chunk the input stream seems to make some difference (fewer errors) at 4800 baud. But no difference at 2400 (mostly good) or 9600 (bad transmission). See code below (both .available and .on('data') fragments are included:

    var d0 = console.log;
    var debug = console.log;
    
    var hubSerial = new Serial();
    var usbSerial = new Serial();
    
    function onInit() {
    	hubSerial.setup(4800, { // problematic when baud rate=9600
    		rx: NodeMCU.D5,
    		tx: NodeMCU.D6
    	});
    	usbSerial.setup(9600, {
    		rx: NodeMCU.D9,
    		tx: NodeMCU.D10
    	});
    
        usbSerial.on('data', function (data) {
            try {
                hubSerial.print(data);
            } catch (e) {
                d0("failed to send on hub's data");
            }
        });
      
        //*
    	hubSerial.on('framing', function (data) {
    		d0("framing error");
    	});
    	
    	setInterval(() => {
    		let n = hubSerial.available();
    		if (n > 0) {
    			//d0(n);
    			let d = hubSerial.read(0);
    			usbSerial.print(d);
    		}
    	}, 200);
        //*/
    
    	/*
    	hubSerial.on('data', function (data) {
    		try {
             // d0(data.length);
    			usbSerial.print(data);
    		} catch (e) {
    			d0("failed to send on usb's data");
    		}
    	});
    	//*/
    }
    
  • If you like you can run some tests with removing TX if you just reading and RX if just sending.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

ESP8266 UART question

Posted by Avatar for user106712 @user106712

Actions