“我正在设置两个NodeMCu设备,其中一个名为NodeA的NodeMCU将连接到WiFi路由器,而另一个名为NodeB的NodeMCU设备将与NodeA通信以发送由NodeB捕获的温度值。”
“为了避免对WiFi凭据进行硬编码,我使用了WiFi Manager库,以便我们可以创建一个将在可用网络中显示的SSID。该库创建一个SoftAP,该SoftAP具有自己的IP地址,通过它我们可以配置WiFi路由器凭据。同时,NodeB设备还需要使用相同的SoftAP IP与NodeA设备进行通信,我将这个IP和SoftAP SSID硬编码到我的NodeB代码中,以便在其通电时它开始连接到NodeA。但是NodeB和NodeA之间没有建立连接。如何解决此问题?”
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
// Set web server port number to 80
WiFiServer server(80);
String apiKey_RTD = "AMWCKCGM6MCUTHW2",apiKey_RFTD = "XN4BVE0THAI6J2M9",apiKey_HLB = "VMJ4R2XQ8ZA43KSB"; // Enter your Write API key from ThingSpeak
const char *ssidAP = "myNodeMCU"; // The name of the Wi-Fi network that will be created
const char *passwordAP = "connectme"; // The password required to connect to it, leave blank for an open network
int channel=2,max_connection=4;
boolean hidden=false;
// Variable to store the HTTP request
//String header;
byte ledPin = 16,lock=0,showonce=1,dispRTD1=0,dispRTD2=0,dispHLB=0;
String request,request1,mybigdata,myRTD1values,myRTD2values,myHLBvalues,mytemp1,myhumi1,mytemp2,myfaht2,mylightvalue,mylightstatus,lightoffvalue,lightonvalue;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
WiFi.softAP(ssidAP, passwordAP, channel, hidden, max_connection); // Start the access point
WiFiManager wifiManager;
// Uncomment and run it once, if you want to erase all the stored information
// wifiManager.resetSettings();
// void setAPStaticIPConfig(IPAddress IP, IPAddress gateway, IPAddress Subnet);
wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
boolean portal=wifiManager.autoConnect("StorM3","password9"); //start another access point with 10.0.1.1 to configure WiFi credentials
// or use this for auto generated name ESP + ChipID
//boolean portal=wifiManager.autoConnect();
// if you get here you have connected to the WiFi
if(portal)
{
Serial.println("Connected.");
}
server.begin();
Serial.print("Access Point \"");
Serial.print(ssidAP);
Serial.println("\" started");
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // some parameters from the network
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
Serial.print("Networks: ");
Serial.println(WiFi.scanNetworks());
pinMode(ledPin, OUTPUT);
delay(2000);
}
void loop(){
WiFiClient client = server.available();
if (client)
{
if (client.connected())
{
// Serial.printf("Stations connected = %d\n", WiFi.softAPgetStationNum());
// delay(1000);
Serial.println("Client connected");
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
//***************************Temperature receive code added from here*******************************//
request = client.readStringUntil('\r'); // receives the message from the client
//Serial.println(request);
mybigdata = request;
if(request.substring(3,4)=="1")
{
myRTD1values = request;
}
Serial.print("RTD1 Temperature = ");
Serial.println(myRTD1values);
//****************************DATA FROM RTD1 WILL BE PROCESSED HERE*********************************//
if(mybigdata.substring(3,4)=="1")
{
dispRTD1=1;
mytemp1=myRTD1values.substring(4,8);
//Serial.print("Temperature data from RTD: ");
//Serial.println(mytemp1);
myhumi1=myRTD1values.substring(8,12);
//Serial.print("Humidity data from RTD: ");
//Serial.println(myhumi1);
client.flush();
client.println("Hi RTD1!I received your data.\r");//String length 28
digitalWrite(ledPin, HIGH);
}
}
client.stop(); // tarminates the connection with the client
if(dispRTD1)
{
//Serial.println("Master disconnected from RTD but RTD is still in connection");
}
showonce=0;
}
else
{
if(showonce==1)
{
if(dispRTD1)
{Serial.println("\n!!!!!RTD not connected!!!!!\n");}
showonce=0;
}
}
}