-
1Step 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. :)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Dear Frenky, thanx for sharing Your "brainware"! Can You please explain what the lua code "function newdata(serialData)" should do and when or where will this func be called? I think, serial data coming from Ardu should be collected here?! Greetings from Germany, WolgaWibr
Are you sure? yes | no
I have interfaced Arduino with ESP8266 wifi module and LCD. I want to send text from Webpage and it should get displayed on LCD. Can anyone please suggest me the arduino code and web code.
Are you sure? yes | no
How i will connect esp8266 oled and arduino?, can u give us wiring map?
Are you sure? yes | no
sir please suggest me any other serial device or ic which has serial port ...:-)
Are you sure? yes | no
Are you sure? yes | no
okay sir ...:-)
Are you sure? yes | no
Try to understand what my code does. Because it works.
You code is totally different...
Are you sure? yes | no
what can i do now...?i m confused:-(
Are you sure? yes | no
Perhaps this will help? http://goo.gl/50yA6d
Are you sure? yes | no
error is like this:unprotected error in call to Lua API (init.lua:16:attempt to concatenate global 'encoder1count' (a nil value))
Are you sure? yes | no
hii sir ....code--
wifi.setmode(wifi.STATION)
wifi.sta.config("ELITE","rutujajoshi123%")
print(wifi.sta.getip())
if data=="encoder1count" then
uart.on("data")
print("Received from Arduino:", data)
end
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
--print(payload) -- for debugging only
--generates HTML web site
conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
\
'..encoder1count..'')
conn:on("sent",function(conn) conn:close() end)
end)
end)
i have connected one rotary encoder to the arduino...n then to to the esp...i m getting data arduino to esp and printing it in serial monitor bt i want it on webpage...as i send something stable value cnt=100 its showing on webpage when i connected encoder its printing on serial monitor bt not on webpage...i think continous data is being rejected by webpage...kindly give some suggestions...i almost reached thank you..;-)
Are you sure? yes | no
There is a better way to upload mysite.html. Copy the code in notepad. Save it as mysite.html. Then use "Upload File" option in LuaLoader to upload it to ESP:
Are you sure? yes | no
i have uploaded this file i think this is ok..showing"s2.html:1: only one tcp server allowed"......one question sir for uploading html file is it another way or same like lua?..:-(..i have paste it in editor then save it as "s2.html' then write same in blank box below "upload file"..is it right way to download html file??
Are you sure? yes | no
mysite.html does not containt LUA code so you cant just execute it. It is used as a response string for web server.
Try to upload this code on ESP and tell me if it's working?
-- A simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
conn:send("Hello, NodeMcu.")
end)
conn:on("sent",function(conn) conn:close() end)
end)
Are you sure? yes | no
i uploaded mysite.html code to the esp in lualoader when i did "dofie"it is showing error like "mysite.html:1: unexpected symbol near '<' "what is that exactly??
Are you sure? yes | no
When you open IP location in browser you should see a green text "ESP8266 serial sniffer:".
If you don't see that then there is something wrong with the code on ESP... Or the IP is wrong...
If you loaded NodeMCU then you probably have USBtoSerial adapter? Try running the following command on ESP:
print(wifi.sta.getip())
to see what is the IP of the ESP. Then reupload init.lua with the correct IP (in the first line of code).
And also check what is your gateway and netmask by running "ipconfig" in windows cmd. And correct that values in init.lua if needed.
Are you sure? yes | no
i have used another arduino as serial device...which will send "hii"serially...
void setup() {
Serial.begin(9600); }
void loop() {
Serial.print("HI!");
delay(1000); }
then i have connect 1) rx of first arduino to 11 pin of second arduino..and
2)tx of first arduino to 10 pin of second arduino
3)rx tx of second arduino to tx rx of esp respectively...
~first i programmed 1 st arduino with above program
then ~ second one with your serial code as you posted in comment
then ~ in esp firstly i set ssid and passward of my wifi then download your mysite.html code then init.lua code thats all i did it ...esp's blue light is being flickring but on obtained ip it doesn't show anything..plz help
Are you sure? yes | no
ESP should be connected to arduino's pins 1 and 2. Which are hardware serial port like this:
Are you sure? yes | no
No connections are not ok.
On the top of the arduino code are instructions:
RX is digital pin 10 (connect to TX of other device)
TX is digital pin 11 (connect to RX of other device)
The "other device" is the device tha you wan't to listen to.
Are you sure? yes | no
yes sir i did all of it...it doesn't work..connections are ok n??
Are you sure? yes | no