This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSAMD_ServerAllFunctionsDemo.ino
154 lines (115 loc) · 4.64 KB
/
SAMD_ServerAllFunctionsDemo.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/****************************************************************************************************************************
SAMD_ServerAllFunctionsDemo.ino
For SAMD21/SAMD51 with WiFiNINA 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 and SAMD21/SAMD51 boards besides ESP8266 and ESP32
The library provides simple and easy interface for websockets (Client and Server).
Example first created on: 10.05.2018
Original Author: Markus Sattler
Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic
Licensed under MIT license
*****************************************************************************************************************************/
#include "defines.h"
#include <WebSockets2_Generic.h>
#include <WiFiWebServer.h>
using namespace websockets2_generic;
WebsocketsServer SocketsServer;
#define WEBSOCKETS_PORT 8080
// Change pins according to your boards
#define RED_LED 7
#define GREEN_LED 6
#define BLUE_LED 5
WiFiWebServer server(80);
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
Serial.println("\nStarting SAMD_ServerAllFunctionsDemo with WiFiNINA on " + String(BOARD_NAME));
Serial.println(WEBSOCKETS2_GENERIC_VERSION);
for (uint8_t t = 4; t > 0; t--)
{
Serial.println("[SETUP] BOOT WAIT " + String(t));
Serial.flush();
delay(1000);
}
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
digitalWrite(RED_LED, 1);
digitalWrite(GREEN_LED, 1);
digitalWrite(BLUE_LED, 1);
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
return;
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION)
{
Serial.println("Please upgrade the firmware");
}
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// 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);
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("Connected to Wifi, IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("\nNo WiFi");
return;
}
SocketsServer.listen(WEBSOCKETS_PORT);
Serial.print(SocketsServer.available() ? "WebSockets Server Running and Ready on " : "Server Not Running on ");
Serial.println(BOARD_NAME);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print(", Port: ");
Serial.println(WEBSOCKETS_PORT); // Websockets Server Port
// handle index
server.on("/", []()
{
String wsServer = String("ws://192.168.2.95:") + WEBSOCKETS_PORT + "/";
Serial.print("wsServer: ");
Serial.println(wsServer);
// send index.html
server.send(200, "text/html", "<html><head><script>var connection = new WebSocket(wsServer, ['arduino']);connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
});
server.begin();
digitalWrite(RED_LED, 0);
digitalWrite(GREEN_LED, 0);
digitalWrite(BLUE_LED, 0);
}
WebsocketsClient* client = NULL;
void loop()
{
server.handleClient();
client = new WebsocketsClient;
if (client)
{
*client = SocketsServer.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();
}
delete client;
}
}