mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
0.3.1 SGP30
This commit is contained in:
parent
173dfb17c0
commit
3c330e8f73
@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.3.1] - 2024-01-01
|
||||
- add LORA example
|
||||
- add Multiplexer example
|
||||
- update readme.md
|
||||
- minor edits.
|
||||
|
||||
## [0.3.0] - 2023-12-08
|
||||
- refactor API, begin()
|
||||
- update readme.md
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2023 Rob Tillaart
|
||||
Copyright (c) 2021-2024 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -78,6 +78,8 @@ or switch the VCC as a sort of ChipSelect signal.
|
||||
|
||||
- https://github.com/RobTillaart/TCA9548 (I2C 8 channel multiplexer)
|
||||
|
||||
See **TCA9548_demo_SGP30.ino** example.
|
||||
|
||||
|
||||
#### Related
|
||||
|
||||
@ -127,10 +129,10 @@ Note the measurement is slow as there is an active blocking until the sensor is
|
||||
If the last measurement is less than a second ago, no measurement is made and the function returns false.
|
||||
|
||||
|
||||
#### A-synchronous measurements
|
||||
#### Asynchronous measurements
|
||||
|
||||
With the async interface, the user should control that reads are at least one second apart.
|
||||
The user should also take care not to mix up different requests. See examples.
|
||||
The user should also take care not to mix up different requests. See examples.
|
||||
|
||||
- **void request()** sends a request to the sensor to read CO2 and TVOC.
|
||||
- **bool read()** returns true if the last request is more than 12 milliseconds ago the
|
||||
@ -208,9 +210,9 @@ The used references are based upon
|
||||
(2) the assumption that this is 0.4 resp 0.5 ppm.
|
||||
(Note only 1 significant digit) as mentioned in datasheet P2.
|
||||
|
||||
- **void setSrefH2(uint16_t s = 13119)** // 13119 is my measurement.
|
||||
- **void setSrefH2(uint16_t s = 13119)** 13119 is my measurement.
|
||||
- **uint16_t getSrefH2()** returns value set.
|
||||
- **void setSrefEthanol(uint16_t s = 18472)** // 18472 is my measurement.
|
||||
- **void setSrefEthanol(uint16_t s = 18472)** 18472 is my measurement.
|
||||
- **uint16_t getSrefEthanol()** returns value set.
|
||||
|
||||
|
||||
@ -229,7 +231,6 @@ The used references are based upon
|
||||
#### Could
|
||||
|
||||
- move code from .h to .cpp
|
||||
- improve/merge the private **command()** function
|
||||
- add/extend error handling
|
||||
- better name for **measureTest()**
|
||||
|
||||
@ -241,6 +242,7 @@ for the smallest platforms e.g. tiny.
|
||||
|
||||
- make defines for the magic numbers (commands)
|
||||
- only used once.
|
||||
- improve/merge the private **command()** function (made no difference)
|
||||
|
||||
|
||||
## Support
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: SGP30.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.0
|
||||
// VERSION: 0.3.1
|
||||
// DATE: 2021-06-24
|
||||
// PURPOSE: Arduino library for SGP30 environment sensor.
|
||||
// URL: https://github.com/RobTillaart/SGP30
|
||||
@ -17,8 +17,8 @@
|
||||
//
|
||||
SGP30::SGP30(TwoWire *wire)
|
||||
{
|
||||
_address = 0x58;
|
||||
_wire = wire;
|
||||
_address = 0x58; // Fixed!
|
||||
|
||||
_tvoc = 0;
|
||||
_co2 = 0;
|
||||
@ -26,6 +26,7 @@ SGP30::SGP30(TwoWire *wire)
|
||||
_ethanol = 0;
|
||||
|
||||
_lastTime = 0;
|
||||
_lastRequest = 0;
|
||||
_error = SGP30_OK;
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: SGP30.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.0
|
||||
// VERSION: 0.3.1
|
||||
// DATE: 2021-06-24
|
||||
// PURPOSE: Arduino library for SGP30 environment sensor.
|
||||
// URL: https://github.com/RobTillaart/SGP30
|
||||
@ -12,7 +12,7 @@
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
#define SGP30_LIB_VERSION (F("0.3.0"))
|
||||
#define SGP30_LIB_VERSION (F("0.3.1"))
|
||||
|
||||
#define SGP30_OK 0x00
|
||||
#define SGP30_ERROR_CRC 0xFF
|
||||
@ -95,13 +95,14 @@ public:
|
||||
private:
|
||||
uint8_t _address;
|
||||
int _error;
|
||||
uint32_t _lastTime = 0;
|
||||
uint32_t _lastRequest = 0;
|
||||
uint32_t _lastTime;
|
||||
uint32_t _lastRequest;
|
||||
|
||||
// TODO improve?
|
||||
int _command(uint16_t cmd);
|
||||
int _command(uint16_t cmd, uint16_t v1);
|
||||
int _command(uint16_t cmd, uint16_t v1, uint16_t v2);
|
||||
|
||||
uint8_t _CRC8(uint16_t val);
|
||||
void _init();
|
||||
|
||||
|
30
libraries/SGP30/examples/SGP30_demo_lora/.arduino-ci.yml
Normal file
30
libraries/SGP30/examples/SGP30_demo_lora/.arduino-ci.yml
Normal file
@ -0,0 +1,30 @@
|
||||
platforms:
|
||||
rpipico:
|
||||
board: rp2040:rp2040:rpipico
|
||||
package: rp2040:rp2040
|
||||
gcc:
|
||||
features:
|
||||
defines:
|
||||
- ARDUINO_ARCH_RP2040
|
||||
warnings:
|
||||
flags:
|
||||
|
||||
packages:
|
||||
rp2040:rp2040:
|
||||
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
# - uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
# - m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
||||
# - rpipico
|
||||
libraries:
|
||||
- ArduinoJson
|
||||
- LoRa
|
107
libraries/SGP30/examples/SGP30_demo_lora/SGP30_demo_lora.ino
Normal file
107
libraries/SGP30/examples/SGP30_demo_lora/SGP30_demo_lora.ino
Normal file
@ -0,0 +1,107 @@
|
||||
|
||||
//
|
||||
// FILE: SGP30_demo_lora.ino
|
||||
// AUTHOR: Rob Tillaart, Anthony Powell
|
||||
// PURPOSE: demo SGP30 to LORA
|
||||
// URL: https://github.com/RobTillaart/SGP30
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
#include <LoRa.h>
|
||||
#include <SGP30.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Wire.h>
|
||||
|
||||
|
||||
SGP30 sgp;
|
||||
|
||||
// Create a unique ID for the data from each ESP32 running this code
|
||||
|
||||
const char* jediID = "Envirodrone";
|
||||
|
||||
uint32_t lastTime = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println(F("LoRa Sender"));
|
||||
|
||||
if (!LoRa.begin(868E6))
|
||||
{
|
||||
Serial.println("Error: Starting LoRa failed! reboot system");
|
||||
while (1);
|
||||
}
|
||||
Serial.println(F("Enviro Drone SGP30 Sensor Start"));
|
||||
delay(1000);
|
||||
|
||||
// Initialize I2C bus
|
||||
|
||||
Wire.begin();
|
||||
Wire.setClock(400000);
|
||||
|
||||
// Initialize sensor
|
||||
while (! sgp.begin())
|
||||
{
|
||||
Serial.println(F("Error: Could not detect SGP30 sensor"));
|
||||
delay(3000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
String postData;
|
||||
|
||||
// send data once every 10 seconds
|
||||
if (millis() - lastTime >= 10000)
|
||||
{
|
||||
uint32_t start = millis();
|
||||
Serial.print("time: \t");
|
||||
Serial.println(start);
|
||||
|
||||
|
||||
Serial.print(millis() - start);
|
||||
Serial.println("Read SGP30 sensor.");
|
||||
sgp.measure(true);
|
||||
float co2 = sgp.getCO2();
|
||||
float tvoc = sgp.getTVOC();
|
||||
float h2 = sgp.getH2();
|
||||
float etha = sgp.getEthanol();
|
||||
Serial.print(co2);
|
||||
Serial.print("\t");
|
||||
Serial.print(tvoc);
|
||||
Serial.print("\t");
|
||||
Serial.print(h2);
|
||||
Serial.print("\t");
|
||||
Serial.print(etha);
|
||||
Serial.println();
|
||||
|
||||
|
||||
Serial.print(millis() - start);
|
||||
Serial.println("Create serialized JSON string.");
|
||||
StaticJsonDocument <200> doc;
|
||||
JsonObject data = doc.createNestedObject("data");
|
||||
data["CO2"] = co2;
|
||||
data["TVOC"] = tvoc;
|
||||
data["H2"] = h2;
|
||||
data["Ethanol"] = etha;
|
||||
serializeJson(doc, postData);
|
||||
Serial.println(postData);
|
||||
|
||||
|
||||
Serial.print(millis() - start);
|
||||
Serial.println("Send LORA packet.");
|
||||
LoRa.beginPacket();
|
||||
LoRa.print(postData);
|
||||
LoRa.endPacket();
|
||||
|
||||
|
||||
uint32_t stop = millis();
|
||||
Serial.print(millis() - start);
|
||||
Serial.print("Duration:\t");
|
||||
Serial.println(stop - start);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// -- END OF FILE --
|
30
libraries/SGP30/examples/TCA9548_demo_SGP30/.arduino-ci.yml
Normal file
30
libraries/SGP30/examples/TCA9548_demo_SGP30/.arduino-ci.yml
Normal file
@ -0,0 +1,30 @@
|
||||
platforms:
|
||||
rpipico:
|
||||
board: rp2040:rp2040:rpipico
|
||||
package: rp2040:rp2040
|
||||
gcc:
|
||||
features:
|
||||
defines:
|
||||
- ARDUINO_ARCH_RP2040
|
||||
warnings:
|
||||
flags:
|
||||
|
||||
packages:
|
||||
rp2040:rp2040:
|
||||
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
|
||||
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
- esp8266
|
||||
# - mega2560
|
||||
- rpipico
|
||||
libraries:
|
||||
- "AM232X"
|
||||
- "TCA9548"
|
@ -0,0 +1,82 @@
|
||||
//
|
||||
// FILE: TCA9548_demo_SGP30.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo TCA9548 I2C multiplexer
|
||||
// URL: https://github.com/RobTillaart/TCA9548
|
||||
// URL: https://github.com/RobTillaart/SGP30
|
||||
|
||||
|
||||
#include "SGP30.h"
|
||||
#include "TCA9548.h"
|
||||
|
||||
PCA9546 MP(0x70);
|
||||
uint8_t channels = 0;
|
||||
|
||||
SGP30 living; // channel 0
|
||||
SGP30 kitchen; // channel 1
|
||||
SGP30 outside; // channel 2
|
||||
|
||||
uint32_t lastTime = 0;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("TCA9548_LIB_VERSION: ");
|
||||
Serial.println(TCA9548_LIB_VERSION);
|
||||
Serial.print("SGP30_LIB_VERSION: ");
|
||||
Serial.println(SGP30_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
Wire.begin();
|
||||
// initialize multiplexer
|
||||
if (MP.begin() == false)
|
||||
{
|
||||
Serial.println("Multiplexer error");
|
||||
}
|
||||
channels = MP.channelCount();
|
||||
|
||||
// initialize the temperature sensors
|
||||
MP.selectChannel(0);
|
||||
if (living.begin() == false)
|
||||
{
|
||||
Serial.println("living error");
|
||||
}
|
||||
MP.selectChannel(1);
|
||||
if (kitchen.begin() == false )
|
||||
{
|
||||
Serial.println("kitchen error");
|
||||
}
|
||||
MP.selectChannel(2);
|
||||
if (outside.begin() == false )
|
||||
{
|
||||
Serial.println("outside error");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
if ((millis() - lastTime) > 5000)
|
||||
{
|
||||
lastTime = millis();
|
||||
|
||||
MP.selectChannel(0);
|
||||
living.read();
|
||||
Serial.print(living.getCO2(), 1);
|
||||
Serial.print("\t");
|
||||
|
||||
MP.selectChannel(1);
|
||||
kitchen.read();
|
||||
Serial.print(kitchen.getCO2(), 1);
|
||||
Serial.print("\t");
|
||||
|
||||
MP.selectChannel(2);
|
||||
outside.read();
|
||||
Serial.print(outside.getCO2(), 1);
|
||||
Serial.print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/SGP30.git"
|
||||
},
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=SGP30
|
||||
version=0.3.0
|
||||
version=0.3.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for SGP30 environment sensor.
|
||||
|
Loading…
Reference in New Issue
Block a user