-
Notifications
You must be signed in to change notification settings - Fork 2
/
02-server.ino
85 lines (69 loc) · 2.63 KB
/
02-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
/*
Author : Teeraphat Kullanankanjana
Version : 1.0
Date : 31/07/2024
Description : Sets up an Arduino board as a TCP server, listens for incoming connections, and sends/receives data.
Copyright (C) 2024 Teeraphat Kullanankanjana. All rights reserved.
*/
#include <WiFiNINA.h> // Include the WiFiNINA library for WiFi functionality
const char* ssid = "your_SSID"; // Replace with your network's SSID
const char* password = "your_PASSWORD"; // Replace with your network's password
const int port = 1102; // Port number to listen for incoming connections
WiFiServer server(port); // Create a WiFiServer object listening on the specified port
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
while (!Serial) {
; // Wait for the serial port to connect (needed for some boards)
}
// Check for the presence of the WiFi shield
if (WiFi.status() == WL_NO_MODULE) {
// If the WiFi module is not detected, print an error message
Serial.println("WiFi module not detected");
while (true) {
// Enter an infinite loop to halt further execution
}
}
// Attempt to connect to the specified network
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.println("...");
WiFi.begin(ssid, password); // Start the connection process
// Wait for the connection to be established
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500); // Wait for 500 milliseconds before retrying
Serial.print("."); // Print a dot to indicate the connection attempt
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
// Connection successful, print connection details
Serial.println("\nConnected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); // Print the local IP address
// Start the server
server.begin();
Serial.print("Server started on port ");
Serial.println(port);
} else {
// Connection failed
Serial.println("\nFailed to connect to the network.");
}
}
void loop() {
WiFiClient client = server.available(); // Check if a client has connected
if (client) {
Serial.println("New client connected!");
while (client.connected()) {
if (client.available()) {
String message = client.readString(); // Read the message from the client
Serial.print("Received: ");
Serial.println(message);
// Echo the message back to the client
client.println("Message received: " + message);
}
}
// Client disconnected
Serial.println("Client disconnected.");
client.stop();
}
}