Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
jeredo
/
CO2
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7371ca4c
authored
7 years ago
by
Jeffrey Redondo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
CO2 Arduino and GSM
parents
master
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
154 additions
and
0 deletions
i2c_GSM_Thingworx.ino/i2c_GSM_Thingworx.ino/i2c_GSM_Thingworx.ino.ino
i2c_GSM_Thingworx.ino/i2c_GSM_Thingworx.ino/i2c_GSM_Thingworx.ino.ino
0 → 100644
View file @
7371ca4c
// 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);
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment