ESP8266-01 Transceiver as WiFi Station to connect WiFI Hotspot

Dear Members,

I have made a project of Arduino Based Air quality monitoring System using Arduino Uno, MQ135 Sensor, and ESP8266-01 WiFi Transceiver. The system is working quite well with existing codes. 

Now I can see the values on LCD Screen, Buzzer is functional for the Alert and I can even see the readings on the Mobile phone as ESP8266 is working as Access Point. 

I want to make changes in the code so that ESP8266 will act as WiFi Station and it could connect to a nearby WiFi Hotspot. I want to send data on Thngspeak.com IOT Server. 

I'm using the code and idea from: https://circuitdigest.com/microcontroller-projects/iot-air-pollution-monitoring-using-arduino​ 

What changes do I have to make in the below code to accomplish my requirement? I don't want to keep other features like LCD Screen, Buzzer as it is.

Please guide. 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include "MQ135.h"
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(9,10); // This makes pin 9 of Arduino as RX pin and pin 10 of Arduino as the TX pin
const int sensorPin= 0;
int air_quality;
#include <LiquidCrystal.h> 
LiquidCrystal lcd(12,11, 5, 4, 3, 2);

void setup() {
pinMode(8, OUTPUT);
lcd.begin(16,2);
lcd.setCursor (0,0);
lcd.print ("Govt Engg College Valsad ");
lcd.setCursor (0,1);
lcd.print ("Sensor Warming ");
delay(100);
Serial.begin(115200);
esp8266.begin(115200); // your esp's baud rate might be different
  sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
pinMode(sensorPin, INPUT);        //Gas sensor will be an input to the arduino
lcd.clear();
}

void loop() {

MQ135 gasSensor = MQ135(A0);
float air_quality = gasSensor.getPPM();

if(esp8266.available()) // check if the esp 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 Air Pollution Monitoring System, GEC Valsad</h1>";
       webpage += "<p><h2>";   
       webpage+= " Air Quality is ";
       webpage+= air_quality;
       webpage+=" PPM";
       webpage += "<p>";
     if (air_quality<=1000)
{
  webpage+= "Fresh Air";
}
else if(air_quality<=2000 && air_quality>=1000)
{
  webpage+= "Poor Air";
}

else if (air_quality>=2000 )
{
webpage+= "Danger! Move to Fresh Air";
}

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);
     
     cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     
     sendData(closeCommand,3000,DEBUG);
    }
  }

lcd.setCursor (0, 0);
lcd.print ("Air Quality is ");
lcd.print (air_quality);
lcd.print (" PPM ");
lcd.setCursor (0,1);
if (air_quality<=1000)
{
lcd.print("Fresh Air");
digitalWrite(8, LOW);
}
else if( air_quality>=1000 && air_quality<=2000 )
{
lcd.print("Poor Air, Open Windows");
digitalWrite(8, HIGH );
}
else if (air_quality>=2000 )
{
lcd.print("Danger! Move to Fresh Air");
digitalWrite(8, HIGH);   // turn the LED on
}
lcd.scrollDisplayLeft();
delay(400);
}
String sendData(String command, const int timeout, boolean debug)
{
    String response = ""; 
    esp8266.print(command); // send the read character to the esp8266
    long int time = millis();
    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }
    if(debug)
    {
      Serial.print(response);
    }
    return response;
}

 

You need to set the autoconnect library in the arduino ide and configure that one. Search in the google using the keyword "Autoconnect library for ESP8266".

You also need to run a small webpage in the esp.

 

hope this will help you further.

  Joined February 12, 2018      696
Monday at 02:11 PM

Hello Sourav Gupta,

My requirement is different. I want to configure ESP8266 as station so that it would connect to a nearby Hotspot. I also want  to add API / Codes so that ESP can send the reading to Thingspeak IOT Cloud. 

I don't want to compromise the features of seeing reading on 16 x 2 LCD Screen and getting alert if threasold value reaches > 1000 ! Currently I'm running a webpage in ESP and able to browse in Mobile Phone screen.

 

Regards,

Mithilesh Pandey

  Joined October 14, 2020      5
Wednesday at 10:38 AM

Dont get me wrong on this, but I think you should find the sollution to your problem yourself, if you do so it will be a great learning experience for you, and the knowledge you will get from this excersise, will enable you to make more cool projects like this. if you want to do this yourself first you have to brake down your problems and requirements, and search them separately, if you do this you can clearly understand what is what and what does what. think that is a good starting point. while doing so if you stumble upon any problems feel free to ask on the forum wood be very happy to help you.

  Joined December 02, 2019      117
Monday at 10:02 PM

I understand. For connecting to a nearby hotspot you need to connect it automatically. If the hotspot is free of password it will connect using the autoconnect library. It is an additional library irrespective of what type of peripherals you use in the ESP32.

  Joined February 12, 2018      696
Monday at 02:11 PM