Post topics, source code that relate to the Arduino Platform

User avatar
By Gene Dell
#55420 Can anyone show, or point me to an example of turning on/off an LED using AJAX on an ESP8266 E12 web server?

I can't seem to find any examples on this I know there must be some.

Thanks
User avatar
By mrburnette
#57153 Thanks to Peter (katz), I "flattened" the code a bit and made a multi-tab project (attached.) I also set the slider to control the NodeMCU pin #2 to vary the flash rate of the LED on the NodeMCU board (by LoLin.)

By breaking the project into 3 tabs (the Notes.h tab is just for word processing stuff...) it is easier to use for training. The active tabs are:
[*] AJAX.ino
[*] functions.h
[*] javascript.h

Note: You will need theStreaming.h library from here

MultiTab Arduino projects really simplify what is happening, IMO. Here is the main AJAX tab:
Code: Select all#include "./functions.h"
#include <Streaming.h>                  // http://arduiniana.org/libraries/streaming/

const char* ssid     = "**SSID*";       // ERASE prior to publishing
const char* password = "wifipasswd";    //   "" ditto

int LED = 2;                            // NodeMCU by LoLin

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(19200); delay(100);
  Serial << (F("... Attempting to log into router... \r\n"));

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  WiFi.mode(WIFI_STA);

  Serial << (F("\n\nBOOTING ESP8266 ..."));
  Serial << (F("Connected to ")) << ssid << endl;
  Serial << (F("Station IP address: ")) << WiFi.localIP() << endl;

  server.on("/",          handleWebsite);
  server.on("/xml",       handleXML);
  server.on("/setESPval", handleESPval);
  server.begin();
}

void loop() {
  server.handleClient();
  if (millis() > wait000) {
    buildXML();
    wait000 = millis() + 1000UL;
  }
  if (millis() > wait001) {             // slider0 controls LED blink rate
    digitalWrite(LED, !digitalRead(LED));
    wait001 = millis() + ESPval[0];
  }
}


Non-members can download here withoutsign-up.


Ray
You do not have the required permissions to view the files attached to this post.
Last edited by mrburnette on Wed Oct 26, 2016 6:07 pm, edited 1 time in total.