Close
0%
0%

Hardware serial port monitor with WiFi

Arduino connects to the TX line (of a router, RPI) and display serial data on smartphone over WiFi

Similar projects worth following
The goal of the project is to create a small module that can connect to any hardware serial port and then display serial data on smartphone or PC.

*************** UPDATE: (8th March 2016): *********************************************************************
I started this project almost a year ago. At that time arduino core for esp was in pre alpha stage so it was logical to use nodemcu. If you use nodemcu the serial port is already reserved for issuing commands to esp so I could not use it for sniffing traffic.

If I started now I wouldn't use arduino...

I would just flash arduino core on esp8266 and be done with it. It would be a lot easier to make.
*************************************************************************************************************************

Arduino listens for serial port communication on its hardware serial port.
Then it sends every received line of data trough software serial port to ESP8266.

ESP8266 puts every received line of data into circular buffer.

ESP8266 also runs code for webserver and a website which pools the buffer for new data and displays it on the website. (Sadly there is no websockets support for ESP8266.)

To see this serial data all you have to do is open the website (IP) on your smartphone and enable javascript.

  • 1 × Arduino UNO Converted to 3.3.V
  • 1 × ESP8266 Model ESP-07 running NodeMCU
  • 1 × OLED display 128x64

  • Why did I use arduino?

    Frenky03/08/2016 at 11:55 0 comments

    I started this project almost a year ago. At that time arduino core for esp was in pre alpha stage so it was logical to use nodemcu. If you use nodemcu the serial port is already reserved for issuing commands to esp so I could not use it for sniffing traffic.

    If I started now I wouldn't use arduino...

    I would just flash arduino core on esp8266 and be done with it. It would be a lot easier to make.

View project log

  • 1
    Step 1

    Here is some code:

    Arduino:

    /*
     * RX is digital pin 10 (connect to TX of other device)
     * TX is digital pin 11 (connect to RX of other device)
     */
    #include <SoftwareSerial.h>
    #include "U8glib.h"
    
    U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); 
    
    String inputString = "";         // a string to hold incoming data
    boolean stringComplete = false;  // whether the string is complete
    
    SoftwareSerial mySerial(10, 11); // RX, TX
    
    void draw(void) {
      u8g.setFont(u8g_font_9x15);
      u8g.setPrintPos(0, 11); 
      u8g.print("Speed: 115200");
      u8g.setPrintPos(0, 26); 
      u8g.print("Data bits: 8");
      u8g.setPrintPos(0, 40); 
      u8g.print("Parity: None");
      u8g.setPrintPos(0, 54); 
      u8g.print("Stop bits: 1");
    }
    
    void setup()
    {
      inputString.reserve(200);
      // Open serial communications and wait for port to open:
      Serial.begin(115200);
      // set the data rate for the SoftwareSerial port
      mySerial.begin(9600);
      mySerial.println("Hello, world?");
    }
    
    void loop() {
      // print the string when a newline arrives:
      if (stringComplete) {
        inputString.trim();
        inputString="newdata('"+inputString+"')";
        mySerial.println(inputString); 
        // clear the string:
        inputString = "";
        stringComplete = false;
      }
      u8g.firstPage();  
      do {
        draw();
      } while( u8g.nextPage() );
    }
    
    void serialEvent() {
      while (Serial.available()) {
        // get the new byte:
        char inChar = (char)Serial.read(); 
        // add it to the inputString:
        inputString += inChar;
        // if the incoming character is a newline, set a flag
        // so the main loop can do something about it:
        if (inChar == '\n') {
          stringComplete = true;
        } 
      }
    }

    HTML website on ESP8266. Save it on ESP as mysite.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width">
    <style>
    body {
        background-color: black;color: #00FF00;font-family: Courier, monospace;font-size:12px;font-weight:bold;	line-height: 100%;
    }
    </style> 
    </head>
    <body>
    <div id="test">
        <p>ESP8266 serial sniffer:</p>
    </div>
    <span class="css3-blink">_</span>
    <script>
    function loadXMLDoc()
    {
    	var xmlhttp;
    	xmlhttp=new XMLHttpRequest();
    	xmlhttp.onreadystatechange = function(){
    		if (xmlhttp.readyState==4 && xmlhttp.status==200){
    			document.getElementById('test').insertAdjacentHTML( 'beforeend', '<p>' + xmlhttp.responseText+'</p>' );
    			window.scrollTo(0,document.body.scrollHeight);
    		}
    	}
    	xmlhttp.open("GET","http://192.168.1.214/data",true);
    	xmlhttp.send();
    }
    var myVar = setInterval(function(){ loadXMLDoc() }, 200);
    </script>
    </body>
    </html>

    And init.lua for ESP8266:

    wifi.sta.setip({ip="192.168.1.214",netmask="255.255.255.0",gateway="192.168.1.1"})
    a = {}
    a[1] = " "
    readIx=1
    writeIx=2
    srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
        conn:on("receive", function(client,request)
            local buf = "";
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
            if(path ~= "/") then
    			if(readIx ~= writeIx) then
    				 buf = a[readIx];
    				 readIx=readIx+1
    			else
    				 buf=" "
    			end
            else
                 file.open("mysite.html", "r")
                 buf =  file.read()
                 file.close()
            end
            client:send("HTTP/1.1 200 OK\r\n")
            client:send("Access-Control-Allow-Origin:*\r\n")
            client:send("Connection:close\r\n\r\n")
            client:send(buf);
            client:close();
            collectgarbage();
        end)
    end)
    function newdata(serialData)
      a[writeIx] = serialData
      writeIx=writeIx+1
    end
    That is all you'll need. :)

