Chat freely about the open source Javascript projects for ESP8266

User avatar
By Erni
#30044 This is an example of a webserver that lets you turn a LED on/off
I am using ESP8266-01.
The LED is connected to GPIO2
The original example can be found here:
https://github.com/esp8266-espruino/esp ... 07-Samples

Code: Select allESP8266WiFi.init();
var ipInfo = ESP8266WiFi.getIPInfo();
print("Current IP address is: " + ESP8266WiFi.getAddressAsString(ipInfo.ip));

var GPIO2 = new Pin(2);
var http = require("http");

var httpSrv = http.createServer(function(request, response) {
print(process.memory());

  response.write("<html><body>");
  if (request.url == "/on") {
      response.write("<b>LED os on</b>");
      digitalWrite(GPIO2, true);
      print ("led on");
      } else if (request.url == "/off") {
       response.write("<b>LED os off</b>");
       print ("led off");
       digitalWrite(GPIO2, false);
           
     } else {
        response.write("Welcome to ESP with JS");
    }
    response.end("<br><a href='/on'>Led ON</a><br><a href='/off'>Led OFF</a></body></html>");
});

httpSrv.listen(80);
Last edited by Erni on Fri Oct 16, 2015 5:06 am, edited 1 time in total.
User avatar
By kolban
#30061 Wow Erni ... you rock!!! We hadn't expected anyone to get so far so quickly!!! This is still early, early code.

Many thanks for taking the time to post this ... looking forward to studying it in depth.
User avatar
By Erni
#30067 Thanks kolban
Although I must admit it was mostly a copy/paste solution.

It seems very stable, I cannot get it to crash even if I try clicking as fast as I can on the links (that was a problem with LUA atleast in the early stages, caused by memmory leaks)

I am not sure how I should interprete the output from process.memory()

I get this result:
Code: Select all{ "free": 755, "usage": 268, "total": 1023, "history": 125 }


And I almost forgot to thank you and Blaz for the great work on this
User avatar
By kolban
#30069 See the following Espruino docs:

http://www.espruino.com/Reference#l_process_memory

And beyond that, I don't have much more to offer myself yet. We are still VERY early days in the port and only over time will we start to learn more. Things like JavaScript memory management is all internal knowledge (that to me) is locked up in the nature of the Espruino JS engine ... and only study and time will help me understand what is there.

Neil