1
0
mirror of https://github.com/daniviga/bite.git synced 2024-12-04 18:15:03 +01:00
bite/arduino/tempLightSensor/tempLightSensor.ino

150 lines
3.5 KiB
Arduino
Raw Normal View History

2020-06-02 22:38:20 +02:00
#include <EEPROM.h>
2020-06-02 00:57:20 +02:00
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <NTPClient.h>
2020-06-02 00:57:20 +02:00
#include <ArduinoJson.h>
#define DEBUG_TO_SERIAL 1
#define USE_INTERNAL_NTP 0 // use default ntp server or the internal one
2020-06-02 00:57:20 +02:00
#define AREF_VOLTAGE 3.3
// const String serverName = "sensor.server.domain";
2020-06-03 23:53:25 +02:00
const size_t capacity = 2 * JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(2) + 20;
2020-06-02 00:57:20 +02:00
2020-06-03 23:53:25 +02:00
DynamicJsonDocument telemetry(capacity);
JsonObject payload = telemetry.createNestedObject("payload");
2020-06-02 00:57:20 +02:00
JsonObject temp = payload.createNestedObject("temperature");
unsigned int counter = 0;
2020-06-02 00:57:20 +02:00
int tempPin = A0;
int photocellPin = A1;
EthernetUDP ntpUDP;
NTPClient timeClient(ntpUDP);
bool NTPValid = false;
2020-06-02 22:38:20 +02:00
struct netConfig {
IPAddress address;
unsigned int port;
2020-06-02 22:38:20 +02:00
};
netConfig config;
2020-06-02 00:57:20 +02:00
2020-06-03 23:53:25 +02:00
const String apiURL = "/api/subscribe/";
const String telemetryURL = "/telemetry/";
2020-06-02 22:38:20 +02:00
const int postDelay = 10 * 1000;
2020-06-02 00:57:20 +02:00
void setup(void) {
Serial.begin(9600);
2020-06-02 00:57:20 +02:00
analogReference(EXTERNAL);
2020-06-02 22:38:20 +02:00
2020-06-03 23:53:25 +02:00
StaticJsonDocument<20> api;
2020-06-02 22:38:20 +02:00
byte mac[6];
char serial[9];
2020-06-02 22:38:20 +02:00
int eeAddress = 0;
EEPROM.get(eeAddress, mac);
eeAddress += sizeof(mac);
EEPROM.get(eeAddress, serial);
eeAddress += sizeof(serial);
2020-06-02 00:57:20 +02:00
if (Ethernet.begin(mac) == 0) {
2020-06-02 22:38:20 +02:00
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("ERROR: ethernet shield was not found.");
2020-06-02 22:38:20 +02:00
}
while (true) {
delay(1);
}
}
EEPROM.get(eeAddress, config);
2020-06-02 22:38:20 +02:00
Serial.print("IoT #");
Serial.print(serial);
Serial.println(" at address:");
2020-06-02 22:38:20 +02:00
Serial.println(Ethernet.localIP());
2020-06-02 00:57:20 +02:00
Serial.println();
2020-06-02 22:38:20 +02:00
Serial.println("Connecting to:");
Serial.print(config.address);
Serial.print(":");
Serial.println(config.port);
2020-06-02 00:57:20 +02:00
#if USE_INTERNAL_NTP
timeClient.setPoolServerIP(config.address);
#endif
timeClient.begin();
if (timeClient.update()) {
NTPValid = true;
}
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: clock updated via NTP.");
#endif
2020-06-03 23:53:25 +02:00
api["serial"] = serial;
postData(config, apiURL, api);
telemetry["device"] = serial;
// payload["id"] = serverName;
2020-06-02 00:57:20 +02:00
}
void loop(void) {
unsigned int photocellReading = analogRead(photocellPin);
unsigned int tempReading = analogRead(tempPin);
2020-06-02 00:57:20 +02:00
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
float tempC = (tempVoltage - 0.5) * 100 ;
if (NTPValid) {
2020-06-03 23:53:25 +02:00
telemetry["clock"] = timeClient.getEpochTime();
} else {
2020-06-03 23:53:25 +02:00
telemetry["clock"] = NULL; // converted into 0
}
2020-06-02 00:57:20 +02:00
payload["light"] = photocellReading;
temp["celsius"] = tempC;
temp["raw"] = tempReading;
temp["volts"] = tempVoltage;
2020-06-03 23:53:25 +02:00
postData(config, telemetryURL, telemetry);
if (counter == 6 * 120) { // Update clock every 6 times * 10 sec * 120 minutes = 2 hrs
timeClient.update();
counter = 0;
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: clock updated via NTP.");
#endif
} else {
counter++;
}
delay(postDelay);
}
void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) {
if (EthernetClient client = client.connect(postAPI.address, postAPI.port)) {
2020-06-02 00:57:20 +02:00
client.print("POST ");
client.print(URL);
client.println(" HTTP/1.1");
client.print("Host: ");
2020-06-03 23:53:25 +02:00
client.print(postAPI.address);
2020-06-02 00:57:20 +02:00
client.print(":");
2020-06-03 23:53:25 +02:00
client.println(postAPI.port);
2020-06-02 00:57:20 +02:00
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(measureJsonPretty(json));
2020-06-02 00:57:20 +02:00
client.println("Connection: close");
client.println();
serializeJson(json, client);
2020-06-02 00:57:20 +02:00
client.stop();
#if DEBUG_TO_SERIAL
Serial.println("DEBUG: >>>");
serializeJsonPretty(json, Serial);
Serial.println("\n<<<");
#endif
}
2020-06-02 00:57:20 +02:00
}