123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- #include <MKRNB.h>
- #include <TinyGPS++.h>
- #include <SparkFun_I2C_GPS_Arduino_Library.h>
- #include <RGBLed.h>
- #include <Arduino.h>
- #include <SensirionI2CSen5x.h>
- #include <Wire.h>
- #define MAXBUF_REQUIREMENT 48
- #if (defined(I2C_BUFFER_LENGTH) && (I2C_BUFFER_LENGTH >= MAXBUF_REQUIREMENT)) || (defined(BUFFER_LENGTH) && BUFFER_LENGTH >= MAXBUF_REQUIREMENT)
- #define USE_PRODUCT_INFO
- #endif
- #define rLEDPIN 6
- #define gLEDPIN 7
- #define bLEDPIN 8
- #define batPin 5
- RGBLed ledButton(rLEDPIN, gLEDPIN, bLEDPIN, RGBLed::COMMON_ANODE);
- byte curR = 0;
- byte curG = 0;
- byte curB = 0;
- SensirionI2CSen5x sen5x;
- I2CGPS myI2CGPS;
- TinyGPSPlus gps;
- boolean encodeGPSAgain = false;
- NBClient client(false);
- GPRS gprs;
- NB nbAccess;
- boolean connected = false;
- char server[] = "aml.media.tuwien.ac.at";
- char path[] = "/api/sensordata/eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjI0LCJpYXQiOjE3NDY3MDgxOTJ9.pKcHaTF88j6usI55lnWdgdXk9PESK4mGbKn6nf9Vv7A";
- int port = 11312;
- float lastPM1 = 0;
- float lastPM25 = 0;
- float lastPM4 = 0;
- float lastPM10 = 0;
- float lastHumidity = 0;
- float lastTemperature = 0;
- float lastVocIndex = 0;
- float lastNoxIndex = 0;
- unsigned long currentMillis = 0;
- uint32_t printTimer = millis();
- unsigned long sendToDATAhubInterval = 20000;
- unsigned long lastSendToDATAhub = 0;
- byte statusInfo = 0;
- void setup() {
- updateLED(statusInfo);
- pinMode(batPin, INPUT_PULLUP);
- Serial.begin(115200);
- Serial1.begin(9600);
-
-
-
-
-
-
- connected = false;
-
-
- connectToNetwork();
- updateLED(statusInfo);
- Wire.begin();
-
-
- if (myI2CGPS.begin() == false) {
-
- statusInfo = 3;
- updateLED(statusInfo);
- while (1)
- ;
- }
- encodeGPS();
- updateLED(statusInfo);
-
-
- sen5x.begin(Wire);
- uint16_t error;
- char errorMessage[256];
- error = sen5x.deviceReset();
- if (error) {
-
- errorToString(error, errorMessage, 256);
-
- statusInfo = 6;
- updateLED(statusInfo);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- float tempOffset = 0.0;
- error = sen5x.setTemperatureOffsetSimple(tempOffset);
- if (error) {
-
- errorToString(error, errorMessage, 256);
-
- statusInfo = 6;
- updateLED(statusInfo);
- } else {
-
-
-
- }
-
- readSEN();
- updateLED(statusInfo);
- if (statusInfo == 0) statusInfo = 1;
- updateLED(statusInfo);
- }
- void loop() {
-
-
- currentMillis = millis();
- if ((currentMillis - lastSendToDATAhub >= sendToDATAhubInterval)) {
- lastSendToDATAhub = currentMillis;
- updateLED(2);
- Serial.println("start sending");
-
-
-
-
-
-
- encodeGPS();
-
-
-
- readSEN();
-
- delay(1000);
-
-
-
-
- int satNum = gps.satellites.value();
- Serial.println("Satellites: " + String(satNum));
-
- if (statusInfo != 3 && statusInfo != 4 && statusInfo != 6) {
- sendToDATAhub();
- }
-
-
- updateLED(statusInfo);
- }
- }
- void sendToDATAhub() {
-
-
- String dataString = getGeoJSONDataString();
-
- unsigned int len = dataString.length() + 1;
- char data[len];
- dataString.toCharArray(data, len);
-
-
- Serial.println("Server: " + String(server) + " Port: " + String(port));
-
- if (client.connect(server, port)) {
-
- Serial.println("connected to server -> YAY I CAME ACROSS THE CONNECTION PART!");
-
-
-
- client.print("POST ");
- client.print(path);
- client.println(" HTTP/1.1");
- client.print("Host: ");
- client.println(server);
- client.println("Content-Type: text/plain");
- client.print("Content-Length: ");
- client.println(strlen(data));
- client.println();
- client.println(data);
- client.println("Connection: close");
- client.println();
-
- Serial.println("...sent to DATAhub!");
- if(statusInfo ==2) statusInfo = 1;
-
- } else {
-
- Serial.println("sendToDATAhub: connection failed");
- statusInfo = 4;
- updateLED(statusInfo);
- connected = false;
- Serial.println("Shutting down the modem");
- nbAccess.shutdown();
- delay(5000);
- connectToNetwork();
-
-
- }
- }
- void connectToNetwork() {
-
-
-
- while (!connected) {
-
-
- Serial.println("Connecting to network!");
-
- if ((nbAccess.begin() == NB_READY)) {
- Serial.println("Connected to NB-IOT Service (A1)");
- connected = true;
- if(statusInfo ==2) statusInfo = 1;
- } else {
- Serial.println("Not connected to NB-IOT Service (A1)");
- delay(1000);
- statusInfo = 4;
- updateLED(statusInfo);
- }
- }
- }
- void encodeGPS() {
- unsigned long start = millis();
-
- for (start; millis() - start < 1000;) {
- while (myI2CGPS.available()) {
- gps.encode(myI2CGPS.read());
- }
- }
-
-
- if (start > 5000 && gps.charsProcessed() < 10) {
-
- statusInfo = 3;
- updateLED(statusInfo);
- encodeGPS();
- } else if (!gps.location.isValid()) {
- Serial.println("ERROR: GPS Data not valid! Encoding again");
- statusInfo = 3;
- updateLED(statusInfo);
- encodeGPS();
- } else if (gps.satellites.value() < 3) {
- Serial.println("ERROR: Not enough GPS satellites");
- statusInfo = 3;
- updateLED(statusInfo);
-
- } else {
- if(statusInfo ==2) statusInfo = 1;
- }
-
- }
- void printGPSData() {
-
- Serial.println();
- if (gps.time.isValid()) {
- Serial.print(F("Date: "));
- Serial.print(gps.date.month());
- Serial.print(F("/"));
- Serial.print(gps.date.day());
- Serial.print(F("/"));
- Serial.print(gps.date.year());
- Serial.print((" Time: "));
- if (gps.time.hour() < 10) Serial.print(F("0"));
- Serial.print(gps.time.hour());
- Serial.print(F(":"));
- if (gps.time.minute() < 10) Serial.print(F("0"));
- Serial.print(gps.time.minute());
- Serial.print(F(":"));
- if (gps.time.second() < 10) Serial.print(F("0"));
- Serial.print(gps.time.second());
- Serial.println();
- } else {
- Serial.println(F("Time not yet valid"));
- }
- if (gps.location.isValid()) {
- Serial.print("Location: ");
- Serial.print(gps.location.lat(), 6);
- Serial.print(F(", "));
- Serial.print(gps.location.lng(), 6);
- Serial.println();
- } else {
- Serial.println(F("Location not yet valid"));
- }
- }
- void readSEN() {
-
-
-
- uint16_t error;
- char errorMessage[256];
- error = sen5x.startMeasurement();
- if (error) {
-
- errorToString(error, errorMessage, 256);
-
- statusInfo = 6;
- updateLED(statusInfo);
- }
- error = sen5x.readMeasuredValues(
- lastPM1, lastPM25, lastPM4,
- lastPM10, lastHumidity, lastTemperature, lastVocIndex,
- lastNoxIndex);
- if (error) {
-
- errorToString(error, errorMessage, 256);
-
- statusInfo = 6;
- updateLED(statusInfo);
- } else {
-
- if(statusInfo ==2) statusInfo = 1;
-
- }
-
- delay(1000);
-
- }
- void printSen54Data() {
- Serial.print("PM 1.0: ");
- Serial.print(lastPM1);
- Serial.print("\t");
- Serial.print("PM 2.5: ");
- Serial.print(lastPM25);
- Serial.print("\t");
- Serial.print("PM 4.0: ");
- Serial.print(lastPM4);
- Serial.print("\t");
- Serial.print("PM 10.0: ");
- Serial.print(lastPM10);
- Serial.print("\t");
- Serial.print("Humidity: ");
- if (isnan(lastHumidity)) {
- Serial.print("n/a");
- } else {
- Serial.print(lastHumidity);
- }
- Serial.print("\t");
- Serial.print("Temperature: ");
- if (isnan(lastTemperature)) {
- Serial.print("n/a");
- } else {
- Serial.print(lastTemperature);
- }
- Serial.print("\t");
- Serial.print("VocIndex: ");
- if (isnan(lastVocIndex)) {
- Serial.print("n/a");
- } else {
- Serial.print(lastVocIndex);
- }
- Serial.print("\t");
- Serial.print("NoxIndex: ");
- if (isnan(lastNoxIndex)) {
- Serial.println("n/a");
- } else {
- Serial.println(lastNoxIndex);
- }
- }
- String getGeoJSONDataString() {
-
-
- String json = "{\"type\":\"Feature\",\"geometry\":{\"type\": \"Point\", \"coordinates\":[" + String(gps.location.lng(), 6) + "," + String(gps.location.lat(), 6) + "]}, \"properties\": {\"PM 1\":[" + String(lastPM1) + ", \"myg/m3\"], \"PM 2.5\":[" + String(lastPM25) + ", \"myg/m3\"], \"PM 4\":[" + String(lastPM4) + ", \"myg/m3\"], \"PM 10\":[" + String(lastPM10) + ", \"myg/m3\"], \"Humidity\":[" + String(lastHumidity) + ", \"%\"], \"Temperature\":[" + String(lastTemperature) + ", \"°C\"], \"VOC\":[" + String(lastVocIndex) + ", \"VOC Index\"], \"timestamp\":\"""\"}}";
-
-
-
- return json;
- }
- String getGeoJSONTestString() {
-
-
- String json = "{\"type\":\"Feature\",\"geometry\":{\"type\": \"Point\", \"coordinates\":[16.504511, 48.225247]}, \"properties\": {\"PM 1\":[" + String(lastPM1) + ", \"myg/m3\"], \"PM 2.5\":[" + String(lastPM25) + ", \"myg/m3\"], \"PM 4\":[" + String(lastPM4) + ", \"myg/m3\"], \"PM 10\":[" + String(lastPM10) + ", \"myg/m3\"], \"Humidity\":[" + String(lastHumidity) + ", \"%\"], \"Temperature\":[" + String(lastTemperature) + ", \"°C\"], \"VOC\":[" + String(lastVocIndex) + ", \"VOC Index\"], \"timestamp\":\""
- "\"}}";
- return json;
- }
- void checkBattery() {
-
-
- int batState = digitalRead(batPin);
- if (batState == LOW) {
-
- statusInfo = 5;
- updateLED(statusInfo);
- }
-
- }
- void updateLED(byte status) {
-
-
-
-
-
-
-
-
-
- statusInfo = status;
- if (status == 0) {
- ledButton.crossFade(curR, curG, curB, 255, 255, 0, 10, 500);
- curR = 255;
- curG = 255;
- curB = 0;
- } else if (status == 1) {
- ledButton.crossFade(curR, curG, curB, 0, 255, 0, 10, 500);
- curR = 0;
- curG = 255;
- curB = 0;
-
- } else if (status == 2) {
- ledButton.crossFade(curR, curG, curB, 200, 255, 50, 10, 500);
-
- curR = 200;
- curG = 255;
- curB = 50;
- } else if (status == 3) {
- ledButton.crossFade(curR, curG, curB, 0, 0, 255, 10, 500);
- curR = 0;
- curG = 0;
- curB = 255;
- } else if (status == 4) {
- ledButton.crossFade(curR, curG, curB, 255, 0, 255, 10, 500);
- curR = 255;
- curG = 0;
- curB = 255;
- } else if (status == 5) {
- ledButton.crossFade(curR, curG, curB, 255, 0, 0, 10, 500);
- curR = 255;
- curG = 0;
- curB = 0;
- } else if (status == 6) {
- ledButton.crossFade(curR, curG, curB, 170, 0, 255, 10, 500);
- curR = 170;
- curG = 0;
- curB = 255;
- } else if (status == 7) {
- ledButton.crossFade(curR, curG, curB, 3, 252, 177, 10, 500);
- curR = 3;
- curG = 252;
- curB = 177;
- }
- statusInfo = status;
- }
|