Skip to content

Commit

Permalink
Report actual temp and humidity to c8y
Browse files Browse the repository at this point in the history
  • Loading branch information
amalabey committed Dec 6, 2020
1 parent 5534581 commit 2dc7632
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ lib_deps =
marvinroger/AsyncMqttClient@^0.8.2
me-no-dev/AsyncTCP@^1.1.1
knolleary/PubSubClient@^2.8
adafruit/DHT sensor library@^1.4.1
adafruit/Adafruit Unified Sensor@^1.1.4
build_flags = -D ESP32 -D CORE_DEBUG_LEVEL=5
23 changes: 20 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
#else //ESP8266
#include <ESP8266WiFi.h>
#endif
#include "Credentials.h"

#ifndef CONFIGURATION_H
#define CONFIGURATION_H
#include "Configuration.h"
#endif

#include "DHT.h"
#define DHTPIN 26
#define DHTTYPE DHT21 // AM2301
DHT dht(DHTPIN, DHTTYPE);

#define FORMAT_SPIFFS_IF_FAILED true

WiFiClientSecure _wifiClient = WiFiClientSecure();
Expand Down Expand Up @@ -54,6 +58,7 @@ void setup()
Configuration _config;
Settings_t settings = _config.getSettings(SPIFFS, Serial);

// Connect to Wifi
WiFi.begin(settings.wifiSsid, settings.wifiPassword);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
Expand All @@ -63,8 +68,8 @@ void setup()
}
Serial.println("connected to wifi");

// Connect to Cumulocity
connectCumulocityServer(settings, !_config.isPersisted(SPIFFS));

if (!_config.isPersisted(SPIFFS))
{
Credentials received = _client.getCredentials();
Expand All @@ -73,13 +78,25 @@ void setup()
settings.tenantId = received.tenant;
_config.persistSettings(SPIFFS, settings);
}

// Init temp/humidity sensor library
dht.begin();
}

void loop()
{
delay(1000);
_client.loop();
_client.createMeasurement("Temperature", "T", "20.5", (char *)"*C");

float temperature = dht.readTemperature();
char tempStr[10];
sprintf(tempStr, "%f", temperature);
_client.createMeasurement("c8y_Temperature", "T", tempStr, (char *)"*C");

float humidity = dht.readHumidity();
char humidityStr[10];
sprintf(humidityStr, "%f", humidity);
_client.createMeasurement("Humidity", "H", humidityStr, (char *)"%\t");

int8_t rssi = WiFi.RSSI();
char rssiStr[10];
Expand Down

0 comments on commit 2dc7632

Please sign in to comment.