ESP8266 med Websockets (3 / 5 steg)
Steg 3: ESP8266 websockets
Para cargar el firmware debes de avkorta el proyecto hårdvara-counter que se encuentre en la matta del mismo nombre en tu Arduino IDE (con el plugin de ESP instalado puedes ver como instalarlo en el siguiente instructable paso 2)
Importante: Allt incluir los datos necesarios para hacer las pruebes sv tu röd (SSID, lösenord, IPServer)
/ * * Autores: * Andres Sabas * Iddar Olivares * Creado: Julio 2015 * Contador de personas con * ESP8266 Version 12 * /
#include #include #include #include
#define SSID "Wifi namn" #define lösenord "Wifi lösenord" #define SERVER_PORT 3000 #define SERVER_IP "104.236.241.103"#define USE_SERIAL följetong
WebSocketsClient webSocket; int StatusFlag = 0;
void webSocketEvent (WStype_t typ, uint8_t * nyttolast, size_t längd) {switch(type) {mål WStype_DISCONNECTED: USE_SERIAL.printf ("[WSc] frånkopplad! \n"), bryta, fall WStype_CONNECTED: USE_SERIAL.print ("[WSc] ansluten till url:dbug.mx"); bryta; }
}
void setup() {pinMode(4,INPUT); pinMode(13,INPUT); pinMode(5,OUTPUT);
attachInterrupt (13, inPeople, RISING); attachInterrupt (4, outPeople, RISING); digitalWrite(5,HIGH);
Informacion de Depuración USE_SERIAL.begin(115200); USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println(); USE_SERIAL.println();
för (uint8_t t = 4; t > 0; t--) {USE_SERIAL.printf ("[Konfiguration] BOOT vänta %d...\n", t); USE_SERIAL.flush(); Delay(1000); }
WiFi.begin (SSID, lösenord); medan (WiFi.status()! = WL_CONNECTED) {delay(100);}
CONEXION al servidor webSocket.begin (SERVER_IP, SERVER_PORT); webSocket.onEvent(webSocketEvent);}
void loop() {webSocket.loop(); //Activacion de interrupciones attachInterrupt (13, inPeople, RISING), attachInterrupt (4, outPeople, RISING);
Envio de datos en servidor if(StatusFlag == 1) {webSocket.sendTXT ("i", 2). StatusFlag = 0; USE_SERIAL.println("in"); }
IF(StatusFlag == 2) {webSocket.sendTXT ("ut", 3). StatusFlag = 0; USE_SERIAL.println("out"); }
Delay(1500);}
void inPeople() {while(digitalRead(13)) {detachInterrupt(4);}
StatusFlag = 1;}
void outPeople() {while(digitalRead(4)) {detachInterrupt(13);}
StatusFlag = 2;}