-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path01-client.ino
93 lines (77 loc) · 2.99 KB
/
01-client.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
/*
Author : Teeraphat Kullanankanjana
Version : 1.1
Date : 31/07/2024
Description : Connects to a specified WiFi network, establishes a TCP connection to a specified IP and port,
and sends/receives data through the socket.
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
IPAddress serverIP(192, 168, 31, 50); // Replace with the IP address you want to connect to
const int port = 1102; // Replace with the port you want to connect to
WiFiClient client; // Create a WiFiClient object
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
// Attempt to connect to the server
Serial.print("Connecting to server at ");
Serial.print(serverIP);
Serial.print(":");
Serial.println(port);
if (client.connect(serverIP, port)) {
Serial.println("Connected to server!");
// Send data to the server
client.println("Hello from Arduino!");
Serial.println("Data sent to server.");
// Wait for a response from the server
while (client.connected() || client.available()) {
if (client.available()) {
String response = client.readString();
Serial.print("Received from server: ");
Serial.println(response);
}
}
// Close the connection
client.stop();
Serial.println("Connection closed.");
} else {
// Connection failed
Serial.println("Failed to connect to server.");
}
} else {
// Connection failed
Serial.println("\nFailed to connect to the network.");
}
}
void loop() {
// The loop function is empty because no repeated actions are needed
}