Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add examples #16

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.


Expand All @@ -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()**

Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions SGP30.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,15 +17,16 @@
//
SGP30::SGP30(TwoWire *wire)
{
_address = 0x58;
_wire = wire;
_address = 0x58; // Fixed!

_tvoc = 0;
_co2 = 0;
_h2 = 0;
_ethanol = 0;

_lastTime = 0;
_lastRequest = 0;
_error = SGP30_OK;
}

Expand Down
9 changes: 5 additions & 4 deletions SGP30.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -95,13 +95,14 @@ class SGP30
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();

Expand Down
30 changes: 30 additions & 0 deletions examples/SGP30_demo_lora/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -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 changes: 107 additions & 0 deletions examples/SGP30_demo_lora/SGP30_demo_lora.ino
Original file line number Diff line number Diff line change
@@ -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 changes: 30 additions & 0 deletions examples/TCA9548_demo_SGP30/.arduino-ci.yml
Original file line number Diff line number Diff line change
@@ -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"
82 changes: 82 additions & 0 deletions examples/TCA9548_demo_SGP30/TCA9548_demo_SGP30.ino
Original file line number Diff line number Diff line change
@@ -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 --
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*",
Expand Down
Loading
Loading