| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /*
- Minimal Esp8266 Websockets Server
- This sketch:
- 1. Connects to a WiFi network
- 2. Starts a websocket server on port 80
- 3. Waits for connections
- 4. Once a client connects, it wait for a message from the client
- 5. Sends an "echo" message to the client
- 6. closes the connection and goes back to step 3
- Hardware:
- For this sketch you only need an ESP8266 board.
- Created 15/02/2019
- By Gil Maimon
- https://github.com/gilmaimon/ArduinoWebsockets
- */
- #include <ArduinoWebsockets.h>
- #include <ESP8266WiFi.h>
- const char* ssid = "ssid"; //Enter SSID
- const char* password = "password"; //Enter Password
- using namespace websockets;
- WebsocketsServer server;
- void setup() {
- Serial.begin(115200);
- // Connect to wifi
- WiFi.begin(ssid, password);
- // Wait some time to connect to wifi
- for(int i = 0; i < 15 && WiFi.status() != WL_CONNECTED; i++) {
- Serial.print(".");
- delay(1000);
- }
-
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP()); //You can get IP address assigned to ESP
- server.listen(80);
- Serial.print("Is server live? ");
- Serial.println(server.available());
- }
- void loop() {
- WebsocketsClient client = server.accept();
- if(client.available()) {
- WebsocketsMessage msg = client.readBlocking();
- // log
- Serial.print("Got Message: ");
- Serial.println(msg.data());
- // return echo
- client.send("Echo: " + msg.data());
- // close the connection
- client.close();
- }
-
- delay(1000);
- }
|