2020-06-21 23:34:54 +02:00
|
|
|
/* -*- coding: utf-8 -*-
|
|
|
|
* vim: tabstop=2 shiftwidth=2 softtabstop=2
|
|
|
|
*
|
|
|
|
* BITE - A Basic/IoT/Example
|
2021-03-21 16:18:44 +01:00
|
|
|
* Copyright (C) 2020-2021 Daniele Viganò <daniele@vigano.me>
|
2020-06-21 23:34:54 +02:00
|
|
|
*
|
|
|
|
* BITE is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* BITE is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-02 22:38:20 +02:00
|
|
|
#include <EEPROM.h>
|
2020-06-02 00:57:20 +02:00
|
|
|
#include <Ethernet.h>
|
2020-06-03 18:53:33 +02:00
|
|
|
#include <EthernetUdp.h>
|
2020-06-08 23:17:16 +02:00
|
|
|
#include <PubSubClient.h>
|
2020-06-03 18:53:33 +02:00
|
|
|
#include <NTPClient.h>
|
2020-06-02 00:57:20 +02:00
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
2020-06-21 22:58:46 +02:00
|
|
|
#define DEBUG_TO_SERIAL 0 // debug on serial port
|
|
|
|
#define USE_MQTT 1 // use mqtt protocol instead of http post
|
|
|
|
#define USE_INTERNAL_NTP 1 // use default ntp server or the internal one
|
|
|
|
#define TELEMETRY_DELAY 10 // second between telemetry samples
|
|
|
|
#define AREF_VOLTAGE 3.3 // set aref voltage to 3.3v instead of default 5v
|
2020-06-08 23:17:16 +02:00
|
|
|
|
2020-06-03 18:53:33 +02:00
|
|
|
// 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");
|
|
|
|
|
2020-06-03 18:53:33 +02:00
|
|
|
unsigned int counter = 0;
|
2020-06-05 13:24:12 +02:00
|
|
|
|
2020-06-03 18:53:33 +02:00
|
|
|
EthernetUDP ntpUDP;
|
|
|
|
NTPClient timeClient(ntpUDP);
|
|
|
|
bool NTPValid = false;
|
|
|
|
|
2020-06-08 23:17:16 +02:00
|
|
|
EthernetClient ethClient;
|
|
|
|
PubSubClient clientMQTT(ethClient);
|
|
|
|
|
2020-06-02 22:38:20 +02:00
|
|
|
struct netConfig {
|
2021-12-08 22:27:42 +01:00
|
|
|
IPAddress iot_address;
|
|
|
|
unsigned int iot_port;
|
|
|
|
IPAddress ntp_address;
|
|
|
|
unsigned int ntp_port; // not used
|
|
|
|
} config;
|
2020-06-02 00:57:20 +02:00
|
|
|
|
2021-12-08 22:27:42 +01:00
|
|
|
char serial[9];
|
2020-06-05 13:24:12 +02:00
|
|
|
const String apiURL = "/api/device/subscribe/";
|
2020-06-03 23:53:25 +02:00
|
|
|
const String telemetryURL = "/telemetry/";
|
2020-06-02 00:57:20 +02:00
|
|
|
|
|
|
|
void setup(void) {
|
|
|
|
Serial.begin(9600);
|
2020-06-03 18:53:33 +02:00
|
|
|
|
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];
|
|
|
|
int eeAddress = 0;
|
|
|
|
|
|
|
|
EEPROM.get(eeAddress, mac);
|
|
|
|
eeAddress += sizeof(mac);
|
|
|
|
EEPROM.get(eeAddress, serial);
|
|
|
|
eeAddress += sizeof(serial);
|
2020-06-03 18:53:33 +02:00
|
|
|
|
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) {
|
2020-06-03 18:53:33 +02:00
|
|
|
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-03 18:53:33 +02:00
|
|
|
|
2020-06-02 22:38:20 +02:00
|
|
|
Serial.print("IoT #");
|
|
|
|
Serial.print(serial);
|
2020-06-08 23:17:16 +02:00
|
|
|
Serial.print(" 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-08 23:17:16 +02:00
|
|
|
Serial.print("Connecting to: ");
|
2021-12-08 22:27:42 +01:00
|
|
|
Serial.print(config.iot_address);
|
2020-06-02 22:38:20 +02:00
|
|
|
Serial.print(":");
|
2021-12-08 22:27:42 +01:00
|
|
|
Serial.print(config.iot_port);
|
2021-03-21 23:57:00 +01:00
|
|
|
Serial.print(" every ");
|
|
|
|
Serial.print(TELEMETRY_DELAY);
|
|
|
|
Serial.println("s");
|
2020-06-02 00:57:20 +02:00
|
|
|
|
2020-06-03 18:53:33 +02:00
|
|
|
#if USE_INTERNAL_NTP
|
2021-12-08 22:27:42 +01:00
|
|
|
Serial.print("Using NTP: ");
|
|
|
|
Serial.println(config.ntp_address);
|
|
|
|
timeClient.setPoolServerIP(config.ntp_address);
|
2020-06-03 18:53:33 +02:00
|
|
|
#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;
|
2020-06-03 18:53:33 +02:00
|
|
|
// payload["id"] = serverName;
|
2020-06-08 23:17:16 +02:00
|
|
|
|
|
|
|
#if USE_MQTT
|
2021-12-08 22:27:42 +01:00
|
|
|
clientMQTT.setServer(config.iot_address, 1883);
|
2020-06-08 23:17:16 +02:00
|
|
|
#endif
|
2020-06-02 00:57:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void) {
|
2020-06-21 22:58:46 +02:00
|
|
|
const int postDelay = TELEMETRY_DELAY * 1000;
|
2020-06-03 18:53:33 +02:00
|
|
|
|
2020-06-05 13:24:12 +02:00
|
|
|
unsigned int tempReading = analogRead(A0);
|
|
|
|
unsigned int photocellReading = analogRead(A1);
|
2020-06-02 00:57:20 +02:00
|
|
|
|
|
|
|
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
|
|
|
float tempC = (tempVoltage - 0.5) * 100 ;
|
|
|
|
|
2020-06-03 18:53:33 +02:00
|
|
|
if (NTPValid) {
|
2020-06-03 23:53:25 +02:00
|
|
|
telemetry["clock"] = timeClient.getEpochTime();
|
2020-06-03 18:53:33 +02:00
|
|
|
} else {
|
2020-06-03 23:53:25 +02:00
|
|
|
telemetry["clock"] = NULL; // converted into 0
|
2020-06-03 18:53:33 +02:00
|
|
|
}
|
2020-06-02 00:57:20 +02:00
|
|
|
payload["light"] = photocellReading;
|
|
|
|
|
|
|
|
temp["celsius"] = tempC;
|
|
|
|
temp["raw"] = tempReading;
|
|
|
|
temp["volts"] = tempVoltage;
|
2020-06-03 18:53:33 +02:00
|
|
|
|
2020-06-08 23:17:16 +02:00
|
|
|
#if USE_MQTT
|
|
|
|
publishData(config, telemetry);
|
|
|
|
#else
|
2020-06-03 23:53:25 +02:00
|
|
|
postData(config, telemetryURL, telemetry);
|
2020-06-08 23:17:16 +02:00
|
|
|
#endif
|
2020-06-03 23:53:25 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:17:16 +02:00
|
|
|
#if USE_MQTT
|
|
|
|
void publishData(const netConfig &mqtt, const DynamicJsonDocument &json) {
|
2020-06-16 13:12:09 +02:00
|
|
|
if (clientMQTT.connect(serial)) {
|
2020-06-08 23:17:16 +02:00
|
|
|
char buffer[256];
|
|
|
|
serializeJson(json, buffer);
|
|
|
|
clientMQTT.publish(serial, buffer);
|
|
|
|
|
|
|
|
#if DEBUG_TO_SERIAL
|
|
|
|
Serial.println("DEBUG: MQTT PUBLISH>>>");
|
|
|
|
serializeJsonPretty(json, Serial);
|
|
|
|
Serial.println("\n<<<");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-03 23:53:25 +02:00
|
|
|
void postData(const netConfig &postAPI, const String &URL, const DynamicJsonDocument &json) {
|
2021-12-08 22:27:42 +01:00
|
|
|
if (ethClient.connect(postAPI.iot_address, postAPI.iot_port)) {
|
2020-06-16 13:12:09 +02:00
|
|
|
ethClient.print("POST ");
|
|
|
|
ethClient.print(URL);
|
|
|
|
ethClient.println(" HTTP/1.1");
|
|
|
|
ethClient.print("Host: ");
|
2021-12-08 22:27:42 +01:00
|
|
|
ethClient.print(postAPI.iot_address);
|
2020-06-16 13:12:09 +02:00
|
|
|
ethClient.print(":");
|
2021-12-08 22:27:42 +01:00
|
|
|
ethClient.println(postAPI.iot_port);
|
2020-06-16 13:12:09 +02:00
|
|
|
ethClient.println("Content-Type: application/json");
|
|
|
|
ethClient.print("Content-Length: ");
|
2020-06-21 22:58:46 +02:00
|
|
|
ethClient.println(measureJson(json));
|
2020-06-16 13:12:09 +02:00
|
|
|
ethClient.println("Connection: close");
|
|
|
|
ethClient.println();
|
|
|
|
serializeJson(json, ethClient);
|
|
|
|
ethClient.stop();
|
2020-06-03 18:53:33 +02:00
|
|
|
|
|
|
|
#if DEBUG_TO_SERIAL
|
2020-06-08 23:17:16 +02:00
|
|
|
Serial.println("DEBUG: HTTP POST>>>");
|
2020-06-03 18:53:33 +02:00
|
|
|
serializeJsonPretty(json, Serial);
|
|
|
|
Serial.println("\n<<<");
|
|
|
|
#endif
|
|
|
|
}
|
2020-06-02 00:57:20 +02:00
|
|
|
}
|