0% found this document useful (0 votes)
85 views2 pages

MQ 2 Thingspeak

This document contains code for an Arduino sketch that connects an MQ2 gas sensor module to a WiFi network. It reads gas level values from the sensor and sends the data to ThingSpeak using an HTTP POST request. The sketch initializes the WiFi and MQ2 sensor, reads the gas levels in a loop, and sends the data along with an API key to ThingSpeak every 2 seconds.

Uploaded by

Rianda. Pratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

MQ 2 Thingspeak

This document contains code for an Arduino sketch that connects an MQ2 gas sensor module to a WiFi network. It reads gas level values from the sensor and sends the data to ThingSpeak using an HTTP POST request. The sketch initializes the WiFi and MQ2 sensor, reads the gas levels in a loop, and sends the data along with an API key to ThingSpeak every 2 seconds.

Uploaded by

Rianda. Pratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <MQ2.

h>
#include <ESP8266WiFi.h>

int pinAout = A0;


int lpg_gas, co_gas, smoke_gas;

MQ2 mq2(pinAout);
String apiKey = ""; // Masukan Write API key kamu dari ThingSpeak
const char* ssid = ""; // Masukan nama wifi kamu
const char* pass = ""; // Masukan Password wifi kamu
const char* server = "api.thingspeak.com";
WiFiClient client;

void setup() {

Serial.begin(115200);

mq2.begin();
delay(10);

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED)


{
delay(100);
Serial.print("*");
}
Serial.println("");
Serial.println("***WiFi Terhubung***");

void loop() {

if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com


{
String sendData = apiKey+"&field1="+String(lpg_gas)
+"&field2="+String(co_gas)+"&field3="+String(smoke_gas)+"\r\n\r\n";

//Serial.println(sendData);

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(sendData.length());
client.print("\n\n");
client.print(sendData);

float* values= mq2.read(false); //jika diset "false" tidak akan dimunculkan di


serial monitor
lpg_gas = mq2.readLPG();
co_gas = mq2.readCO();
smoke_gas = mq2.readSmoke();

Serial.print("LPG:");
Serial.print(lpg_gas);
Serial.print(" CO:");
Serial.print(co_gas);
Serial.print("SMOKE:");
Serial.println(smoke_gas);

Serial.println("%. Menghubungkan ke Thingspeak.");

client.stop();
Serial.println("Mengirim data....");

//delay(2000); // Delay ini akan mempengaruhi pengiriman data ke Thingspeak


setiap 2000 milidetik atau = 2 detik sekali. Bisa dirubah sesuai dengan kebutuhan.
}

You might also like