diff --git a/.gitignore b/.gitignore index 0a1ff2e..259148f 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,3 @@ *.exe *.out *.app -*.bin diff --git a/README.md b/README.md index e849e73..fbf14bd 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,2 @@ -# Ambi Client (C++) - -An Edge IoT ambient room environment sensor implementation for a [Particle Argon](https://docs.particle.io/argon/) based on the [Particle IoT Air Quality Monitoring Kit](https://store.particle.io/products/air-quality-monitoring-kit-wi-fi). - -This client pairs with and pushes readings to a [Ambi web backend instance](https://github.com/Jim-Hodapp-Coaching/ambi). - -## Compiling this firmware using the Particle CLI - -When you're ready to compile this firmware, make sure you have the [Particle CLI installed](https://docs.particle.io/tutorials/developer-tools/cli/) and also make sure you have the Particle Argon device target selected and run `particle compile ` in the CLI. Note: if you're on an Arm-based Mac, the default Particle CLI install doesn't seem to work right. Work around this by doing a more manual install by running `sudo npm install -g particle-cli`. - -Also make sure that you have an account registered with the Particle cloud and you've logged in using `particle setup`. - -To build: -`particle compile argon --saveTo ambi_client_cpp.bin` - -If building succeeds, you'll see output similar to: - -``` -Compiling code for argon - -Including: - src/air_purity_sensor.h - src/base_sensor.h - src/dust_sensor.h - src/http_client.h - src/humidity_sensor.h - src/pressure_sensor.h - src/temperature_sensor.h - src/sensorchi.ino - src/air_purity_sensor.cpp - src/base_sensor.cpp - src/dust_sensor.cpp - src/http_client.cpp - src/humidity_sensor.cpp - src/pressure_sensor.cpp - src/sensorchi.cpp - src/temperature_sensor.cpp - project.properties - -attempting to compile firmware -downloading binary from: /v1/binaries/621006549bdb2a2cea6cc276 -saving to: ambi_client_cpp.bin -Memory use: - text data bss dec hex filename - 34080 128 2324 36532 8eb4 /workspace/target/workspace.elf - -Compile succeeded. -Saved firmware to: /Users/jhodapp/Projects/cpp/ambi_client_cpp/ambi_client_cpp.bin -``` - -The following files in project folder will be sent to the cloud-hosted compile service: - -- Everything in the `/src` folder, including the `.ino` application file -- The `project.properties` project definition file -- All libraries stored under `lib//src` - -## Flashing firmware onto Argon target device - -To flash your intended Particle Argon target device with the built firmware, first identify the name of the device registered to the Particle cloud by listing all devices: - -`particle list` - -You should see a list similar to: - -``` -ambi1 [e00fce68834a01fd162abace] (Argon) is online -ambi2 [e00fce68d002c229a830d87f] (Argon) is offline - Functions: - int digitalread (String args) - int digitalwrite (String args) - int analogread (String args) - int analogwrite (String args) -``` - -Note: in this example each target already has a specific name (e.g. ambi1) because it was changed via the Particle mobile app. By default you'll have a more generic device target name if using a new Argon from the factory. - -Now flash the firmware to your particle device target like so: - -`particle flash ambi1 ambi_client_cpp.bin` - -Note: again, make sure you replace `ambi1` with your particular device name. - -*If for some reason over-the-air (OTA) flashing fails, you can flash over USB like so:* - -`particle flash --usb ambi_client_cpp.bin` - -# Changing the firmware to point to your Ambi instance - -An important step is to make sure that the firmware points to the local IP address of your Ambi web backend. To do that, open the source file `src/sensorchi.ino` and change one or both of the byte arrays `dev_server` or `prod_server`. Note: at this time, full domain or hostnames are not supported and is future work to improve this firmware once Ambi is fully cloud-hosted. - -Also make sure that if you're using a the `dev_server` IP address that `#define DEV` is left defined. If you want to use `prod_server`, then just comment out the line `#define DEV`. - -Now rebuild and reflash your firmware with it pointing to your local Ambi instance. One tip is to either dedicate a small computer like a Raspberry Pi to host the Ambi backend, or host it in a VM or bare on your development computer. Set your WiFi router to always issue your Ambi host the same IP address based on its MAC address. This way you won't have to keep changing the dev/prod IP address. +# sensorchi +An IoT sensor platform based on the Particle IoT Air Quality Monitoring Kit diff --git a/project.properties b/project.properties deleted file mode 100644 index 224272e..0000000 --- a/project.properties +++ /dev/null @@ -1,5 +0,0 @@ -name=sensorchi -dependencies.Adafruit_BME280=1.1.5 -dependencies.Grove_Air_quality_Sensor=1.0.1 -dependencies.OLED_Display_128X64=1.0.0 -dependencies.JsonParserGeneratorRK=0.1.3 diff --git a/src/air_purity_sensor.cpp b/src/air_purity_sensor.cpp deleted file mode 100644 index 591037c..0000000 --- a/src/air_purity_sensor.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "air_purity_sensor.h" - -#include "Particle.h" - -AirPuritySensor::AirPuritySensor() - : sensor(nullptr) -{ - setup(); -} - -AirPuritySensor::~AirPuritySensor() -{ - if (sensor != nullptr) - delete sensor; -} - -bool AirPuritySensor::setup() -{ - static bool doOnce = true; - - if (doOnce) { - doOnce = false; - - sensor = new AirQualitySensor(AQS_PIN); - // Initialize the air quality sensor - return sensor->init(); - } - - return false; -} - -String AirPuritySensor::read_str(bool mock_data) -{ - if (sensor == nullptr) - return "Error reading air quality level"; - - static int lastValue = 0; - if (mock_data) { - lastValue++; - if (lastValue > 3) - lastValue = 0; - } else { - // Read from the Grove Air Quality sensor - lastValue = sensor->slope(); - } - - if (lastValue == AirQualitySensor::FORCE_SIGNAL) - return "Dangerous Pollution"; - else if (lastValue == AirQualitySensor::HIGH_POLLUTION) - return "High Pollution"; - else if (lastValue == AirQualitySensor::LOW_POLLUTION) - return "Low Pollution"; - else if (lastValue == AirQualitySensor::FRESH_AIR) - return "Fresh Air"; - else - return "Unknown air quality level"; -} \ No newline at end of file diff --git a/src/air_purity_sensor.h b/src/air_purity_sensor.h deleted file mode 100644 index 568a11e..0000000 --- a/src/air_purity_sensor.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef AIR_PURITY_SENSOR_H -#define AIR_PURITY_SENSOR_H - -#include "base_sensor.h" - -#include "Air_Quality_Sensor.h" - -#define AQS_PIN A2 - -class AirPuritySensor : public BaseSensor -{ - public: - AirPuritySensor(); - ~AirPuritySensor(); - - virtual bool setup(); - virtual String read_str(bool mock_data = false); - - private: - AirQualitySensor *sensor; -}; - -#endif \ No newline at end of file diff --git a/src/base_sensor.cpp b/src/base_sensor.cpp deleted file mode 100644 index 467546f..0000000 --- a/src/base_sensor.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "base_sensor.h" - -BaseSensor::BaseSensor() - : doOnce(true) -{ -} - -BaseSensor::~BaseSensor() -{ -} - -bool BaseSensor::setup() -{ - if (doOnce) { - doOnce = false; - // Initialize the BME280 sensor - return bme_sensor.begin(); - } - - return true; -} - -float BaseSensor::read(bool mock_data) -{ - return 0.0; -} - -String BaseSensor::read_str(bool mock_data) -{ - return "BaseSensor read_str()"; -} - -Adafruit_BME280 & BaseSensor::sensor() -{ - return bme_sensor; -} \ No newline at end of file diff --git a/src/base_sensor.h b/src/base_sensor.h deleted file mode 100644 index ac932a3..0000000 --- a/src/base_sensor.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef BASE_SENSOR_H -#define BASE_SENSOR_H - -#undef max -#include - -#include "Adafruit_BME280.h" - -#include "Particle.h" - -class BaseSensor -{ - public: - BaseSensor(); - virtual ~BaseSensor(); - - virtual bool setup(); - // TODO: make read() be container-based so it can return any type - // and get rid of read_str() - virtual float read(bool mock_data = false); - virtual String read_str(bool mock_data = false); - - protected: - virtual Adafruit_BME280 & sensor(); - - private: - bool doOnce; - Adafruit_BME280 bme_sensor; -}; - -#endif \ No newline at end of file diff --git a/src/dust_sensor.cpp b/src/dust_sensor.cpp deleted file mode 100644 index 43032ff..0000000 --- a/src/dust_sensor.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include "dust_sensor.h" - -#include "Particle.h" - -#include - -#define DUST_SENSOR_PIN D4 -#define SENSOR_READING_INTERVAL 30000 - -DustSensor::DustSensor() - : lastInterval(0), - lowPulseOccupancy(0), - lastLpo(0), - duration(0), - ratio(0), - concentration(0) -{ - setup(); -} - -DustSensor::~DustSensor() -{ -} - -bool DustSensor::setup() -{ - static bool doOnce = true; - - if (doOnce) { - doOnce = false; - - // Connect to the Shinyei PPD42 dust sensor - Serial.begin(9600); - - pinMode(DUST_SENSOR_PIN, INPUT); - lastInterval = millis(); - - return true; - } - - return false; -} - -float DustSensor::read(bool mock_data) -{ - if (mock_data) { - // Adjust this by a little bit each check so we can see it change - if (rand() > (RAND_MAX / 2)) { - concentration += 1.1; - } - else { - concentration -= 1.1; - } - } else { - duration = pulseIn(DUST_SENSOR_PIN, LOW); - lowPulseOccupancy = lowPulseOccupancy + duration; - - getSensorValues(); - - lowPulseOccupancy = 0; - } - - return concentration; -} - -void DustSensor::getSensorValues() -{ - // This particular dust sensor returns 0s often, so let's filter them out by making sure we only - // capture and use non-zero LPO values for our calculations once we get a good reading. - if (lowPulseOccupancy == 0) { - lowPulseOccupancy = lastLpo; - } else { - lastLpo = lowPulseOccupancy; - } - - ratio = lowPulseOccupancy / (SENSOR_READING_INTERVAL * 10.0); // Integer percentage 0=>100 - concentration = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve -} \ No newline at end of file diff --git a/src/dust_sensor.h b/src/dust_sensor.h deleted file mode 100644 index d666157..0000000 --- a/src/dust_sensor.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef DUST_SENSOR_H -#define DUST_SENSOR_H - -#include "base_sensor.h" - -class DustSensor : public BaseSensor -{ - public: - DustSensor(); - ~DustSensor(); - - virtual bool setup(); - virtual float read(bool mock_data = false); - - private: - unsigned long lastInterval; - unsigned long lowPulseOccupancy; - unsigned long lastLpo; - unsigned long duration; - - float ratio = 0; - float concentration = 0; - - void getSensorValues(); -}; - -#endif \ No newline at end of file diff --git a/src/http_client.cpp b/src/http_client.cpp deleted file mode 100644 index 64dfc33..0000000 --- a/src/http_client.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include "http_client.h" - - -HttpClient::HttpClient(IPAddress ip, uint16_t port) - : ip(ip), port(port), use_host(false), - buf_idx(0), received_full_response(false) -{ -} - -HttpClient::HttpClient(const String &host, uint16_t port) - : host(host), port(port), use_host(true), - buf_idx(0), received_full_response(false) -{ -} - -HttpClient::HttpClient() - : port(80), use_host(false), - buf_idx(0), received_full_response(false) -{ -} - -HttpClient::~HttpClient() -{ - client.stop(); - -#if 0 - if (response_buf != nullptr) - free(response_buf); -#endif -} - -bool HttpClient::connect() -{ - Log.info("HttpClient::connect()"); - // If already connected, don't try to connect again - if (client.connected()) { - Log.warn("Connect called after client already connected to endpoint."); - return true; - } - - if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] == 0 && host.length() == 0) { - Log.error("Can't connect, ip nor host are set."); - return false; - } - - if (use_host) { - Log.info("Connecting to: %s:%d", host.c_str(), port); - return client.connect(host, port); - } else if (ip[0] > 0) { - Log.info("Connecting to: %d.%d.%d.%d:%d", ip[0], ip[1], ip[2], ip[3], port); - return client.connect(ip, port); - } else { - Log.warn("Not connecting, host or ip not specified."); - return false; - } - -#if 0 - // Allocate a buffer to hold HTTP responses from the server - if (response_buf == nullptr) - response_buf = static_cast(malloc(RESPONSE_BUFFER_SIZE)); - - if (!response_buf) - Log.error("** Failed to allocate HTTP response_buf **"); - else - Log.info("** Successfully allocated %d bytes of the heap **", RESPONSE_BUFFER_SIZE); -#endif -} - -bool HttpClient::connected() -{ - return client.connected(); -} - -bool HttpClient::available() -{ - return client.available(); -} - -const char HttpClient::read() -{ - const char c = client.read(); - if (buf_idx < RESPONSE_BUFFER_SIZE) { - response_buf[buf_idx] = c; - Log.info("response_buf[%d]: %c", buf_idx, response_buf[buf_idx]); - - // TODO: process the HTTP response - if (httpResponseEnd()) { - Log.info("Received HTTP response end"); - received_full_response = true; - } - - if (buf_idx < RESPONSE_BUFFER_SIZE) - buf_idx++; - else { - std::memset(response_buf, 0, RESPONSE_BUFFER_SIZE); - buf_idx = 0; - } - } - return c; -} - -void HttpClient::setEnpoint(const String &endpoint) -{ - this->endpoint = endpoint; -} - -bool HttpClient::sendJson(const JsonWriterStatic &json) -{ - if (!connected()) { - Log.warn("Failed to send JSON payload, not connected to endpoint."); - return false; - } - - Log.info("Sending POST request:"); - Log.info("POST %s HTTP/1.1", endpoint.c_str()); - Log.info("Host: %s:%d", (use_host ? host.c_str() : ip.toString().c_str()), port); - Log.info("User-Agent: argon/0.0.1"); - Log.info("Accept: */*"); - Log.info("Content-Type: application/json"); - Log.info("Content-Length: %d", payloadLength(json)); - Log.info(json.getBuffer()); - - client.print("POST "); - client.print(endpoint); - client.print(" HTTP/1.1\r\n"); - client.print("Host: "); - client.print(use_host ? host : ip.toString()); - client.print(":"); - client.print(port); - client.print("\r\n"); - client.print("User-Agent: argon/0.0.1\r\n"); - client.print("Accept: */*\r\n"); - client.print("Content-Type: application/json\r\n"); - client.print("Content-Length: "); - client.print(payloadLength(json)); - client.print("\r\n"); - client.print("\r\n"); - client.print(json.getBuffer()); - client.print("\r\n"); - - return true; -} - -uint16_t HttpClient::payloadLength(const JsonWriterStatic &json) -{ - const char *buf = json.getBuffer(); - for (uint16_t i=0; i &json); - - protected: - HttpClient(); - - uint16_t payloadLength(const JsonWriterStatic &json); - bool httpResponseEnd(); - - private: - TCPClient client; - IPAddress ip; - String host; - uint16_t port; - bool use_host; - String endpoint; - - //char *response_buf; - char response_buf[RESPONSE_BUFFER_SIZE]; - uint16_t buf_idx; - bool received_full_response; -}; - -#endif \ No newline at end of file diff --git a/src/humidity_sensor.cpp b/src/humidity_sensor.cpp deleted file mode 100644 index 7cc3ec8..0000000 --- a/src/humidity_sensor.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "humidity_sensor.h" - -HumiditySensor::HumiditySensor() -{ - BaseSensor::setup(); -} - -HumiditySensor::~HumiditySensor() -{ -} - -float HumiditySensor::read(bool mock_data) -{ - static float lastValue = 0; - if (mock_data) { - // Adjust this by a little bit each check so we can see it change - if (rand() > (RAND_MAX / 2)) { - lastValue += 5.1; - } - else { - lastValue -= 5.1; - } - } else { - // Read from the BME280 sensor - lastValue = BaseSensor::sensor().readHumidity(); - } - - return lastValue; -} \ No newline at end of file diff --git a/src/humidity_sensor.h b/src/humidity_sensor.h deleted file mode 100644 index d246a30..0000000 --- a/src/humidity_sensor.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HUMIDITY_SENSOR_H -#define HUMIDITY_SENSOR_H - -#include "base_sensor.h" - -class HumiditySensor : public BaseSensor -{ - public: - HumiditySensor(); - ~HumiditySensor(); - - virtual float read(bool mock_data = false); -}; - -#endif \ No newline at end of file diff --git a/src/pressure_sensor.cpp b/src/pressure_sensor.cpp deleted file mode 100644 index 9c29687..0000000 --- a/src/pressure_sensor.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "pressure_sensor.h" - -PressureSensor::PressureSensor() -{ - BaseSensor::setup(); -} - -PressureSensor::~PressureSensor() -{ -} - -float PressureSensor::read(bool mock_data) -{ - static float lastValue = 0; - if (mock_data) { - // Adjust this by a little bit each check so we can see it change - if (rand() > (RAND_MAX / 2)) { - lastValue += 0.1; - } - else { - lastValue -= 0.1; - } - } else { - // Read from the BME280 sensor - lastValue = BaseSensor::sensor().readPressure() / 100.0F; - } - - return lastValue; -} \ No newline at end of file diff --git a/src/pressure_sensor.h b/src/pressure_sensor.h deleted file mode 100644 index eb6c581..0000000 --- a/src/pressure_sensor.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef PRESSURE_SENSOR_H -#define PRESSURE_SENSOR_H - -#include "base_sensor.h" - -class PressureSensor : public BaseSensor -{ - public: - PressureSensor(); - ~PressureSensor(); - - virtual float read(bool mock_data = false); -}; - -#endif \ No newline at end of file diff --git a/src/sensorchi.cpp b/src/sensorchi.cpp deleted file mode 100644 index d8bee7b..0000000 --- a/src/sensorchi.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/******************************************************/ -// THIS IS A GENERATED FILE - DO NOT EDIT // -/******************************************************/ - -#include "Particle.h" -#line 1 "/home/jhodapp/Projects/particle/sensorchi/src/sensorchi.ino" -/* - * Project sensorchi - * Description: - * Author: Jim Hodapp - * Date: 12/2/20 - */ - -#include "temperature_sensor.h" - -// This firmware works better with system thread enabled, otherwise it is not -// initialized until you've already connected to the cloud, which is not as useful. -void setup(); -void loop(); -#line 12 "/home/jhodapp/Projects/particle/sensorchi/src/sensorchi.ino" -SYSTEM_THREAD(ENABLED); - -TemperatureSensor temp_sensor; - -// setup() runs once, when the device is first turned on. -void setup() { - // Put initialization like pinMode and begin functions here. - - -} - -// loop() runs over and over again, as quickly as it can execute. -void loop() { - // The core of your code will likely live here. - -} \ No newline at end of file diff --git a/src/sensorchi.ino b/src/sensorchi.ino deleted file mode 100644 index 7d50529..0000000 --- a/src/sensorchi.ino +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Project sensorchi - * Description: - * Author: Jim Hodapp - * Date: 12/2/20 - */ - -#include "air_purity_sensor.h" -#include "dust_sensor.h" -#include "http_client.h" -#include "humidity_sensor.h" -#include "pressure_sensor.h" -#include "temperature_sensor.h" - -#include "JsonParserGeneratorRK.h" -#include "Particle.h" -#include "SeeedOLED.h" - -// Define DEV to point to development server -//#define DEV - -// This firmware works better with system thread enabled, otherwise it is not -// initialized until you've already connected to the cloud, which is not as useful. -SYSTEM_THREAD(ENABLED); - -AirPuritySensor air_purity_sensor; -DustSensor dust_sensor; -HumiditySensor humidity_sensor; -PressureSensor pressure_sensor; -TemperatureSensor temp_sensor; - -SerialLogHandler serialLogHandler(LOG_LEVEL_TRACE); - -const byte dev_server[] = { 192, 168, 1, 252 }; -const byte prod_server[] = { 192, 168, 1, 253 }; - -HttpClient http_client_dev(dev_server, 4000); -HttpClient http_client_prod(prod_server, 4000); - -const unsigned long PUSH_SENSOR_DATA_INTERVAL = 10000; // milliseconds -unsigned long lastPushSensorData = 0; -float temp = 0, humidity = 0, dust_concentration = 0; -int pressure = 0; -String air_purity; - -static void DisplayOledReadings(); -static void LogReadings(); -static void ReadSensors(); -static void CreateAndSendSensorPayload(); - -// setup() runs once, when the device is first turned on. -void setup() { - // I2C bus init for use with OLED display - Wire.begin(); - SeeedOled.init(); - SeeedOled.clearDisplay(); - SeeedOled.setNormalDisplay(); - SeeedOled.setPageMode(); - - while (!WiFi.ready()); -#ifdef DEV - http_client_dev.connect(); -#else - http_client_prod.connect(); -#endif - - Log.info("Setup complete."); -} - -// loop() runs over and over again, as quickly as it can execute. -void loop() { - if (millis() - lastPushSensorData >= PUSH_SENSOR_DATA_INTERVAL) { - lastPushSensorData = millis(); - - ReadSensors(); - DisplayOledReadings(); - LogReadings(); - SetReadingMetadata(); - CreateAndSendSensorPayload(); - } - -#ifdef DEV - if (http_client_dev.available()) { - const char c = http_client_dev.read(); - Serial.print(c); - } -#else - if (http_client_prod.available()) { - const char c = http_client_prod.read(); - Serial.print(c); - } -#endif -} - -static void DisplayOledReadings() -{ - SeeedOled.setTextXY(1, 0); - SeeedOled.putString(" "); - SeeedOled.putString("Temp: "); - SeeedOled.putNumber(temp); - SeeedOled.putString("C"); - - SeeedOled.setTextXY(2, 0); - SeeedOled.putString(" "); - SeeedOled.putString("Humidity: "); - SeeedOled.putNumber(humidity); - SeeedOled.putString("%"); - - SeeedOled.setTextXY(3, 0); - SeeedOled.putString(" "); - SeeedOled.putString("Pressure: "); - SeeedOled.putNumber(pressure); - SeeedOled.putString(" mb"); - - SeeedOled.setTextXY(4, 0); - SeeedOled.putString(" "); - SeeedOled.putString("Dust: "); - SeeedOled.putNumber(dust_concentration); - SeeedOled.putString(" pcs/L"); - - SeeedOled.setTextXY(5, 0); - SeeedOled.putString(" "); - SeeedOled.putString(air_purity); -} - -static void LogReadings() -{ - //Log.info(""); - Log.info("temp: %0.0fC", temp); - Log.info("humidity: %0.1f percent", humidity); - Log.info("pressure: %d mb", pressure); - Log.info("dust: %0.1f pcs/L", dust_concentration); - Log.info(air_purity.c_str()); -} - -static void ReadSensors() -{ - temp_sensor.setTemperatureUnits(TemperatureSensor::TemperatureUnits::Celcius); - temp = temp_sensor.read(); - - humidity = humidity_sensor.read(); - - pressure = static_cast(pressure_sensor.read()); - - air_purity = air_purity_sensor.read_str(); - - dust_concentration = dust_sensor.read(); -} - -static void CreateAndSendSensorPayload() -{ -#ifdef DEV - if (!http_client_dev.connected()) - http_client_dev.connect(); -#else - if (!http_client_prod.connected()) - http_client_prod.connect(); -#endif - -#ifdef DEV - if (http_client_dev.connected()) - { - JsonWriterStatic jw; - { - JsonWriterAutoObject obj(&jw); - jw.insertKeyValue("temperature", temp); - jw.insertKeyValue("humidity", humidity); - jw.insertKeyValue("pressure", pressure); - jw.insertKeyValue("dust_concentration", dust_concentration); - jw.insertKeyValue("air_purity", air_purity); - } - - Log.info("ip address: %s", WiFi.localIP().toString().c_str()); - Log.info("gateway: %s", WiFi.gatewayIP().toString().c_str()); - - http_client_dev.setEnpoint("/api/readings/add"); - http_client_dev.sendJson(jw); - } else { - Log.error("http_client_dev not connected to server"); - } -#else - if (http_client_prod.connected()) - { - JsonWriterStatic jw; - { - JsonWriterAutoObject obj(&jw); - jw.insertKeyValue("temperature", temp); - jw.insertKeyValue("humidity", humidity); - jw.insertKeyValue("pressure", pressure); - jw.insertKeyValue("dust_concentration", dust_concentration); - jw.insertKeyValue("air_purity", air_purity); - } - - Log.info("ip address: %s", WiFi.localIP().toString().c_str()); - Log.info("gateway: %s", WiFi.gatewayIP().toString().c_str()); - - http_client_prod.setEnpoint("/api/readings/add"); - http_client_prod.sendJson(jw); - } else { - Log.error("http_client_prod not connected to server"); - } -#endif -} - -static void SetReadingMetadata() -{ -#ifdef DEV - if (!http_client_dev.connected()) - http_client_dev.connect(); -#else - if (!http_client_prod.connected()) - http_client_prod.connect(); -#endif - -#ifdef DEV - if (http_client_dev.connected()) - { - JsonWriterStatic jw; - { - JsonWriterAutoObject obj(&jw); - jw.insertKeyValue("timestamp_resolution_seconds", PUSH_SENSOR_DATA_INTERVAL / 1000); - } - - Log.info("ip address: %s", WiFi.localIP().toString().c_str()); - Log.info("gateway: %s", WiFi.gatewayIP().toString().c_str()); - - http_client_dev.setEnpoint("/api/reading_metadata/set"); - http_client_dev.sendJson(jw); - } else { - Log.error("http_client_dev not connected to server"); - } -#else - if (http_client_prod.connected()) - { - JsonWriterStatic jw; - { - JsonWriterAutoObject obj(&jw); - jw.insertKeyValue("timestamp_resolution_seconds", PUSH_SENSOR_DATA_INTERVAL / 1000); - } - - Log.info("ip address: %s", WiFi.localIP().toString().c_str()); - Log.info("gateway: %s", WiFi.gatewayIP().toString().c_str()); - - http_client_prod.setEnpoint("/api/reading_metadata/set"); - http_client_prod.sendJson(jw); - } else { - Log.error("http_client_prod not connected to server"); - } -#endif -} \ No newline at end of file diff --git a/src/temperature_sensor.cpp b/src/temperature_sensor.cpp deleted file mode 100644 index 759e4c3..0000000 --- a/src/temperature_sensor.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "temperature_sensor.h" - -TemperatureSensor::TemperatureSensor() - : temperature_units(TemperatureUnits::Celcius) -{ - BaseSensor::setup(); -} - -TemperatureSensor::~TemperatureSensor() -{ -} - -float TemperatureSensor::read(bool mock_data) -{ - static float lastValue = 0; - if (mock_data) { - // Adjust this by a little bit each check so we can see it change - if (rand() > (RAND_MAX / 2)) { - lastValue += 0.1; - } - else { - lastValue -= 0.1; - } - } else { - // Read from the BME280 sensor - lastValue = BaseSensor::sensor().readTemperature(); - } - - return convertUnits(lastValue); -} - -void TemperatureSensor::setTemperatureUnits(TemperatureSensor::TemperatureUnits units) -{ - temperature_units = units; -} - -TemperatureSensor::TemperatureUnits TemperatureSensor::getTemperatureUnits() const -{ - return temperature_units; -} - -float TemperatureSensor::convertUnits(float temp) -{ - switch (temperature_units) - { - case TemperatureUnits::Fahrenheit: - return ((temp * 9.0) / 5.0) + 32.0; - case TemperatureUnits::Kelvin: - return temp + 273.15; - case TemperatureUnits::Celcius: - default: - return temp; - } -} \ No newline at end of file diff --git a/src/temperature_sensor.h b/src/temperature_sensor.h deleted file mode 100644 index f14cb93..0000000 --- a/src/temperature_sensor.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef TEMPERATURE_SENSOR_H -#define TEMPERATURE_SENSOR_H - -#include "base_sensor.h" - -class TemperatureSensor : public BaseSensor -{ - public: - enum TemperatureUnits { Celcius, Fahrenheit, Kelvin }; - TemperatureSensor(); - ~TemperatureSensor(); - - virtual float read(bool mock_data = false); - - void setTemperatureUnits(TemperatureUnits units); - TemperatureUnits getTemperatureUnits() const; - - private: - TemperatureUnits temperature_units; - - float convertUnits(float temp); -}; - -#endif \ No newline at end of file