-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_client_esp8266.ino
184 lines (147 loc) · 4.6 KB
/
tcp_client_esp8266.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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <rdm6300.h>
#include <Ticker.h>
const char* ssid = "Wi-FI";
const char* password = "Password";
const char* serverAddress = "tcp-server";
const int serverPort = 7776;
#define RDM6300_RX_PIN 5
#define READ_LED_PIN 4
#define MAX_JSON_STRING_LENGTH 20
Rdm6300 rdm6300;
WiFiClient client;
ESP8266WebServer server(80);
// Variables to store the last three serial messages
String serialMessages[3];
int serialIndex = 0;
// Variable to store error messages
String errorMessages;
// Timer to clear error messages after 10 seconds
Ticker errorClearTimer;
// Timer to clear Rfid card messages after 20 seconds
Ticker rfidCardClearTimer;
// Function to connect to Wi-Fi
void connectToWiFi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
errorMessages = "Wi-Fi connection failed. Check your credentials or restart the device.";
Serial.println("\n" + errorMessages);
}
}
// Handler for the root URL
void handleRoot() {
// Build the HTML response
String html = "<html><body>";
html += "<h1>Printer TCP client</h1>";
html += "<p>Wi-Fi Status: " + String(WiFi.status() == WL_CONNECTED ? "Connected" : "Disconnected") + "</p>";
html += "<p>IP Address: " + WiFi.localIP().toString() + "</p>";
html += "<p>Print Server: " + String(serverAddress) + "</p>";
html += "<p>Port: " + String(serverPort) + "</p>";
// Display the last three serial messages
html += "<p><b>Rfid card messages:</b></p>";
for (int i = 0; i < 1; i++) {
html += "<p>" + serialMessages[(serialIndex + i) % 1] + "</p>";
}
// Add a separate field for errors
html += "<h2>Error Log:</h2>";
html += "<p>" + errorMessages + "</p>";
// Add a link to reboot at the bottom
html += "<p><a href='/restart'>Reboot</a></p>";
html += "</body></html>";
// Send the HTML response
server.send(200, "text/html", html);
}
Ticker timer;
void restartESP() {
Serial.println("Restarting...");
ESP.restart();
}
// Feed the watchdog timer
void feedWatchdog() {
ESP.wdtFeed();
}
// Handler for device restart
void handleRestart() {
// Send the "Restarting..." response
server.send(200, "text/plain", "Restarting...");
// Set up a timer to restart the ESP after a longer delay (e.g., 5 seconds)
timer.attach(5, restartESP);
}
void clearErrorMessages() {
// Clear error messages
errorMessages = "";
Serial.println("Error messages cleared");
}
void clearRfidCardMessages() {
// Clear Rfid card messages
serialMessages[0] = "";
Serial.println("Rfid card messages cleared");
}
void setup() {
Serial.begin(115200);
connectToWiFi();
pinMode(READ_LED_PIN, OUTPUT);
digitalWrite(READ_LED_PIN, LOW);
rdm6300.begin(RDM6300_RX_PIN);
// Set up web server routes
server.on("/", HTTP_GET, handleRoot);
server.on("/restart", HTTP_GET, handleRestart);
// Set up a timer to clear error messages after 10 seconds
errorClearTimer.attach(10, clearErrorMessages);
// Set up a timer to clear Rfid card messages after 20 seconds
rfidCardClearTimer.attach(20, clearRfidCardMessages);
// Start the server
server.begin();
}
void loop() {
server.handleClient();
feedWatchdog();
handleRFIDCard();
handleWiFiReconnection();
// ... other logic
delay(100);
}
void handleRFIDCard() {
if (rdm6300.get_new_tag_id()) {
uint32_t cardSerial = rdm6300.get_tag_id();
char jsonString[MAX_JSON_STRING_LENGTH];
snprintf(jsonString, MAX_JSON_STRING_LENGTH, "%X\r\n", cardSerial);
Serial.println("Connecting to the server...");
// Save the serial message to the array
serialMessages[serialIndex] = jsonString;
serialIndex = (serialIndex + 1) % 3;
if (client.connect(serverAddress, serverPort)) {
Serial.println("Connected to the server");
client.println(jsonString);
Serial.println("Data sent to the server");
digitalWrite(READ_LED_PIN, HIGH);
delay(500);
digitalWrite(READ_LED_PIN, LOW);
client.stop();
Serial.println("Connection to the tcp server closed");
} else {
errorMessages = "Connection to the tcp server failed";
Serial.println(errorMessages);
}
}
}
void handleWiFiReconnection() {
if (WiFi.status() != WL_CONNECTED) {
errorMessages = "Wi-Fi connection lost. Reconnecting...";
Serial.println(errorMessages);
connectToWiFi();
}
}