mirror of
https://github.com/daniviga/bite.git
synced 2024-11-26 23:06:14 +01:00
Enable NTP (#6)
* Add ntpd server to the stack * Enable NTP updates on Arduino * Fix telemetry when clock is 0
This commit is contained in:
parent
5768ed9390
commit
e4a3684951
|
@ -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,32 @@
|
||||||
#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);
|
||||||
|
bool NTPValid = false;
|
||||||
|
|
||||||
struct netConfig {
|
struct netConfig {
|
||||||
IPAddress address;
|
IPAddress address;
|
||||||
int port;
|
unsigned int port;
|
||||||
};
|
};
|
||||||
netConfig config;
|
netConfig config;
|
||||||
|
|
||||||
|
@ -39,15 +48,10 @@ void setup(void) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -56,7 +60,7 @@ void setup(void) {
|
||||||
|
|
||||||
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,18 +68,35 @@ 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();
|
||||||
|
if (timeClient.update()) {
|
||||||
|
NTPValid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#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 ;
|
||||||
|
|
||||||
|
if (NTPValid) {
|
||||||
|
json["clock"] = timeClient.getEpochTime();
|
||||||
|
} else {
|
||||||
|
json["clock"] = NULL; // converted into 0
|
||||||
|
}
|
||||||
payload["light"] = photocellReading;
|
payload["light"] = photocellReading;
|
||||||
|
|
||||||
temp["celsius"] = tempC;
|
temp["celsius"] = tempC;
|
||||||
|
@ -87,20 +108,32 @@ void loop(void) {
|
||||||
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);
|
||||||
|
|
|
@ -11,6 +11,14 @@ x-op-service-default: &service_default
|
||||||
init: true
|
init: true
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
ntpd:
|
||||||
|
<<: *service_default
|
||||||
|
image: daniviga/ntpd
|
||||||
|
networks:
|
||||||
|
- net
|
||||||
|
ports:
|
||||||
|
- "127.0.0.1:123:123/udp"
|
||||||
|
|
||||||
timescale:
|
timescale:
|
||||||
<<: *service_default
|
<<: *service_default
|
||||||
image: timescale/timescaledb:latest-pg12
|
image: timescale/timescaledb:latest-pg12
|
||||||
|
|
8
docker/ntpd/Dockerfile
Normal file
8
docker/ntpd/Dockerfile
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
FROM alpine:3.9
|
||||||
|
|
||||||
|
RUN apk update && apk add chrony && \
|
||||||
|
chown -R chrony:chrony /var/lib/chrony
|
||||||
|
COPY ./chrony.conf /etc/chrony/chrony.conf
|
||||||
|
|
||||||
|
EXPOSE 123/udp
|
||||||
|
ENTRYPOINT ["chronyd", "-d", "-s", "-x"]
|
4
docker/ntpd/chrony.conf
Normal file
4
docker/ntpd/chrony.conf
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
pool pool.ntp.org iburst
|
||||||
|
initstepslew 10 pool.ntp.org
|
||||||
|
driftfile /var/lib/chrony/chrony.drift
|
||||||
|
allow all
|
|
@ -5,6 +5,8 @@ from api.models import Device, WhiteList
|
||||||
@admin.register(Device)
|
@admin.register(Device)
|
||||||
class DeviceAdmin(admin.ModelAdmin):
|
class DeviceAdmin(admin.ModelAdmin):
|
||||||
readonly_fields = ('creation_time', 'updated_time',)
|
readonly_fields = ('creation_time', 'updated_time',)
|
||||||
|
list_filter = ('serial',)
|
||||||
|
search_fields = ('serial',)
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(None, {
|
(None, {
|
||||||
|
@ -20,6 +22,8 @@ class DeviceAdmin(admin.ModelAdmin):
|
||||||
@admin.register(WhiteList)
|
@admin.register(WhiteList)
|
||||||
class WhiteListAdmin(admin.ModelAdmin):
|
class WhiteListAdmin(admin.ModelAdmin):
|
||||||
readonly_fields = ('creation_time', 'updated_time',)
|
readonly_fields = ('creation_time', 'updated_time',)
|
||||||
|
list_filter = ('serial',)
|
||||||
|
search_fields = ('serial',)
|
||||||
|
|
||||||
fieldsets = (
|
fieldsets = (
|
||||||
(None, {
|
(None, {
|
||||||
|
|
|
@ -4,4 +4,7 @@ from telemetry.models import Telemetry
|
||||||
|
|
||||||
@admin.register(Telemetry)
|
@admin.register(Telemetry)
|
||||||
class TelemetryAdmin(admin.ModelAdmin):
|
class TelemetryAdmin(admin.ModelAdmin):
|
||||||
readonly_fields = ('device', 'time', 'payload',)
|
readonly_fields = ('device', 'time', 'clock', 'payload',)
|
||||||
|
list_display = ('__str__', 'device')
|
||||||
|
list_filter = ('time', 'device__serial')
|
||||||
|
search_fields = ('device__serial',)
|
||||||
|
|
19
freedcs/telemetry/migrations/0005_telemetry_clock.py
Normal file
19
freedcs/telemetry/migrations/0005_telemetry_clock.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
# Generated by Django 3.0.6 on 2020-06-03 13:08
|
||||||
|
|
||||||
|
import django.core.validators
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('telemetry', '0004_auto_20200602_2132'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='telemetry',
|
||||||
|
name='clock',
|
||||||
|
field=models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)]),
|
||||||
|
),
|
||||||
|
]
|
17
freedcs/telemetry/migrations/0006_auto_20200603_1317.py
Normal file
17
freedcs/telemetry/migrations/0006_auto_20200603_1317.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
# Generated by Django 3.0.6 on 2020-06-03 13:17
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('telemetry', '0005_telemetry_clock'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name='telemetry',
|
||||||
|
options={'ordering': ['-time', 'device'], 'verbose_name_plural': 'Telemetry'},
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,4 +1,5 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.core.validators import MinValueValidator
|
||||||
from django.contrib.postgres.fields import JSONField
|
from django.contrib.postgres.fields import JSONField
|
||||||
|
|
||||||
from api.models import Device
|
from api.models import Device
|
||||||
|
@ -7,11 +8,14 @@ from api.models import Device
|
||||||
class Telemetry(models.Model):
|
class Telemetry(models.Model):
|
||||||
device = models.ForeignKey(Device, on_delete=models.CASCADE)
|
device = models.ForeignKey(Device, on_delete=models.CASCADE)
|
||||||
time = models.DateTimeField(primary_key=True, auto_now_add=True)
|
time = models.DateTimeField(primary_key=True, auto_now_add=True)
|
||||||
|
clock = models.IntegerField(
|
||||||
|
validators=[MinValueValidator(0)],
|
||||||
|
null=True)
|
||||||
payload = JSONField()
|
payload = JSONField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['time', 'device']
|
ordering = ['-time', 'device']
|
||||||
verbose_name_plural = "Telemetry"
|
verbose_name_plural = "Telemetry"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s - %s" % (self.time, self.device.serial)
|
return str(self.time)
|
||||||
|
|
|
@ -11,7 +11,7 @@ class TelemetrySerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Telemetry
|
model = Telemetry
|
||||||
fields = ('device', 'time', 'payload',)
|
fields = ('time', 'device', 'clock', 'payload',)
|
||||||
|
|
||||||
|
|
||||||
# class WhiteListSerializer(serializers.ModelSerializer):
|
# class WhiteListSerializer(serializers.ModelSerializer):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user