esp加入

This commit is contained in:
2025-10-19 19:08:23 +08:00
parent e7b0457fb9
commit 71ef7cdd20
16 changed files with 948 additions and 97 deletions

View File

@@ -0,0 +1,97 @@
#include <ESP8266WiFi.h>
#ifndef STASSID
#define STASSID "601_IoT_WIFI"
#define STAPSK "wuhcw64667_jsj"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "192.168.1.111";
const uint16_t port = 1347;
WiFiClient client;
void connectToServer() {
// Serial.print("connecting to ");
// Serial.print(host);
// Serial.print(':');
// Serial.println(port);
if (!client.connect(host, port)) {
// Serial.println("connection failed");
return;
}
// Serial.println("connected");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
// Serial.println(WiFi.localIP());
connectToServer();
}
void loop() {
// static bool wait = false;
// Serial.print("connecting to ");
// Serial.print(host);
// Serial.print(':');
// Serial.println(port);
// Use WiFiClient class to create TCP connections
// This will send a string to the server
// Serial.println("sending data to server");
if (client.connected()) {
while(Serial.available()) {
char ch = static_cast<char>(Serial.read());
client.print(ch);
}
// client.println("hello from ESP8266");
}
else {
connectToServer();
}
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
// Serial.println(">>> Client Timeout !");
client.stop();
delay(1000);
return;
}
}
// Read all the lines of the reply from server and print them to Serial
// Serial.println("receiving from remote server");
// not testing 'client.connected()' since we do not need to send data here
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}
// Close the connection
// Serial.println();
// Serial.println("closing connection");
// client.stop();
// if (wait) {
// delay(300000); // execute once every 5 minutes, don't flood remote service
// }
// wait = true;
}