Skip to content

Instantly share code, notes, and snippets.

@rhagni
Last active October 21, 2018 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhagni/7d1582c0d942aadc339bae3ec5072fe8 to your computer and use it in GitHub Desktop.
Save rhagni/7d1582c0d942aadc339bae3ec5072fe8 to your computer and use it in GitHub Desktop.
ESP8266 NodeMCU example, simple http server blinking board led (First blinks at http request, second led keep blinking at interval)
#include <ESP8266WiFi.h>
#define Sprintf(data...) { \
Serial.printf(data); \
}
#define INITIATE_LED(led) { \
pinMode(led, OUTPUT); \
digitalWrite(led, LED_STATE_ON); \
}
const char* ssid = "<WIFI SSID>";
const char* password = "<WIFI PASS>";
const int LED_COM = 2;
const int LED_OTHER = 16;
const int LED_STATE_ON = LOW;
const int LED_STATE_OFF = HIGH;
const char* HTTP_RESPONSE_OK = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
const char* HTTP_RESPONSE_NOT_FOUND = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nLocation: /\r\n\r\n";
int last_blink = 0;
bool blink_state = false;
const int BLINK_INTERVAL = 500;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
INITIATE_LED(LED_COM);
INITIATE_LED(LED_OTHER);
Sprintf("\r\n\r\nConnecting to %s\r\n", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Sprintf(".");
}
Sprintf("\r\n\r\nWiFi connected");
server.begin();
Sprintf("Server started\r\n");
Serial.println(WiFi.localIP());
}
void loop() {
blink_led_other();
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
int routeStart = req.indexOf(" ") + 1;
int routeEnd = req.indexOf(" ", routeStart);
String route = req.substring(routeStart, routeEnd);
Serial.print("Route: ");
Serial.print(route);
Sprintf(" => (%i)\r\n", route.length());
client.flush();
if (route == "/") {
client.print(HTTP_RESPONSE_OK);
client.print(index());
} else if (route == "/gpio/1" || route == "/gpio/0") {
client.print(HTTP_RESPONSE_OK);
client.print(blink_led(route));
} else {
client.print(HTTP_RESPONSE_NOT_FOUND);
client.print(not_found());
}
client.flush();
delay(1);
Sprintf("Client disonnected\r\n");
}
void blink_led_other() {
int now = millis();
if ((now - last_blink) > BLINK_INTERVAL) {
blink_state = !blink_state;
last_blink = now;
// Sprintf("Led state: %s\r\n", blink_state ? "ON" : "OFF");
digitalWrite(LED_OTHER, blink_state ? LED_STATE_ON : LED_STATE_OFF);
}
}
const char* index() {
return "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>ESP8266 RULES</title></head><body><h1>Change LED state</h1><button onclick=\"request('/gpio/1')\">ON</button><button onclick=\"request('/gpio/0')\">OFF</button><script>const request=function(url){const xhr=new XMLHttpRequest();xhr.open('GET',url,true);xhr.onload=()=>{if(xhr.status>=200&&xhr.status<400){alert('Success:'+xhr.responseText);}else{alert('Error:'+xhr.responseText);}};xhr.onerror=function(err){alert(err.toString());};xhr.send();};</script></body></html>";
}
const char* not_found() {
return "<!DOCTYPE html><html><head><script>window.location.href='/';</script></head><body><script>window.location.href='/';</script></body></html>";
}
const char* blink_led(String route) {
int state = route == "/gpio/1" ? LED_STATE_ON : LED_STATE_OFF;
digitalWrite(LED_COM, state);
return state == LED_STATE_ON ? "ON" : "OFF";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment