我正在使用三个部分:
ESP32
模式:STA + AP
传送资料至:我的手机热点
IP:192.168.4.1
服务器端口:80
ESP-01(A)
模式:STA + AP
发送数据至:ESP32
IP:192.168.0.80
服务器端口:8080
ESP-01(B)
模式:STA + AP
将数据发送到:ESP-01(A)(尚未成功)
IP:192.168.0.50
服务器端口:50
我打算增加esp-01的内容
问题是当我通过将GPIO0接地并将其重置为esp(B)时,esp(A)立即停止向esp32发送数据
一旦我断开esp(B),esp(A)再次正常发送
我用单独的电源为esp(B)供电
我播放了频道编号,仍然没有区别
我知道我可以忽略该问题,因为我将不再上传任何内容,但是我担心自己做错了,该怎么办?
这是esp32代码:
#include <WiFi.h>
WiFiServer server(80);
char Pssid[] = "toOomy";
char Ppass[] = "1222222224";
char assid[] = "my_net";
char apass[] = "1222222224";
int sensor_ID= -1;
float sensor_V= -1;
float sensor_I= -1;
float sensor_P= -1;
void responseToClient(){
String response= "okok";
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("connected");
String request = client.readStringUntil('\r');
if (sensor_ID == 0){
response= "1";
}
client.flush();
Serial.println(request);
client.println(response); // sends the temperature to the client
client.stop(); // disconnects the client
}
Serial.println(sensor_ID);
Serial.println(sensor_V);
Serial.println(sensor_I);
Serial.println(sensor_P);
}
}
void handleNew(){
//IPAddress newIP(192, 168, 1, 1);
responseToClient();
}
void setup() {
Serial.begin(115200);
// starts the WiFi server
// WiFi.config(ip, gateway, subnet);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(assid, apass, 5);
WiFi.begin(Pssid,Ppass, 5);
while (WiFi.status() != WL_CONNECTED) {
Serial.println('.');
delay(500);
}
server.begin();
Serial.println(WiFi.softAPIP());
}
void loop() {
handleNew();
}
这是esp-01的代码:
#include <ESP8266WiFi.h>
// #include <ArduinoJson.h>
WiFiClient client;
//// for the first esp-01 ////
IPAddress next_Server_IP1(192, 168, 4, 1);
int next_server_port1 = 80;
int ID = 1;
WiFiServer local_server(8080);
IPAddress ip(192, 168, 0, 80);
char ssid[] = "my_net";
char pass[] = "1222222224";
char assid[] = "Amy_net1";
char apass[] = "1222222224";
int wifi_channel= 5;
int softAP_channel= 5;
float V = 230.4;
float I = 2.3;
float P = 2678.9;
//// for the second esp-01 ////
IPAddress next_Server_IP2(192, 168, 0, 80);
int next_server_port2 = 8080;
// int ID = 2;
// WiFiServer local_server(50);
// IPAddress ip(192, 168, 0, 50);
// char ssid[] = "Amy_net1";
// char pass[] = "1222222224";
// char assid[] = "Amy_net2";
// char apass[] = "1222222224";
// int wifi_channel= 5;
// int softAP_channel= 5;
// float V = 200.5;
// float I = 10.5;
// float P = 3000.5;
IPAddress gateway(192, 168, 0, 2); // WiFi router's IP ((I AM NOT SURE WHAT SHOULD I DO HERE))
IPAddress subnet(255, 255, 255, 0);
String answer = "\n";
String SensorData()
{
String Data = "{\"Sensor_Data\":{"
"\"sensor_ID\":\"sensor_ID_val\","
"\"sensor_V\":\"sensor_V_val\","
"\"sensor_I\":\"sensor_I_val\","
"\"sensor_P\":\"sensor_P_val\"}}\r";
Data.replace("sensor_ID_val", String(ID));
Data.replace("sensor_V_val", String(V));
Data.replace("sensor_I_val", String(I));
Data.replace("sensor_P_val", String(P));
return (Data);
}
void responseToClient()
{
String response = "Thanks";
WiFiClient sensorClient = local_server.available();
if (sensorClient)
{
if (sensorClient.connected())
{
Serial.println("connected");
String request = sensorClient.readStringUntil('\r'); // reads the message from the client
Serial.println(request);
sensorClient.flush();
Serial.println(request);
sensorClient.println(response); // sends the temperature to the client
sensorClient.stop(); // disconnects the client
}
}
}
void requestToServer(String request, IPAddress IP, int port)
{
if (client.connect(IP, port))
{ // connects to the server
Serial.println("connected");
client.println(request); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
Serial.println(String(answer));
client.stop();
}
}
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(assid, apass, softAP_channel);
// WiFi.config(ip, gateway, subnet);
delay(300);
WiFi.softAPConfig(ip, gateway, subnet);
WiFi.begin(ssid, pass, wifi_channel);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
local_server.begin();
}
void loop()
{
Serial.println("request:");
if (ID == 1)
{
requestToServer(SensorData(), next_Server_IP1, next_server_port1);
}
else if (ID > 1)
{
Serial.println("ID>1");
requestToServer(SensorData(), next_Server_IP2, next_server_port2 );
}
Serial.println("response:");
responseToClient();
}