Created
July 17, 2023 15:50
-
-
Save neolead/a806cf1f1056d9efbc4a81984c701319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Хорошо, давайте создадим два скрипта для Arduino: один будет работать в качестве WiFi сервера (Device 1), второй - в качестве клиента (Device 2). Device 1 будет подключен к компьютеру через USB и будет обеспечивать двустороннюю связь с Device 2, который в свою очередь будет подключен к внешнему устройству через TTL. | |
WiFi Сервер (Device 1): | |
#include <WiFi.h> | |
// WiFi network name and password: | |
const char * networkName = "Your_SSID"; | |
const char * networkPswd = "Your_PASSWORD"; | |
// IP address to connect to: | |
IPAddress local_IP(192,168,1,184); | |
IPAddress gateway(192,168,1,1); | |
IPAddress subnet(255,255,255,0); | |
// Create a WiFi server instance | |
WiFiServer server(23); | |
void setup() { | |
Serial.begin(115200); | |
// Configuring IP address | |
if (!WiFi.config(local_IP, gateway, subnet)) { | |
Serial.println("STA Failed to configure"); | |
} | |
WiFi.begin(networkName, networkPswd); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Setting up WiFi..."); | |
} | |
server.begin(); | |
} | |
void loop() { | |
WiFiClient client = server.available(); | |
if (client) { | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
Serial.write(c); | |
} | |
} | |
client.stop(); | |
} | |
while (Serial.available() > 0) { | |
char c = Serial.read(); | |
client.write(c); | |
} | |
} | |
WiFi Клиент (Device 2): | |
#include <WiFi.h> | |
const char * ssid = "Your_SSID"; | |
const char * password = "Your_PASSWORD"; | |
IPAddress remote_IP(192,168,1,184); | |
WiFiClient client; | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi..."); | |
} | |
client.connect(remote_IP, 23); | |
} | |
void loop() { | |
if (client.connected()) { | |
while (client.available() > 0) { | |
char c = client.read(); | |
Serial.write(c); | |
} | |
} else { | |
Serial.println("Connection lost!"); | |
client.connect(remote_IP, 23); | |
} | |
while (Serial.available() > 0) { | |
char c = Serial.read(); | |
client.write(c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment