domingo, 3 de diciembre de 2017

Desarrollo del proyecto

Características principales del ESP8266

Una de las tendencias más relevantes del movimiento Maker es el IoT. Y dentro de esta tecnología cabe destacar un componente que está haciendo furor entre los amantes de los objetos conectados. El ESP8266 es el microcontrolador más utilizado para conectar cosas. 


Ya te he hablado en diferentes artículos y capítulos del podcast de este microcontrolador. Sin entrar en demasiados detalles, este dispositivo es un microcontrolador con WiFi con las siguientes características:
  • Microcontrolador Cadence Tensilica Xtensa LX106 a 80 ó 160 MHz
  • Hasta 4 MB de memoria flash externa al microcontrolador
  • 80 kB de memoria RAM
  • 10 puertos de entrada salida digital
  • Compatibilidad con diferentes protocolos de comunicación (I2C, SPI, I2S, 1-Wire, UART)
  • Interfaz de red WiFi B, G y N con encriptación WEP ó WPA/WPA2 y protocolos TCP/IPv4
Hoja de datos del esp8266
http://espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf

Programa
/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be
 *  printed to Serial when the module is connected.
 */
#include <ESP8266WiFi.h>
const char* ssid = "charly";
const char* password = "";
int val=0;
int val2=0;
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
  Serial.begin(115200);
  delay(1500);
  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
    // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
    WiFi.begin(ssid, password);
    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.println(WiFi.localIP());
}
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);
  } 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();
    if (req.indexOf("/gpio/0") != -1)
    val = 0;
  else if (req.indexOf("/gpio/1") != -1)
    val = 1;
  else if (req.indexOf("/gpio/00") != -1)
    funcion(0,0);
  else if (req.indexOf("/gpio/01") != -1)
    funcion(0,1);
  else if (req.indexOf("/gpio/10") != -1)
    funcion(1,0);
  else if (req.indexOf("/gpio/11") != -1)
    funcion(1,1);
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }
  // Set GPIO2 according to the request
  digitalWrite(2, val);
  digitalWrite(1, val2);
    client.flush();
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO esta ";
  s += (val)?"high":"low";
  s += "\n GPI1is esta :";
  s += (val2)?"high":"low";
  s += "</html>\n";
  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");
  // The client will actually be disconnected
  // when the function returns and 'client' object is detroyed
}
void funcion(int a,int b){
  val=a;
  val2=b;

}


No hay comentarios:

Publicar un comentario

Definición del proyecto

La conectividad a la Red es la base del Internet de las Cosas. Sin embargo, los objetos conectados deben desempeñar un rol importante y pod...