Skip to content

Instantly share code, notes, and snippets.

@glisha
Created November 24, 2012 20:07
Show Gist options
  • Save glisha/4141222 to your computer and use it in GitHub Desktop.
Save glisha/4141222 to your computer and use it in GitHub Desktop.
MQ135 gas sensor
int sensorValue;
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
// If we get a ping from the PC, dump the data
if (Serial.available()) {
byte a = Serial.read();
if (a == 'g') { // gas sensor
sensorValue = analogRead(0);
Serial.println(sensorValue, DEC);
}
else if (a == 'l') { // light sensor
sensorValue = analogRead(5);
float Rsensor;
//Rsensor=(float)(1023-sensorValue)*10/sensorValue;
Serial.println(sensorValue);
}
Serial.println();
}
}
cosm = {
"feed_id": "<11111>",
"api_key": "<COSM KEY>",
}
#!/usr/bin/env python2.7
"""
Reads out the gas MQ135 sensor from serial and posts them to https://cosm.com/feeds/xxxx
- http://wiring.org.co/learning/basics/airqualitymq135.html
"""
import serial
import json
import requests
import time
import sys
from cosm_credentials import cosm
#the current time for the datapoint
curr_time = time.strftime("%Y-%m-%dT%H:%M:%SZ%z", time.gmtime())
#set up the serial
ser = serial.Serial("/dev/ttyUSB1", timeout=10)
# ask for gas sensor data
ser.flushInput()
time.sleep(10)
ser.write("g") #gas sensor
#read the data
while True:
line = ser.readline()
if not line or line == '\r\n':
break
sensor_value = line.strip()
url="http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints" % (cosm['feed_id'], "hacklab_airquality")
headers = {'X-ApiKey':cosm['api_key']}
payload = {'datapoints': [{'at': curr_time, 'value': sensor_value}]}
print payload
requests.post(url, data=json.dumps(payload), headers=headers)
# ask for light sensor data
ser.flushInput()
time.sleep(10)
ser.write("l") #gas sensor
#read the data
while True:
line = ser.readline()
if not line or line == '\r\n':
break
sensor_value = line.strip()
url="http://api.cosm.com/v2/feeds/%s/datastreams/%s/datapoints" % (cosm['feed_id'], "hacklab_light_hwroom")
headers = {'X-ApiKey':cosm['api_key']}
payload = {'datapoints': [{'at': curr_time, 'value': sensor_value}]}
print payload
requests.post(url, data=json.dumps(payload), headers=headers)
@Indira-s
Copy link

Hi ...can anyone pls send code for mq135 gas detecting sensor with arduino uno ...pls help us..send to' Indira.sandhireddy441@gmail.com'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment