A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By bmichels
#2510 @zarya
Thanks for the great starting point!
I took your example and changed the port to 80 and modified the at_user_input function.
Now, you can use your browser to toggle the LED. And it's on a 1 second poll, so multiple devices can connect and stay updated.
However, I have noticed that after a while my unit locks up even with only one device polling. I don't have the WDT enabled and I don't see anything on the serial connection that could be the cause.
Also, this doesn't handle the web page icon request made after the initial GET /.
Code: Select allvoid ICACHE_FLASH_ATTR
at_user_input(void *arg, char *pdata, unsigned short len)
{
  struct espconn *pespconn = (struct espconn *)arg;
  at_linkConType *linkTemp = (at_linkConType *)pespconn->reverse;
  char temp[1024]; // need a larger buffer to hold the html
  if (pdata[0] == 'L' && pdata[1] == 'E' && pdata[2] == 'D') {
    if (pdata[3] == '1') {
      uart0_sendStr("\r\nSet led ON\r\n");
      gpio_output_set(BIT2, 0, BIT2, 0);
      LED = 1;
    } else if (pdata[3] == '0') {
      uart0_sendStr("\r\nSet led OFF\r\n");
      gpio_output_set(0, BIT2, BIT2, 0);
      LED = 0;
    }
    else
    {
      os_sprintf(temp, "LED: %d\r\n",LED);
      espconn_sent(pLink[linkTemp->linkId].pCon, temp, strlen(temp));
    }
  }
  if (pdata[0] == 'G' && pdata[1] == 'E' && pdata[2] == 'T') {
    if (pdata[5] == ' ') { // initial get, send form
      uart0_sendStr("\r\nIndex Request\r\n");
      os_sprintf(temp,
        "<!DOCTYPE html>"
        "<html>"
          "<head>"
            "<title>LED Control</title>"
            "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1,user-scalable=no\">"
            "<style>"
              "body{font-size:18px}"
              "input{width:100%;height:4em}"
            "</style>"
            "<script src=\"//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>"
          "</head>"
          "<body>"
            "<h1>LED: <span id=\"s\">%s</span></h1>"
            "<input type=\"button\" id=\"n\" value=\"On\" />"
            "<input type=\"button\" id=\"f\" value=\"Off\" />"
            "<script>"
              "var nbtn=$('#n'),fbtn=$('#f');"
              "function update(){"
                "if($('#s').html()=='Off') {"
                  "fbtn.hide();nbtn.show();"
                "}else{"
                  "fbtn.show();nbtn.hide();"
                "}"
              "}"
              "update();"
              "setInterval(function(){"
                "$.get('q',{},function(d){"
                  "$('#s').html(d);"
                  "update();"
                "});"
              "}, 1000);"
              "nbtn.off('click').on('click',function(){"
                "$.get('n',{},function(d){"
                  "$('#s').html(d);"
                  "update();"
                "});"
              "});"
              "fbtn.off('click').on('click',function(){"
                "$.get('f',{},function(d){"
                  "$('#s').html(d);"
                  "update();"
                "});"
              "});"
            "</script>"
          "</body>"
        "</html>",(LED == 0 ? "Off" : "On"));
    } else if (pdata[5] == 'n') { // turn on request
      uart0_sendStr("\r\nSet led ON\r\n");
      gpio_output_set(BIT2, 0, BIT2, 0);
      LED = 1;
      os_sprintf(temp,"On");
    } else if (pdata[5] == 'f') { // turn off request
      uart0_sendStr("\r\nSet led OFF\r\n");
      gpio_output_set(0, BIT2, BIT2, 0);
      LED = 0;
      os_sprintf(temp,"Off");
    } else if (pdata[5] == 'q') { // state request
      uart0_sendStr("\r\nSend current state\r\n");
      if (LED == 0) {
        os_sprintf(temp,"Off");
      } else {
        os_sprintf(temp,"On");
      }
    }
    espconn_sent(pLink[linkTemp->linkId].pCon, temp, strlen(temp));
    espconn_disconnect(pLink[linkTemp->linkId].pCon);
  }
}