forked from khoih-prog/WebSockets2_Generic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Teensy-Ethernet-Server.ino
139 lines (107 loc) · 3.98 KB
/
Teensy-Ethernet-Server.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/****************************************************************************************************************************
Teensy-Ethernet-Server.ino
For Teensy with Ethernet module/shield.
Based on and modified from Gil Maimon's ArduinoWebsockets library https://github.com/gilmaimon/ArduinoWebsockets
to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, SAM DUE, Teensy boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
/****************************************************************************************************************************
Teensy Websockets Server : Minimal Teensy Websockets Server
This sketch:
1. Connects to a WiFi network
2. Starts a websocket server on port 8080
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 a Teensy board.
Originally Created : 15/02/2019
Original Author : By Gil Maimon
Original Repository : https://github.com/gilmaimon/ArduinoWebsockets
*****************************************************************************************************************************/
#include "defines.h"
#include <WebSockets2_Generic.h>
using namespace websockets2_generic;
WebsocketsServer server;
#define WEBSOCKETS_PORT 8080
void heartBeatPrint(void)
{
static int num = 1;
Serial.print("H"); // H means server alive
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(" ");
}
}
void check_status()
{
static unsigned long checkstatus_timeout = 0;
//KH
#define HEARTBEAT_INTERVAL 20000L
// Print hearbeat every HEARTBEAT_INTERVAL (20) seconds.
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
{
heartBeatPrint();
checkstatus_timeout = millis() + HEARTBEAT_INTERVAL;
}
}
void setup()
{
#if (USE_ETHERNET || USE_ETHERNET2 || USE_ETHERNET_LARGE)
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
Serial.begin(115200);
while (!Serial);
Serial.println("\nStarting Teensy-Ethernet-Server on " + String(BOARD_NAME));
Serial.println("Ethernet using " + String(ETHERNET_TYPE));
Serial.println(WEBSOCKETS2_GENERIC_VERSION);
#if ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET)
// Must use library patch for Ethernet, EthernetLarge libraries
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#endif // ( USE_ETHERNET || USE_ETHERNET_LARGE || USE_ETHERNET2 || USE_ETHERNET_ENC || USE_NATIVE_ETHERNET )
// start the ethernet connection and the server:
// Use Static IP
Ethernet.begin(mac, serverIP);
//Configure IP address via DHCP
//Ethernet.begin(mac);
server.listen(WEBSOCKETS_PORT);
for (uint8_t t = 2; t > 0; t--)
{
Serial.println("[SETUP] SERVER WAIT " + String(t));
Serial.flush();
delay(500);
}
Serial.print(server.available() ? "WebSockets Server Running and Ready on " : "Server Not Running on ");
Serial.println(BOARD_NAME);
Serial.print("IP address: ");
Serial.print(Ethernet.localIP());
Serial.print(", Port: ");
Serial.println(WEBSOCKETS_PORT); // Websockets Server Port
}
void loop()
{
check_status();
WebsocketsClient client = server.accept();
if (client.available())
{
WebsocketsMessage msg = client.readNonBlocking();
// log
Serial.print("Got Message: ");
Serial.println(msg.data());
// return echo
client.send("Echo: " + msg.data());
// close the connection
client.close();
}
//delay(1000);
}