如何处理来自不同NodeMCU设备的数据

时间:2019-05-22 07:53:48

标签: arduino nodemcu

我正在使用3个12E系列的NodeMCU,每个将不同位置的温度数据发送到另一个充当Master的NodeMCU。现在我面临的问题是我无法一次将所有三个设备数据发送到Thingspeak云。有人可以建议如何解决吗?预先感谢

我在不同温度区域设置了3个NodeMCU设备,这些设备连接到常见的WiFi路由器,还有一个主NodeMCU,用于接收数据并进行处理。我已经对3个单独的设备进行了编程,以发送彼此之间有10秒间隔的数据。主设备从每个设备以10秒的时间间隔接收所有3个设备数据。并将接收到的数据发布到Thingspeak云中。现在我面临的问题是我能够将2个设备数据发送到云,而无法发送另一个设备数据。我不明白问题出在哪里。

#include <SPI.h>
#include <ESP8266WiFi.h>

#define TCP_MSS 
#define LWIP_IPV6 
#define LWIP_FEATURES
#define LWIP_OPEN_SRC

String apiKey_RTD = "",apiKey_RFTD = "",apiKey_HLB = "";     //  Enter your Write API key from ThingSpeak

byte ledPin = 16,lock=0,showonce=1,dispRTD=0,dispRFTD=0,dispHLB=0;
char ssid[] = "freewifi";               // SSID of your home WiFi
char pass[] = "6666666666";               // password of your home WiFi
const char* cloudserver = "api.thingspeak.com";
String request,request1,mybigdata,myRTDvalues,myRFTDvalues,myHLBvalues,mytemp,myhumi,myfridgetemp,myfridgehumi,mylightvalue,mylightstatus;

WiFiServer server(80);                    
IPAddress ip(184,106,153,149);            // IP address of the server,
IPAddress gateway(192,168,43,1);           // gateway of your network,
IPAddress subnet(255,255,255,0);          // subnet mask of your network,WiFi 

void setup() {
  Serial.begin(115200);                   // only for debug
  WiFi.config(ip, gateway, subnet);       // forces to use the fix IP
  WiFi.begin(ssid, pass);                 // connects to the WiFi router
  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print(".");
    delay(500);
  }
  server.begin();                         // starts the server
  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.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(1,2)=="T")
        {
          myRTDvalues = request;
        }

        if(request.substring(1,2)=="F")
        {
          myRFTDvalues = request;
        }

        if(request.substring(1,2)=="L")
        {
          myHLBvalues = request;
        }

        Serial.print("Room Temperature = ");
        Serial.println(myRTDvalues);
        Serial.print("Refrigerator Temperature = ");
        Serial.println(myRFTDvalues);
