From 0dbf1fc658763bf67757ee4b923e658ea0407f7a Mon Sep 17 00:00:00 2001 From: curtainman <aschwarz@xephyr.xyz> Date: Fri, 7 Apr 2023 23:45:18 -0500 Subject: [PATCH] Add collection.ino --- collection.ino | 151 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 collection.ino diff --git a/collection.ino b/collection.ino new file mode 100644 index 0000000..b2ccf51 --- /dev/null +++ b/collection.ino @@ -0,0 +1,151 @@ +#include <PubSubClient.h> +#include <rpcWiFi.h> +#include <TFT_eSPI.h> +#include "sensirion_common.h" +#include "sgp30.h" + +#define WIFISSID "REDACTED" // Put your WifiSSID here +#define PASSWORD "REDACTED" // Put your wifi password here +#define TOKEN "REDACTED" // Put your Ubidots' TOKEN +#define VARIABLE_LABEL1 "voc" // Assign the variable label +#define VARIABLE_LABEL2 "co2" +#define DEVICE_LABEL "co2-monitor-device" // Assign the device label +#define MQTT_CLIENT_NAME "REDACTED" // MQTT client Name + +const long interval = 100; +unsigned long previousMillis = 0; + +char mqttBroker[] = "industrial.api.ubidots.com"; + +WiFiClient wifiClient; +PubSubClient client(wifiClient); + +TFT_eSPI tft = TFT_eSPI(); + +static unsigned short int VOC = 0; +static unsigned short int CO2 = 0; + +// Space to store values to send +char str_voc[6]; +char str_co2[6]; +char payload[700]; +char topic[150]; + +void callback(char* topic, byte* payload, unsigned int length){ + Serial.print("Message arrived ["); + Serial.print(topic); + Serial.print("] "); + for (int i=0;i<length;i++) { + Serial.print((char)payload[i]); + } +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.println("Attempting MQTT connection..."); + + // Attempt to connect + if (client.connect(MQTT_CLIENT_NAME, TOKEN,"")) { + Serial.println("connected"); + } + else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 2 seconds"); + // Wait 2 seconds before retrying + delay(2000); + } + } +} + +void read_sgp30() +{ + s16 err = 0; + sgp_measure_iaq_blocking_read(&VOC, &CO2); + if (err == STATUS_OK) { + Serial.print("tVOC Concentration:"); + Serial.print(VOC); + Serial.println("ppb"); + + Serial.print("CO2eq Concentration:"); + Serial.print(CO2); + Serial.println("ppm"); + } else { + Serial.println("error reading IAQ values\n"); + } +} + +void send_data() +{ + dtostrf(VOC, 4, 0, str_voc); + dtostrf(CO2, 4, 0, str_co2); + + if (!client.connected()) { + reconnect(); + } + + // Builds the topic + sprintf(topic, "%s", ""); // Cleans the topic content + sprintf(topic, "%s%s", "/v2.0/devices/", DEVICE_LABEL); + + //Builds the payload + sprintf(payload, "%s", ""); // Cleans the payload + sprintf(payload, "{\"%s\":", VARIABLE_LABEL1); // Adds the variable label + sprintf(payload, "%s%s", payload, str_voc); // Adds the value + sprintf(payload, "%s}", payload); // Closes the dictionary brackets + client.publish(topic, payload); + Serial.println(payload); + delay(500); + + sprintf(payload, "%s", ""); // Cleans the payload + sprintf(payload, "{\"%s\":", VARIABLE_LABEL2); // Adds the variable label + sprintf(payload, "%s%s", payload, str_co2); // Adds the value + sprintf(payload, "%s}", payload); // Closes the dictionary brackets + client.publish(topic, payload); + Serial.println(payload); + delay(500); + + client.loop(); +} + +void setup() { + Serial.begin(115200); + while (sgp_probe() != STATUS_OK) { + Serial.println("SGP failed"); + } + sgp_set_absolute_humidity(13000); + sgp_iaq_init(); + + tft.begin(); + tft.setRotation(3); + tft.setTextSize(2); + tft.fillScreen(TFT_BLACK); + +// while(!Serial); + + // Set WiFi to station mode and disconnect from an AP if it was previously connected + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + + tft.drawString("Connecting to WiFi...",20,120); + WiFi.begin(WIFISSID, PASSWORD); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + WiFi.begin(WIFISSID, PASSWORD); + } + + tft.fillScreen(TFT_BLACK); + tft.drawString("Connected to the WiFi",20,120); + + delay(1000); + client.setServer(mqttBroker, 1883); + client.setCallback(callback); +} + +void loop() { + read_sgp30(); //Reading sgp30 sensor values + send_data(); //Sending data to Ubidots + delay(2000); +} \ No newline at end of file