26/10/2018

IoT Gas Sensor with ESP8266

By snorlaxprime

I had been wanting to get the gas sensor working with my ESP8266 as an additional item on the IoT list. So similar to the IoT Temperature sensor from the previous post, I had created the IoT gas sensor using ESP8266.

Using the same idea you don’t have to open port in your router, but leveraging an intermediate layer by publishing the data to a website. Here are the list of components used:

The circuit

We are connecting the Gas sensor analog output to Analog input (AD0) of ESP8266 as shown in the following picture.

ESP8266WithGasSensor

The Buzzer is connected to Pin GND and D3.

We will check the sensor via analog to give much better variation, we can also use the Digital output from the sensor, but you will have to calibrate this properly to ensure that it gives the desired trigger when a certain composition of gas was detected. You can use the prototype board to connect them all up and using Arduino interface you can load the code.

If you are not familiar on how to setup the Arduino IDE (integrated development Environment) I had an instructables post that can help you to get started.

The finished connection should look like something below:

IoT Gas Sensor with ESP8266

Now that you have a setup the circuit, there is one more step to setup the Cloud webserver (optional) if you want the sensor to periodically upload the gas reading to the webserver so that you can monitor this remotely.

Assumption: You have a working webserver, and familiar with transferring files to your webserver.

Web server setup

Transfer the content of “IoTGasSensorwebserver.zip” into the root of your webserver. Let’s assume that your webserver is “http://arduinotestbed.com”

You can put the file in a separate folder, but you need to modify the sketch to point to the correct location of “gasdata_store.php”

In this example we are assuming that the full path to the file are http://arduinotestbed.com/gasdata_store.php

Once you had uploaded the files, you can test that it is working going to the following link

http://arduinotestbed.com/GasData.php

watch out for the character case as most webserver are case sensitive. If all is working, you should see something that looks like the following:

IoT Gas Data

One more thing you will need to make sure is the file “gas.txt” needs to be writeable, so you need to set the permission of this file to “666” by using the following unix command:

chmod 666 gas.txt

This file is where the sensor data will be uploaded into by the ESP8266.

You can download the webserver scripting using this link.

Uploading the arduino sketch

Now you are ready to upload the sketch code to the ESP8266. You should have 2 files:

  • ESP8266GasSensor.ino
  • mainPage.h
  • settings.h

Put them all in the same folder and open the “ESP8266GasSensor.ino” in the Arduino IDE, then make small modification to the code to point to the correct webserver location shown in the following picture:

Arduino code changes

Modify the line

String weburi = "/gasdata_store.php"

if you didn’t put this file in the root of the webserver, this should point to the correct path of the file. Then compiled and load the sketch to ESP8266. If all goes well you should have a working AP (Access point) from the ESP8266 the first time you run this. The AP’s name is called “ESP-GasSensor”.

Try to connect to this AP using your laptop or mobile phone, then find out what is the ip address that was assigned to you, this can be done using “ipconfig” command in windows or “ifconfig” command if you are in linux or mac. If you are using iPhone you can click on the “i” button next to the ESP-GasSensor that you are connected to.

Open your web browser and point to the ESP-GasSensor Ip address, if you are assigned with 192.168.4.10 as your up, the ESP-GasSensor is have the ip of 192.168.4.1, so you can point your web browser to http://192.168.4.1

You should be presented with the settings page where you can enter your wifi configuration. Once you had enter your WiFi access point that connects to the internet, tick the “update Wifi Config” check box, and click on “update” to save the settings to the ESP8266.

The ESP8266 will now restart and trying to connect to your WiFi router. If all goes well you should see the gas data being updated to you webserver on a regular interval. In this example you can point your browser to

http://arduinotestbed.com/GasData.php

Congratulations!! if you manage to reach this part. You should give yourself a pat on the back. Now you can tell your friends about the gas sensor that you have.

You can download the full arduino source code here.

Final adjustment

But wait you might want to re-calibrate the sensor alarm to suit your need. This is not just for show, it should trigger and alarm when the threshold of gas reach a certain level. Depends on the type of sensor that you are using you will need to calibrate this. So go get a lighter, and point the lighter towards the sensor, and without lighting the lighter, press the gas release button on the lighter, so the gas will flow to the sensor. This should trip the buzzer. If not then you need to check whether the reading goes up by looking at the webserver. If this is not working then you need to check the connection, the sensor and the buzzer. If all goes well, the buzzer should make a noise. The threshold in the code is set to 100, you should be able to find it in the following section of the code:

double threshold = 100;

Feel free to change the threshold to higher or lower depends on your need. I hope you like this project. If you do please drop me a line, and subscribe to my blog for more projects.