View all instructions

Enjoy this project?

Share

Discussions

homefefeld wrote 01/08/2020 at 17:50 point

Hi Frenky,

I really like the concept.

But indeed it would make more sense to use only a standalone ESP8266. So I was wondering if you managed to kick off the migration.

All the best and Happy new year.

  Are you sure? yes | no

Dustin wrote 03/06/2018 at 15:09 point

Hello all, I have an ESP8266 with a sensor input script for running my fire/CO alarm notification at my house. It is Lua codded and works well but I was wondering if it would be possible to add a serial sniffer like you have here so I can view the real time status via a web server of what the board is doing. Would that be possible? 

Here is my current init file.  Thanks!

file.open( "sensorTypeNum.lua", "r" )
sensorTypeNum = file.read()
sensorTypeNum = string.gsub(sensorTypeNum, "%s+", "")
sensorTypeNum = tonumber(sensorTypeNum)
file.close()

print("Starting Wifi to Cloud Alert System")

tmr.alarm(1, 1000, 1, function()

file "setwifi.lua" so the user can program your wifi network parameters.
if wifi.sta.getip() == nil then
    count = count + 1
print("(" .. count .. ") Waiting for a Wifi Connection...")

    if count == 10 then
        tmr.stop(1)
        dofile("setwifi.lua")
    end

else
tmr.stop(1)
print("You are connected to Wifi. IP Address:"
..wifi.sta.getip())
    print("Starting Sensor...")
    tmr.alarm(0, 3000, 0, function()


if sensorTypeNum == 2 then
print("Executing Continuous Read Sensor Software")
print("Listening for Sensor Inputs...")
dofile("1b_continuousSensor.lua")
else
print("Executing Interrupt Sensor Software")
print("Listening for Sensor Inputs...")
dofile("1a_interruptSensor.lua")
end
end)

end
tmr.alarm(1, 300000 ,1 ,function()
node.restart()
print("RESTARTING....")
end)

end)

  Are you sure? yes | no

Sumant wrote 07/14/2017 at 08:39 point

Hi how to upload HTML and init.lua to ESP? I already have lua loader i am able to uplad inti file but how to uplad HTML.

  Are you sure? yes | no

Sumant wrote 07/12/2017 at 08:28 point

Hi, I've set up ESP 12 such that it receives data from Soft serial from arduino and hosts it on a server, but the problem is the data received is not accurate for example if i send a String ABC i receive A16, A24, B25, B67, C45,A23,A14 etc , whereas if i test the softserial program individually without hosting the server there is no problem everything runs fine. Can you tell me why this happens or if you can think of any solution? 

  Are you sure? yes | no

geekstips wrote 12/16/2016 at 06:51 point

Splendid, ESP8266 rules every time !! Thank you

  Are you sure? yes | no

Willem Wouters wrote 03/09/2016 at 13:06 point

Hi,

I just added websocket support to my own ESP8266 project,
ESP Sdk, not arduino  or  nodemcu
If you are interested in the websocket source code, please take a look at:

https://github.com/willemwouters/ESP8266/tree/master/projects/apps/websocket_example

Kind regards,
Willem Wouters

  Are you sure? yes | no

Frenky wrote 03/09/2016 at 13:15 point

Nice. :) If I decide to remake this project with only esp8266 then I'll definitely look into your code. Websocket solution would be much nicer than timed ajax requests.



  Are you sure? yes | no

zoobab wrote 03/08/2016 at 11:27 point

You could try with an ESP with more pins ( to drive your LCD for ex), the ESP can be easily flashed with the Arduino IDE. That's a real waste of components here.

  Are you sure? yes | no

Frenky wrote 03/08/2016 at 11:51 point

When I started with this project arduino core for esp was in pre alpha stage so it was logical to use nodemcu. If I started now I'd just flash arduino core on it and be done with it. It would be a lot easier to make.

  Are you sure? yes | no

Frenky wrote 07/14/2015 at 08:38 point

And arduino is not just transferring data from one port to another. It's putting data into function newdata('this is some test data') so that ESP knows that this is data to display and not some serial commands for controlling the ESP.

  Are you sure? yes | no

Frenky wrote 07/14/2015 at 08:35 point

I used arduino because its easier to:
- use keypad to set serial port config (to-do)
- display config on lcd
- write algorithm for sniffing out the correct serial config(to-do)

All of these things could probably be done with ESP but would take a lot more effort.

I would also had to program ESP in a way that it sends out all the data that it receives on serial port and this would make it very hard to debug because all incoming serial commands would be just treated as data to display on website...

  Are you sure? yes | no

flickflo wrote 07/10/2015 at 22:53 point

Maybe I missed a point but one question: Why the hell the Arduino to transfer data from one serial port to another? I would attach the ESP directly to the hardware.

edit: apart from that, nice project. I have several scenarios to use that.

  Are you sure? yes | no

Clovis Fritzen wrote 03/08/2016 at 03:47 point

That's the very same question I've been asking myself

  Are you sure? yes | no

Frenky wrote 03/08/2016 at 12:22 point

I started this project almost a year ago. At that time arduino core for esp was in pre alpha stage so it was logical to use nodemcu. If you use nodemcu the serial port is already reserved for issuing commands to esp so I could not use it for sniffing traffic.

If I started now I wouldn't use arduino... 

I would just flash arduino core on esp8266 and be done with it. It would be a lot easier to make.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates