D1 mini / ESP8266 files with what ive learned so far

These are some of my arduino project code, i based them off examples, i think they could be very useful to anyone learning how to use the ESP8266 or one such based microcontroller.

POST script: this sends the contents of a form , such as the contents within any text fields, check boxes, etc, which reside inside the form, to a given distination. The ESP8266 generates a html file when accessed, however my advice is, if you are messing around with learning how to do the html interactions, make a page in notepad or dreamweaver or something, and use that for sending information to the ESP, and only generate a plain text page containing things you need back from the ESP, like variables and messages and etc, its faster than HTML coding through the ESP, because every little issue will take you like 30 seconds to recompile and test, but, POST, can send to any destination, so its not neccesary the page sending info to the ESP actually be hosted by the ESP too. You could also go a step further and set your page to retrieve info from a fixed IP address, so, basically when it loads, or refreshes it pulls everything the ESP is putting out.

 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxxxxxx";
ESP8266WebServer server(8010);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/html", "<html><form action=\"/\" method=\"post\"><label for=\"tf\" ></label><input type=\"text\" name=\"tf\" /><input type=\"submit\" name=\"bt\" value=\"Submit\" /></form></html>");
// server.send(404, "text/plain", "hello from esp8266!");
digitalWrite(led, 0);
}
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
void setup(void) {
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", HTTP_POST, [&]() {
// I'd use "/relay" as a target instead of "/"
Serial.println("start----------");
Serial.println(server.arg("tf"));
Serial.println("---------end");
server.send(200, "text/plain", "OK Response bro");
});
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}

 

This one is just a general relay on/off sketch, however i also have it showing in the serial monitor what its outputting when you click anything, helped me understand what was going on.

#include <ESP8266WebServer.h>
//This example will use a static IP to control the switching of a relay. Over LAN using a web browser.
//A lot of this code have been resued from the example on the ESP8266 Learning Webpage below.
//http://www.esp8266learning.com/wemos-webserver-example.php
//CODE START
//1
#include <ESP8266WiFi.h>
// Below you will need to use your own WIFI informaiton.
//2
const char* ssid = "xxxxxxxxxxxx"; //WIFI Name, WeMo will only connect to a 2.4GHz network.
const char* password = "xxxxxxxxxxxxx"; //WIFI Password
//defining the pin and setting up the "server"
//3
int relayPin = D1; // The Shield uses pin 1 for the relay
WiFiServer server(80);
IPAddress ip(10, 0, 0, 99); // where xx is the desired IP Address
IPAddress gateway(10, 0, 0, 138); // set gateway to match your network
IPAddress subnet(255, 255, 255, 0); // set subnet mask to match your network
// void setup is where we initialize variables, pin modes, start using libraries, etc.
//The setup function will only run once, after each powerup or reset of the wemos board.
//4
void setup() {
Serial.begin(115200);
delay(10);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW);
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
// Connect to WiFi network
//5
// Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
//Trying to connect it will display dots
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
//void loop is where you put all your code. it is a funtion that returns nothing and will repeat over and over again
//6
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Serial.println(request);
// Read the first line of the request
String request = client.readStringUntil('\r');
String postdat = client.readString();
Serial.println(request);
Serial.println("start--------");
Serial.println(postdat);
Serial.println("--------end");
client.flush();
//Match the request, checking to see what the currect state is
int value = LOW;
if (request.indexOf("/relay=ON") != -1) {
digitalWrite(relayPin, HIGH);
value = HIGH;
}
if (request.indexOf("/relay=OFF") != -1) {
digitalWrite(relayPin, LOW);
value = LOW;
}
// Return the response, build the html page
//7
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<form method=\"post\"><label for=\"tf\" ></label><input type=\"text\" name=\"tf\" /><input type=\"submit\" name=\"bt\" value=\"Submit\" /></form>");
if (value == HIGH) {
client.print("Engaged (ON)");
} else {
client.print("Disengaged (OFF)");
}
client.println("<br><br><br>");
client.println("<a href=\"/relay=ON\">Click here to engage (Turn ON) the relay.</a> <br><br><br>");
client.println("<a href=\"/relay=OFF\">Click here to disengage (Turn OFF) the relay.</a><br>");
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}//END

 

Lastly, this one is for controlling a stepper motor through a stepper driver. it should work with just about any board, and the fact its outputting over pin D1 is just coincidence.

void setup() {
// put your setup code here, to run onc
pinMode(D1,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
//
// put your main code here, to run repeatedly:
digitalWrite(D1,LOW);
for(int i=0;i<900;i++){
digitalWrite(D1,HIGH);
delay(1);
digitalWrite(D1,LOW);
delay(1);
}
digitalWrite(D1,LOW);
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
}

Leave a Reply

Your email address will not be published.