[go: up one dir, main page]

0% found this document useful (0 votes)
136 views3 pages

Esp 8266 Websocket

This document contains code for an Arduino sketch that allows control of 3 digital outputs (pins 13, 15, 12) via a web socket connection. It connects to a WiFi access point, sets up the web socket server on port 80, and defines functions to handle incoming messages to turn the outputs on and off. When a client connects, it sends the IP address. Messages to turn individual or all outputs on/off are handled by parsing the text and calling digitalWrite on the appropriate pins.

Uploaded by

Khristian Dulcey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views3 pages

Esp 8266 Websocket

This document contains code for an Arduino sketch that allows control of 3 digital outputs (pins 13, 15, 12) via a web socket connection. It connects to a WiFi access point, sets up the web socket server on port 80, and defines functions to handle incoming messages to turn the outputs on and off. When a client connects, it sends the IP address. Messages to turn individual or all outputs on/off are handled by parsing the text and calling digitalWrite on the appropriate pins.

Uploaded by

Khristian Dulcey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Arduino.

h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WebSocketsServer.h>
#include <Hash.h>
WebSocketsServer webSocket = WebSocketsServer(80);
const char *ssid = "HomeWifi";
const char *password = "home123456";
const int output1 = 13; //D7
const int output2 = 15; //D8
const int output3 = 12; //D6

void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {


Serial.println("request received from browser");
webSocket.sendTXT(num, "succesfull connection");

switch(type) {
case WStype_DISCONNECTED:
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
}
break;
case WStype_TEXT:
{
String text = String((char *) &payload[0]);

if(text=="output1on"){
Serial.println("output1 ala");
digitalWrite(13,HIGH);
Serial.println("output1 ON");
}

if (text=="output1off"){
digitalWrite(13,LOW);
Serial.println("output1 OFF");
}

if(text=="output2on"){

digitalWrite(15,HIGH);
Serial.println("output2 ON");
}

if (text=="output2off"){
digitalWrite(15,LOW);
Serial.println("output2 OFF");
}

if(text=="output3on"){

digitalWrite(12,HIGH);
Serial.println("output2 ON");
}

if (text=="output3off"){
digitalWrite(12,LOW);
Serial.println("output2 OFF");
}

if(text=="outputsON"){

digitalWrite(13,HIGH);
digitalWrite(15,HIGH);
digitalWrite(12,HIGH);
Serial.println("All ON");
}

if(text=="outputsOFF"){

digitalWrite(13,LOW);
digitalWrite(15,LOW);
digitalWrite(12,LOW);
Serial.println("All OFF");
}
}

webSocket.sendTXT(num, payload, lenght);


webSocket.broadcastTXT(payload, lenght);
break;

case WStype_BIN:

hexdump(payload, lenght);

// echo data back to browser


webSocket.sendBIN(num, payload, lenght);
break;
}

WebSocketHandler wsHandler = new WebSocketHandler()


{
@Override
public void configure(WebSocketServletFactory factory)
{
factory.getPolicy().setIdleTimeout(1500);
factory.register(MyWebSocketAdapter.class);

}
}

void setup() {

Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(15,OUTPUT);
pinMode(12,OUTPUT);
WiFi.softAP(ssid, password);

IPAddress myIP = WiFi.softAPIP();


Serial.print("AP IP address: ");
Serial.println(myIP);
webSocket.begin();
webSocket.onEvent(webSocketEvent);
}
void loop() {
webSocket.loop();
}

You might also like