-
Notifications
You must be signed in to change notification settings - Fork 21
/
espasAP.ino
112 lines (83 loc) · 2.87 KB
/
espasAP.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WebSocketsServer.h>
#include <Hash.h>
WebSocketsServer webSocket = WebSocketsServer(81);
const char *ssid = "ESPap";
const char *password = "thereisnospoon";
const int bluePin = 13;
const int redPin = 15;
const int greenPin = 12;
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {
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=="LED"){
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13,LOW);
Serial.println("led just lit");
webSocket.sendTXT(num, "led just lit", lenght);
}
if(text.startsWith("x")){
String xVal=(text.substring(text.indexOf("x")+1,text.length()));
int xInt = xVal.toInt();
analogWrite(redPin,xInt);
Serial.println(xVal);
webSocket.sendTXT(num, "red changed", lenght);
}
if(text.startsWith("y")){
String yVal=(text.substring(text.indexOf("y")+1,text.length()));
int yInt = yVal.toInt();
analogWrite(greenPin,yInt);
Serial.println(yVal);
webSocket.sendTXT(num, "green changed", lenght);
}
if(text.startsWith("z")){
String zVal=(text.substring(text.indexOf("z")+1,text.length()));
int zInt = zVal.toInt();
analogWrite(bluePin,zInt);
Serial.println(zVal);
webSocket.sendTXT(num, "blue changed", lenght);
}
if(text=="RESET"){
analogWrite(bluePin,LOW);
analogWrite(redPin,LOW);
analogWrite(greenPin,LOW);
Serial.println("reset");
}
}
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;
}
}
void setup() {
Serial.begin(115200);
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();
}