Buscar

Registrarse

¡Gracias por tu registro! ¡Recibirás un correo electrónico con más instrucciones para activar tu cuenta!

username already taken
¿Ya tienes una cuenta? .

Login

¿Olvidaste tu contraseña?
¿Todavía no tienes una cuenta? Registrarse para utilizar el UI-Designer y el foro o para crear tus propios proyectos.
¡Conéctate para crear temas y postear!



ESP8266-01 Example Server Code

#1 Jul. 28, 2018 00:26:47 by meccip

ESP8266-01 Example Server Code

Code that works with netIO http protocol.
After upload into ESP-01;
Set up a connection in NetIO on global page
Name: ESP
host: 192,168,1,124
port: 2564
protocol: http

Create two buttons and define the attributes for each button
ON button:
send command: /LED=ON
connection: ESP

OFF button:
send command: /LED=OFF
connection: ESP

Or create one momentary button that makes ON/OFF
send command: /LED=ON
release: /LED=OFF
connection: ESP

// include library 
#include <ESP8266WiFi.h>
// end include library

const char* ssid = "RAKKA";
const char* password = "carisima";

WiFiServer androidServer(2564); // set port number for android netIO

// setup
void setup(){
Serial.begin(115200);
delay(10);

// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(IPAddress(192,168,1,124), IPAddress(255,255,255,0), IPAddress(192,168,1,1)); // static ip,dns,gateway
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
androidServer.begin(); // start the server for android netIO
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());
}
// end setup

// main loop
void loop() {

WiFiClient client = androidServer.available();
if (!client) {
return;
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Match the request

if (request.indexOf("/LED=ON") != -1) {
digitalWrite(2, HIGH);
Serial.print("LED ON");
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(2, LOW);
Serial.print("LED OFF");
}

// Return the response
client.println("HTTP/1.1 200 OK");
client.println(""); // do not forget this one

delay(1);
Serial.println("");
Serial.println("Client disconnected");
Serial.println("");

}

#2 Jul. 28, 2018 09:51:59 by mcgiver0510

ESP8266-01 Example Server Code

@meccip
thank you very much for your example

lg

#3 Jul. 29, 2018 11:03:33 by meccip

ESP8266-01 Example Server Code

I added some lines in the code to give a delay to the button. So, when you need to light the LED only if you hold down the button for 3 seconds, use this code.

// include library 
#include <ESP8266WiFi.h>
// end include library

const char* ssid = "RAKKA";
const char* password = "carisima";

WiFiServer androidServer(2563); // set port number for android netIO

boolean rststate = false; // Save the last state for Reset Button
unsigned long rst = millis(); // store millis for Reset Button

// setup
void setup(){
Serial.begin(115200);
delay(10);

// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, LOW);

// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.config(IPAddress(192,168,1,122), IPAddress(255,255,255,0), IPAddress(192,168,1,1)); // static ip,dns,gateway
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Start the server
androidServer.begin(); // start the server for android netIO
Serial.println("Server started");

// Print the IP address
Serial.println(WiFi.localIP());
}
// end setup

// main loop
void loop() {

// Reset Button Delay - millis function and Button state on.. trigger on every 3 sec (3000 sec)
if ((millis()-rst) > 3000 && rststate == true) {
digitalWrite(2, HIGH);
Serial.print("LED ON"); }
// end Reset Button Delay millis

WiFiClient client = androidServer.available();
if (!client) {
return;
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Match the request

if (request.indexOf("/LED=ON") != -1) {
rststate = true;
rst = millis(); // reset millis
Serial.print("Waiting for delay");
}
if (request.indexOf("/LED=OFF") != -1){
digitalWrite(2, LOW);
Serial.print("LED OFF");
rststate = false;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println(""); // do not forget this one

delay(1);
Serial.println("");
Serial.println("Client disconnected");
Serial.println("");
}

Editado meccip (Jul. 29, 2018 11:04:54)