0

Here is the schematic I followed:

Schematic

Here also the link: https://www.circuito.io/app?components=10167,11021,12083,13678,13959

#include "DHT.h"
#include "NewPing.h"
#include <SoftwareSerial.h>
#define DEBUG true

SoftwareSerial esp8266(11,10);

#define DHTPIN 2       // DHT-11 Output Pin connection
#define DHTTYPE DHT11   // DHT Type is 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);  // Delay so DHT-11 sensor can stabalize

  hum = dht.readHumidity();  // Get Humidity value
  temp= dht.readTemperature();  // Get Temperature value

  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;
}

I've been trying to solve this problem for almost a week.

closed as unclear what you're asking by Greenonline, VE7JRO, sempaiscuba, per1234, MatsK Nov 6 '18 at 20:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Nov 7 '18 at 5:50
  • This is not a forum. Questions should stand on their own, and comments under questions should seek clarification which is edited into the question. If there is more information which would help answer this question it should be edited into the question by the asker of the question (the OP, namely Xiaomy). – Nick Gammon Nov 7 '18 at 5:52
  • @NickGammon, that is what we did. only it took many clarification questions. clarifications were edited into Question – Juraj Nov 8 '18 at 20:01
  • @Juraj need your assistance – Xiaomy Jan 2 at 13:21
  • related in this topic see the chat thank you sir @Juraj – Xiaomy Jan 2 at 13:22
2

If you want to use esp-01 over SoftwareSerial, you must set the AT firmware's default baud rate to 9600 baud. The default baud rate of AT firmware is 115200 baud. SoftwareSerial can't work at this speed.

To change the baud rate you must have a way to communicate with AT firmware at the default baud rate of 115200 baud. You can use the USB adapter of the Uno. Upload BareMinimal sketch from Examples to Uno to make it silent and connect the esp-01 to RX and TX pins of the Uno.

Connect RX to RX and TX to TX to have the cross connection to USB of the Uno. Then connect from Serial Monitor to esp-01 at 115200 with "Both line end characters" set. Try some basic AT commands and then still at 115200 baud send the command to change the default baud rate AT+UART_DEF=9600,8,1,0,0


In setup() in sketch begin for SoftwareSerial is missing. Add esp8266.begin(9600);


Uno pin 11 is RX of software serial, pin 10 is TX. The RX pin 11 must connect to TX pin of esp-01. Same applies for TX pin 10. It must connect to RX pin of the esp-01.

On the Fritzing schema on Uno pin 11 is green wire and on esp-01 TX is green wire. But the green wire is on sides of the level converter not on the same position. The conclusion is that you have RX connected to RX not to TX. Switch wires on best at level converter.


The 3.3 V pin of Uno can't supply enough current for WiFi operations of the esp8266. You need to convert to 3.3 V from Uno 5 V pin or use a separate 3.3 V power source for the esp-01.

Not the answer you're looking for? Browse other questions tagged or ask your own question.