Post your best Lua script examples here

User avatar
By forlotto
#30853 I know you can read GPIO locally or directly off of the noemcu.

But lets say I wanted to display the status of this on another website.

I could create a basic html page on my nodemcu that would show status of all of my GPIO's to do the following:
use an iframe and post it on my web page.
scrape the information off of the node mcu and post it at page load.
These things would work.

But is there a more direct way to communicate.

For instance lets say my code invoked the gpio to be read by an http request...
example http://deviceIPhere/gpio3=?

Does the http request allow for data to be returned like the status of the pin or is it limited to 200 ok 404 etc...
The question is if it is possible and how would I make the nodemcu respond with the pin info or does it automatically respond with this info by default possibly?

I am curious I will continue to look into http request and what is possible but maybe if I can break it down into the little things it will help me sort the bits out that I don't know both so I can address the whole picture easier in smaller steps and so that I don't overwhelm others with my larger questions which would be more of a pain to answer.

Any little bit helps and yes for all of those wondering I will use google and do research on this but in the mean time I wanted to jot this down in question form both for myself to answer and if anyone else feels like contributing they can as well but please do not feel pressured to do so and I only mention this because of some words that me and a couple other forum members have had about how things are handled maybe clarity will help people from getting all wound up about a simple question.... However, I understand at times clarity provides more context to take out of context as well so it is almost a lose lose battle but to that I say have fun fight the good fight I guess.
Last edited by forlotto on Mon Oct 12, 2015 4:28 pm, edited 1 time in total.
User avatar
By jesusangel
#31118 You can ask the esp8266 for any GPIO status and return it, normally if you use a simple tcp server responding as http server you can include the status of GPIO in the code and then process it by your remote code or as you said show it in a iframe or loaded in a div.

Both options are possible but maybe the first is better, then the makeup work of the data is made by your web and not letting the esp8266 to send the whole html data including css.

I think the best option is to encode the data returned by esp8266 in xml or json and then parse it. Even with only one request cand return all GPIO's status.
Maybe this way
Code: Select all<?xml version="1.0" encoding="UTF-8"?>
<ESP8266>
<GPIO id="1">
   <value>1</value>
</GPIO>
<GPIO id="2">
   <value>0</value>
</GPIO>
</ESP8266>


Regards
User avatar
By forlotto
#31126 So the page would run on the nodemcu and show the status then I would have to parse the data from xml or json if I am understanding correctly.

Yes as I stated these are the only two ways I can really think of doing it...

This is an excellent example I have learned pages worth of reading just buy seeing this while I knew how to do this as I have done similar things when I wrote a C# program I wasn't exactly sure what the best avenue would be. XML does not look bad I haven't really messed with JSON although I see it is very popular I am really not aware of what it is all capable of to be honest but I probably should read up a little on it as much as it is used it almost seems as if it has database like capabilities IDK though.

Thank You JesusAngel you are most excellent.
User avatar
By forlotto
#31139 Basically what I want is a page that loads and lists all status of each GPIO PIN and to do it with the least amount of code possible.

for x y z do most likely will be the best approach but IDK


Hrmmm I've been thinking how the heck do I get the LUA api to display the pin status at all times on a webpage hosted on the nodemcu ...


The more and more I look at LUA the more I find out I don't know

Code: Select allrelay1status=nil
relay1status=gpio.read(1) -- this might have to be syntax corrected to relay1status=(gpio.read(1)) not entirely sure on this yeah I know newb.
print ("GPIO1="..relay1status)

I guess I'm going to have to continue reading on this one

The problem is making the read translate to show information.

I used to being able to store something in a register and then call it but when you switch from LUA to html how can you call the information from the LUA to be put into HTML or XML or JSON etc...

With html there is a variable tag but it does not grab the variable from the LUA it is its own variable that you are specifying.

So I need to somehow show this variable from LUA and migrate the read variable to the HTML on the page a bit confusing for me but I will figure it out as this scripting language is new I will need to continue to read the LUA website.


I can kind of see the reasons that kolban likes the javascript version and mmiscool likes his basic version you can more directly display results possibly in a way that makes since.

But I think I am starting to catch a little bit here and a little bit there with LUA shouldn't be long now.

I don't need a response necessarily but if you wish to respond that is fine as well this is kind of a thinking out loud post more or less.