Commit 7371ca4c by Jeffrey Redondo

CO2 Arduino and GSM

parents
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
int TVOC, CO2;
char buff[256];
// LinkIt One sketch for MQTT Demo
#include <LGPRS.h>
#include <LGPRSClient.h>
#include <LGPRSServer.h>
#include <PubSubClient.h>
LGPRSClient c;
/*
Modify to your MQTT broker - Select only one
*/
//char mqttBroker[] = "iot.eclipse.org";
//char mqttBroker[] = "test.mosquitto.org";
byte mqttBroker[] = {37,187,106,16}; // modify to your local broker
PubSubClient client( c );
unsigned long lastSend;
void setupGPRS(){
char server[] = "arduino.cc";
char path[] = "/asciilogo.txt";
int port = 80; // HTTP
Serial.println("Attach to GPRS network by auto-detect APN setting");
while (!LGPRS.attachGPRS("internet","",""))
{
delay(500);
}
Serial.println("Connected to GPRS");
delay(10000);
// if you get a connection, report back via serial:
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Connecting to MQTT broker ...");
// Attempt to connect
if ( client.connect("CO2 LinkIt One Client") ) { // Better use some random name
Serial.println( "[DONE]" );
// Publish a message on topic "outTopic"
client.publish( "outTopic","Hello, This is LinkIt One" );
// Subscribe to topic "inTopic"
client.subscribe( "inTopic" );
} else {
Serial.print( "[FAILED] [ rc = " );
Serial.print( client.state() );
Serial.println( " : retrying in 5 seconds]" );
// Wait 5 seconds before retrying
delay( 5000 );
}
}
}
void setup() {
delay( 10000 );
Serial.begin( 115200 );
setupGPRS();
client.setServer( mqttBroker, 1883 );
//client.setCallback( callback );
lastSend = 0;
Wire.begin(); // join i2c bus (address optional for master)
delay( 10000 );
//client.setServer( mqttBroker, 1883 );
//client.setCallback( callback );
initCCS811();
}
void loop() {
if( !client.connected() ) {
reconnect();
}
readCCS811();
// equivalent CO2 400ppm to 8192ppm
// Total Volatile Organic Compound (TVOC) 0ppb to 1187ppb
sprintf(buff,"CO2 = %d ppm; TVOC = %d ppb", CO2, TVOC);
Serial.println(buff);
delay(500);
}
void initCCS811() {
Wire.beginTransmission(0x5A); // transmit to device
Wire.write(0xF4); // APP_START
Wire.endTransmission(); // stop transmitting
Wire.beginTransmission(0x5A); // transmit to device
Wire.write(0x00); // STATUS
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(0x5A, (uint8_t)1);
byte status;
if(Wire.available() == 1)
status = Wire.read();
//check if firmware is in application mode and application firmware loaded.
if((status >> 4 & 0x01) && (status >> 7 & 0x01)) {
Wire.beginTransmission(0x5A); // transmit to device
Wire.write(0x01); // MEAS_MODE
Wire.write(0x10); // constant power mode
Wire.endTransmission(); // stop transmitting
}
}
void readCCS811() {
Wire.beginTransmission(0x5A); // transmit to device
Wire.write(0x02); // ALG_RESULT_DATA
Wire.endTransmission(); // stop transmitting
Wire.requestFrom(0x5A, (uint8_t)4);
delay(1);
int buffer[4];
if(Wire.available() == 4)
{
for(int i=0; i<4; i++)
{
buffer[i] = Wire.read();
//Serial.print(buffer[i]);
}
}
CO2 = ((uint8_t)buffer[0] << 8) + buffer[1];
TVOC = ((uint8_t)buffer[2] << 8) + buffer[3];
char test[100];
String x ="111";
x= String(CO2);
x.toCharArray( test, 100 );
client.publish( "/Thingworx/Team3Template/CO2_G3", test );
delay(10000);
delay(10000);
//delay(10000);
//delay(10000);
//delay(10000);
//delay(10000);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment