mirror of
https://github.com/daniviga/bite.git
synced 2025-04-20 14:31:20 +02:00
Enable NTP updates on Arduino
This commit is contained in:
parent
6dda52f384
commit
0a27d58b2f
@ -9,7 +9,7 @@ const char serial[] = "abcd1234";
|
|||||||
|
|
||||||
struct netConfig {
|
struct netConfig {
|
||||||
IPAddress address;
|
IPAddress address;
|
||||||
int port;
|
unsigned int port;
|
||||||
};
|
};
|
||||||
|
|
||||||
netConfig config = {
|
netConfig config = {
|
||||||
|
@ -1,23 +1,31 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
|
#include <EthernetUdp.h>
|
||||||
|
#include <NTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
#define DEBUG_TO_SERIAL 1
|
#define DEBUG_TO_SERIAL 1
|
||||||
|
#define USE_INTERNAL_NTP 0 // use default ntp server or the internal one
|
||||||
#define AREF_VOLTAGE 3.3
|
#define AREF_VOLTAGE 3.3
|
||||||
|
|
||||||
const String serverName = "sensor.server.domain";
|
// const String serverName = "sensor.server.domain";
|
||||||
|
|
||||||
const size_t capacity = JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(6);
|
const size_t capacity = JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(3) + 110;
|
||||||
DynamicJsonDocument doc(capacity);
|
|
||||||
JsonObject payload = doc.createNestedObject("payload");
|
DynamicJsonDocument json(capacity);
|
||||||
|
JsonObject payload = json.createNestedObject("payload");
|
||||||
JsonObject temp = payload.createNestedObject("temperature");
|
JsonObject temp = payload.createNestedObject("temperature");
|
||||||
|
|
||||||
|
unsigned int counter = 0;
|
||||||
int tempPin = A0;
|
int tempPin = A0;
|
||||||
int photocellPin = A1;
|
int photocellPin = A1;
|
||||||
|
|
||||||
|
EthernetUDP ntpUDP;
|
||||||
|
NTPClient timeClient(ntpUDP);
|
||||||
|
|
||||||
struct netConfig {
|
struct netConfig {
|
||||||
IPAddress address;
|
IPAddress address;
|
||||||
int port;
|
unsigned int port;
|
||||||
};
|
};
|
||||||
netConfig config;
|
netConfig config;
|
||||||
|
|
||||||
@ -26,37 +34,32 @@ const int postDelay = 10 * 1000;
|
|||||||
|
|
||||||
void setup(void) {
|
void setup(void) {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
analogReference(EXTERNAL);
|
analogReference(EXTERNAL);
|
||||||
|
|
||||||
byte mac[6];
|
byte mac[6];
|
||||||
char serial[9];
|
char serial[9];
|
||||||
|
|
||||||
int eeAddress = 0;
|
int eeAddress = 0;
|
||||||
|
|
||||||
EEPROM.get(eeAddress, mac);
|
EEPROM.get(eeAddress, mac);
|
||||||
eeAddress += sizeof(mac);
|
eeAddress += sizeof(mac);
|
||||||
EEPROM.get(eeAddress, serial);
|
EEPROM.get(eeAddress, serial);
|
||||||
eeAddress += sizeof(serial);
|
eeAddress += sizeof(serial);
|
||||||
|
|
||||||
Serial.println("Initialize Ethernet with DHCP:");
|
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
|
||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
Serial.println("ERROR: ethernet shield was not found.");
|
||||||
} else if (Ethernet.linkStatus() == LinkOFF) {
|
|
||||||
Serial.println("Ethernet cable is not connected.");
|
|
||||||
}
|
}
|
||||||
// no point in carrying on, so do nothing forevermore:
|
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(1);
|
delay(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EEPROM.get(eeAddress, config);
|
EEPROM.get(eeAddress, config);
|
||||||
|
|
||||||
Serial.print("IoT #");
|
Serial.print("IoT #");
|
||||||
Serial.print(serial);
|
Serial.print(serial);
|
||||||
Serial.println(" started at address:");
|
Serial.println(" at address:");
|
||||||
Serial.println(Ethernet.localIP());
|
Serial.println(Ethernet.localIP());
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("Connecting to:");
|
Serial.println("Connecting to:");
|
||||||
@ -64,43 +67,66 @@ void setup(void) {
|
|||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(config.port);
|
Serial.println(config.port);
|
||||||
|
|
||||||
doc["device"] = serial; // FIXME
|
#if USE_INTERNAL_NTP
|
||||||
payload["id"] = serverName;
|
timeClient.setPoolServerIP(config.address);
|
||||||
|
#endif
|
||||||
|
timeClient.begin();
|
||||||
|
timeClient.update();
|
||||||
|
|
||||||
|
#if DEBUG_TO_SERIAL
|
||||||
|
Serial.println("DEBUG: clock updated via NTP.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
json["device"] = 1; // FIXME
|
||||||
|
// payload["id"] = serverName;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
|
|
||||||
int photocellReading = analogRead(photocellPin);
|
unsigned int photocellReading = analogRead(photocellPin);
|
||||||
int tempReading = analogRead(tempPin);
|
unsigned int tempReading = analogRead(tempPin);
|
||||||
|
|
||||||
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
float tempVoltage = tempReading * AREF_VOLTAGE / 1024.0;
|
||||||
float tempC = (tempVoltage - 0.5) * 100 ;
|
float tempC = (tempVoltage - 0.5) * 100 ;
|
||||||
|
|
||||||
|
json["time"] = timeClient.getEpochTime();
|
||||||
payload["light"] = photocellReading;
|
payload["light"] = photocellReading;
|
||||||
|
|
||||||
temp["celsius"] = tempC;
|
temp["celsius"] = tempC;
|
||||||
temp["raw"] = tempReading;
|
temp["raw"] = tempReading;
|
||||||
temp["volts"] = tempVoltage;
|
temp["volts"] = tempVoltage;
|
||||||
|
|
||||||
if (EthernetClient client = client.connect(config.address, config.port)) {
|
if (EthernetClient client = client.connect(config.address, config.port)) {
|
||||||
client.print("POST ");
|
client.print("POST ");
|
||||||
client.print(URL);
|
client.print(URL);
|
||||||
client.println(" HTTP/1.1");
|
client.println(" HTTP/1.1");
|
||||||
client.print("Host: ");
|
client.print("Host: ");
|
||||||
printAddr(config.address, &client);
|
client.print(config.address);
|
||||||
client.print(":");
|
client.print(":");
|
||||||
client.println(config.port);
|
client.println(config.port);
|
||||||
client.println("Content-Type: application/json");
|
client.println("Content-Type: application/json");
|
||||||
client.print("Content-Length: ");
|
client.print("Content-Length: ");
|
||||||
client.println(measureJsonPretty(doc));
|
client.println(measureJsonPretty(json));
|
||||||
client.println("Connection: close");
|
client.println("Connection: close");
|
||||||
client.println();
|
client.println();
|
||||||
serializeJson(doc, client);
|
serializeJson(json, client);
|
||||||
client.stop();
|
client.stop();
|
||||||
|
|
||||||
#if DEBUG_TO_SERIAL
|
#if DEBUG_TO_SERIAL
|
||||||
serializeJsonPretty(doc, Serial);
|
Serial.println("DEBUG: >>>");
|
||||||
#endif
|
serializeJsonPretty(json, Serial);
|
||||||
|
Serial.println("\n<<<");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
delay(postDelay);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
pool pool.ntp.org iburst
|
pool pool.ntp.org iburst
|
||||||
initstepslew 10 pool.ntp.org
|
initstepslew 10 pool.ntp.org
|
||||||
driftfile /var/lib/chrony/chrony.drift
|
driftfile /var/lib/chrony/chrony.drift
|
||||||
|
allow all
|
||||||
|
Loading…
x
Reference in New Issue
Block a user