У меня есть код, печатающий нужные мне данные, но я не знаю, как подключить данные к IP-веб-серверу ESP8266.

   #include "DHT.h"
   #include "NewPing.h"
   #include <SoftwareSerial.h>

   #define DEBUG true
   SoftwareSerial esp8266(11,10); 
   #define DHTPIN 2        // Подключение выходного контакта DHT-11
   #define DHTTYPE DHT11   // Тип DHT — DHT 11 (AM2302)
   #define TRIGGER_PIN 4
   #define ECHO_PIN 3
   #define MAX_DISTANCE 400

    NewPing sonar(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE);
float hum;
float temp;   
    DHT dht(DHTPIN, DHTTYPE); 

void setup() {

   Serial.begin (9600);
   dht.begin();

   pinMode(TRIGGER_PIN, OUTPUT);
   pinMode(ECHO_PIN, INPUT);

   sendData("AT+RST\r\n",2000,DEBUG);            
   sendData("AT+CWMODE=2\r\n",1000,DEBUG);       
   sendData("AT+CIFSR\r\n",1000,DEBUG);          
   sendData("AT+CIPMUX=1\r\n",1000,DEBUG);       
   sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); 

 }

 void loop() 

 {

 delay(2000);  // Задержка, чтобы датчик DHT-11 мог стабилизироваться

   hum = dht.readHumidity();  // Получаем значение влажности
   temp= dht.readTemperature();  // Получаем значение температуры

 digitalWrite(TRIGGER_PIN, LOW);
 delayMicroseconds(2);
 digitalWrite(TRIGGER_PIN, HIGH);
 delayMicroseconds(10);
 digitalWrite(TRIGGER_PIN, LOW);

   long duration,distance;
   duration= pulseIn(ECHO_PIN,HIGH);
   distance = duration/58.2;

   Serial.print("Humid: ");
   Serial.print(hum);
   Serial.print(" %, Temp: ");
   Serial.print(temp);
   Serial.print(" C, ");
   Serial.print("Distance: ");
   Serial.print(distance);
   Serial.println(" cm");

   delay(50); 

  if(esp8266.available())         

   is sending a message 
 {    
   if(esp8266.find("+IPD,"))
 {
 delay(1000);

 int connectionId = esp8266.read()-48; 

  /* We are subtracting 48 from the  
  output because the read() function returns 
  the ASCII decimal value and the 
  first decimal number which is 0 starts at 48*/

  String webpage = "<h1>IOT Garbage Monitoring System</h1>";
  webpage += "<p><h2>";   

  if (distance<5)
 {
  webpage+= " Trash can is Full";
 }
   else{
   webpage+= " Trash can is Empty";
    }
   webpage += "</h2></p></body>";  
   String cipSend = "AT+CIPSEND=";
   cipSend += connectionId;
   cipSend += ",";
   cipSend +=webpage.length();
   cipSend +="\r\n";

   sendData(cipSend,1000,DEBUG);
   sendData(webpage,1000,DEBUG);    

   String closeCommand = "AT+CIPCLOSE="; 
   closeCommand+=connectionId; 
   closeCommand+="\r\n";
   sendData(closeCommand,3000,DEBUG);

   }
   } 
   }

   String sendData(String command, const int timeout, boolean debug)

   {

   String response = "";   
   esp8266.print(command); 
   long int time = millis();

   while( (time+timeout) > millis())
   {
   while(esp8266.available())
   {
   char c = esp8266.read(); 
   response+=c;
   }  
   }
   if(debug)
      {
     Serial.print(response);
      }
     return response;
      }

, 👍0


1 ответ


Лучший ответ:

0

esp-01 находится в режиме точки доступа. Чтобы получить доступ к веб-странице по IP-адресу 192.168.4.1, показанному в Serial Monitor, вам необходимо подключиться к сети Wi-Fi, созданной esp-01.

Если щелкнуть значок Wi-Fi в правом нижнем углу Windows, вы увидите список сетей Wi-Fi.

,