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 pathESP32_Ethernet-RepeatingClient.ino
230 lines (177 loc) · 6.49 KB
/
ESP32_Ethernet-RepeatingClient.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/****************************************************************************************************************************
ESP32_Ethernet-RepeatingClient.ino
For ESP32 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, RP2040 and SAMD21/SAMD51 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
*****************************************************************************************************************************/
/****************************************************************************************************************************
ESP32 Ethernet Websockets Repeating Client
This sketch:
1. Connects to an Ethernet network
2. Connects to a Websockets server
3. Sends the websockets server a message ("Hello to Server from ......")
4. Prints all incoming messages while the connection is open
5. Repeat 2-4 every REPEAT_INTERVAL (10 seconds)
Hardware:
For this sketch you only need an ESP32 board + W5x00/ENC Ethernet
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;
WebsocketsClient client;
// Select the IP address according to your local network
IPAddress serverIP(192, 168, 2, 30);
const char* websockets_server_host = "192.168.2.30"; //Enter server address
//const char* websockets_server_host = "serverip_or_name"; //Enter server address
#define WEBSOCKETS_PORT 8080
const uint16_t websockets_server_port = WEBSOCKETS_PORT; // Enter server port
void onEventsCallback(WebsocketsEvent event, String data)
{
(void) data;
if (event == WebsocketsEvent::ConnectionOpened)
{
Serial.println("Connnection Opened");
}
else if (event == WebsocketsEvent::ConnectionClosed)
{
Serial.println("Connnection Closed");
}
else if (event == WebsocketsEvent::GotPing)
{
Serial.println("Got a Ping!");
}
else if (event == WebsocketsEvent::GotPong)
{
Serial.println("Got a Pong!");
}
}
void initEthernet()
{
#if (USING_SPI2)
#if defined(CUR_PIN_MISO)
LOGWARN(F("Default SPI pinout:"));
LOGWARN1(F("MOSI:"), CUR_PIN_MOSI);
LOGWARN1(F("MISO:"), CUR_PIN_MISO);
LOGWARN1(F("SCK:"), CUR_PIN_SCK);
LOGWARN1(F("SS:"), CUR_PIN_SS);
LOGWARN(F("========================="));
#endif
#else
LOGWARN(F("Default SPI pinout:"));
LOGWARN1(F("MOSI:"), MOSI);
LOGWARN1(F("MISO:"), MISO);
LOGWARN1(F("SCK:"), SCK);
LOGWARN1(F("SS:"), SS);
LOGWARN(F("========================="));
#endif
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 5 //22 // For ESP32
#endif
LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN);
// For other boards, to change if necessary
#if ( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
// Must use library patch for Ethernet, EthernetLarge libraries
// ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
// ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
#elif USE_CUSTOM_ETHERNET
// You have to add initialization for your Custom Ethernet here
// This is just an example to setCSPin to USE_THIS_SS_PIN, and can be not correct and enough
Ethernet.init(USE_THIS_SS_PIN);
#endif //( USE_ETHERNET_GENERIC || USE_ETHERNET_ENC )
/////////////////////////////////////
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[0], ip);
Ethernet.begin(mac[index]);
#if USE_ETHERNET_GENERIC
LOGWARN(F("========================="));
// Just info to know how to connect correctly
// To change for other SPI
LOGWARN(F("Currently Used SPI pinout:"));
LOGWARN1(F("MOSI:"), PIN_MOSI);
LOGWARN1(F("MISO:"), PIN_MISO);
LOGWARN1(F("SCK:"), PIN_SCK);
LOGWARN1(F("SS:"), PIN_SS);
LOGWARN(F("========================="));
#endif
Serial.print(F("Using mac index = "));
Serial.println(index);
}
void setup()
{
#if (defined(ETHERNET_WITH_SD_CARD) && ETHERNET_WITH_SD_CARD)
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
#endif
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(500);
Serial.println("\nStarting ESP32_Ethernet-RepeatingClient on " + String(BOARD_NAME));
Serial.println("Ethernet using " + String(ETHERNET_TYPE));
Serial.println(WEBSOCKETS2_GENERIC_VERSION);
initEthernet();
Serial.print("WebSockets Client IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Connecting to WebSockets Server @");
Serial.println(websockets_server_host);
// run callback when messages are received
client.onMessage([&](WebsocketsMessage message)
{
Serial.print("Got Message: ");
Serial.println(message.data());
});
// run callback when events are occuring
client.onEvent(onEventsCallback);
}
void sendMessage()
{
// try to connect to Websockets server
bool connected = client.connect(websockets_server_host, websockets_server_port, "/");
if (connected)
{
Serial.println("Connected!");
String WS_msg = String("Hello to Server from ") + BOARD_NAME;
client.send(WS_msg);
}
else
{
Serial.println("Not Connected!");
}
}
void checkToSendMessage()
{
#define REPEAT_INTERVAL 10000L
static unsigned long checkstatus_timeout = 1000;
// Send WebSockets message every REPEAT_INTERVAL (10) seconds.
if (millis() > checkstatus_timeout)
{
sendMessage();
checkstatus_timeout = millis() + REPEAT_INTERVAL;
}
}
void loop()
{
checkToSendMessage();
// let the websockets client check for incoming messages
if (client.available())
{
client.poll();
}
}