Giter VIP home page Giter VIP logo

air-quality-monitoring-esp8266-with-display's Introduction

IoT-Air-Quality-monitoring-with-LCD

Using ESP8266, MQ135 and SH1106

I wanted to create the AQ monitoring system shown at the website below, but I found out after some tinkering around and getting frustrated, that the display I had was in fact not the same as in the website, albeit looks the same.

The original website: https://how2electronics.com/iot-air-quality-index-monitoring-esp8266/

Parts used:

  1. Breadboard
  2. 7 connecting wires
  3. MQ-135 gas sensor
  4. ESP8266
  5. SH1106 128x64 OLED display, not to be confused with the similarly looking SSD1306 by Adafruit!

Because the SH1106 and SSD1306 are not the same, they require different libraries to work. The library to use for the SH1106 is U8g2

image

How did I figure out I was not using the correct library? Thanks to this comment here on Reddit.

Additional things to know

This code allows you to upload everything to Thingspeak so you can monitor it remotely too!

Setting up Thingspeak

Just follow the instructions in the screenshot below image

The code

U8g2lib is the library we'll use for the SH1106 monitor

#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <U8g2lib.h>
#include "MQ135.h"

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

Setting up the wifi connection

String apiKey = "14K8UL2QEK8BTHN6"; // Enter your Write API key from ThingSpeak
const char *ssid = "Potato";     // replace with your wifi ssid and wpa2 key
const char *pass = "123456789";  // replace with your wifi password
const char* server = "api.thingspeak.com";
 
WiFiClient client;
 

/* setup(void) {
  u8g2.begin();
}*/

void setup()
{
  u8g2.begin();
  Serial.begin(115200);
  u8g2.clearBuffer();          // clear the internal memory
  delay(10);
 
  u8g2.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font
  u8g2.setCursor(1,10);       // where it will start printing from on the display
  u8g2.print("Connecting to ");
  u8g2.setCursor(1,22);       
  u8g2.print(ssid);
  u8g2.sendBuffer();          // transfer internal memory to the display
  delay(1000); 
  
  WiFi.begin(ssid, pass);
 
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
    Serial.println("");
    Serial.println("WiFi connected");

    u8g2.clearBuffer();          // clear the internal memory
    delay(10);
    u8g2.drawStr(1,10,"WiFi connected");
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(4000); 

}
 

Displaying the air quality values on the display

  void loop()
  {
    MQ135 gasSensor = MQ135(A0);
    float air_quality = gasSensor.getPPM();
    Serial.print("Air Quality: ");  
    Serial.print(air_quality);
    Serial.println("  PPM");   
    Serial.println();

    u8g2.clearBuffer();          // clear the internal memory
    delay(10);
    u8g2.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font

    u8g2.setCursor(1,10);
    u8g2.print("Air Quality Index");
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(1000);

    /* Air quality number */
    u8g2.setFont(u8g2_font_ncenB08_tr);  // choose a suitable font
    u8g2.setCursor(1,20);
    u8g2.print(air_quality);
    u8g2.setCursor(40,20);
    u8g2.print("PPM");          // PPM label, need to add on same line as number though
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(1000);

 
 
    if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
  {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(air_quality);
    postStr += "r\n";
    
    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 + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
    
    Serial.println("Data Send to Thingspeak");
  }
    client.stop();
    Serial.println("Waiting...");
 
    delay(2000);      // thingspeak needs minimum 15 sec delay between updates.
}

air-quality-monitoring-esp8266-with-display's People

Contributors

issuecoding avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.