//        Serial.print("Hoarding Board = ");
//        Serial.println(myHLBvalues);
//        Serial.println();
    //****************************DATA FROM RTD WILL BE PROCESSED HERE*********************************//
        if(mybigdata.substring(1,2)=="T")
        {  
         dispRTD=1;
         mytemp=mybigdata.substring(3,8);
         //Serial.print("Temperature data from RTD: "); 
         //Serial.println(mytemp);

         myhumi=mybigdata.substring(8,13);
         //Serial.print("Humidity data from RTD: "); 
         //Serial.println(myhumi);

         client.flush();
         client.println("Hi RTD!I received your data.\r");
         digitalWrite(ledPin, HIGH); 
       }   

       //**************************************DATA FROM RFTD WILL BE PROCESSED HERE************************************************//
        if(mybigdata.substring(1,2)=="F")
        {  
         dispRFTD=1;
         myfridgetemp=mybigdata.substring(4,9);
         //Serial.print("Temperature data from RFTD: "); 
         //Serial.println(myfridgetemp);

         myfridgehumi=mybigdata.substring(9,14);
         //Serial.print("Humidity data from RFTD: "); 
         //Serial.println(myfridgehumi);

         client.flush();
         client.println("Hi RFTD!I received your data.\r");
         digitalWrite(ledPin, HIGH); 
       }


        if(mybigdata.substring(1,2)=="L")
        {  
         dispHLB=1;
         mylightvalue=mybigdata.substring(3,4);
         //Serial.print("Light status value from HLB: "); 
         //Serial.println(mylightvalue);

         if(mylightvalue == "0")
         {
          mylightstatus="OFF";
         }

         if(mylightvalue == "1")
         {
          mylightstatus="ON";
         }

         client.flush();
         client.println("Hi HLB!I received your data.\r");
         digitalWrite(ledPin, HIGH); 
       }    
    }  
     client.stop();                // tarminates the connection with the client

     if(dispRTD)
      {
        //Serial.println("Master disconnected from RTD but RTD is still in connection");
       }

     if(dispRFTD)
      {
        //Serial.println("Master disconnected from RFTD but RFTD is still in connection");
        }

     if(dispHLB)
      {
        //Serial.println("Master disconnected from HLB but HLB is still in connection");
        }

     lock=1;
     showonce=0;
  }


  else
   { 
    if(showonce==1)
      {
        if(dispRTD)
         {Serial.println("\n!!!!!RTD not connected!!!!!\n");}


        if(dispRFTD)
         {Serial.println("\n!!!!!RFTD not connected!!!!!\n");}

        if(dispHLB)
         {Serial.println("\n!!!!!HLB not connected!!!!!\n");}

        showonce=0;
      }
  }



 //***************************data to thingspeak******************************************//
  if(lock==1)
  {
     if (client.connect(cloudserver,80))   //   "184.106.153.149" or api.thingspeak.com
      {  
       if(dispRTD)
       {
         Serial.print("Room Temperature data from RTD: "); 
         Serial.println(mytemp);
         Serial.print("Room Humidity data from RTD: "); 
         Serial.println(myhumi);

         String sendData = apiKey_RTD+"&field1="+String(mytemp)+"&field2="+String(myhumi)+"\r\n\r\n"; 

         Serial.println(sendData);

         client.print("POST /update HTTP/1.1\n");
         client.print("Host: api.thingspeak.com\n");
         client.print("Connection: close\n");
         client.print("X-THINGSPEAKAPIKEY: "+apiKey_RTD+"\n");
         client.print("Content-Type: application/x-www-form-urlencoded\n");
         client.print("Content-Length: ");
         client.print(sendData.length());
         client.print("\n\n");
         client.print(sendData);

        //Serial.println("Connecting to Thingspeak to send RTD data...");   
        //dispRTD=0; 

       }


       if(dispRFTD)
       {
         Serial.print("Fridge Temperature data from RFTD: "); 
         Serial.println(myfridgetemp);
         Serial.print("Fridge Humidity data from RFTD: "); 
         Serial.println(myfridgehumi);



        String sendData = apiKey_RFTD+"&field1="+String(myfridgetemp)+"&field2="+String(myfridgehumi)+"\r\n\r\n"; 

        Serial.println(sendData);

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+apiKey_RFTD+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(sendData.length());
        client.print("\n\n");
        client.print(sendData);

        //Serial.println("Connecting to Thingspeak to send RFTD data...");    
        //dispRFTD=0;

       }  


       if(dispHLB)
       {
        Serial.print("Light Status: ");
        Serial.print(mylightstatus);

        String sendData = apiKey_HLB+"&field1="+String(mylightvalue)+"&field2="+String(mylightstatus)+"\r\n\r\n"; 

        Serial.println(sendData);

        client.print("POST /update HTTP/1.1\n");
        client.print("Host: api.thingspeak.com\n");
        client.print("Connection: close\n");
        client.print("X-THINGSPEAKAPIKEY: "+apiKey_HLB+"\n");
        client.print("Content-Type: application/x-www-form-urlencoded\n");
        client.print("Content-Length: ");
        client.print(sendData.length());
        client.print("\n\n");
        client.print(sendData);



        //Serial.println("Connecting to Thingspeak to send HLB data...");
        //dispHLB=0; 

       }        
      }
//       Serial.println("Sending to Cloud....");
   // delay(5000);
    lock=0;
    showonce=1;
   }


}

预期结果是我需要将所有3个设备数据发布到Thingspeak云中,并且在主设备上接收到时每个设备的数据都不会冲突

0 个答案:

没有答案