--- /dev/null
+#!/bin/bash
+# This script reads MQTT messages and writes to the appropriate file for logging.
+# I use the "supervise" tool from D.J.Bernstein's daemontools to start and monitor this script.
+
+function write_to_file {
+ if test -d "$1"; then
+ echo "$3">>"$2"
+ else
+ mkdir -p "$1"
+ echo "$3">>"$2"
+ fi
+}
+
+while read -r st_in
+do
+ # st_in is like "trend/air 2022-10-26T15:33+02:00 0.074259 ..."
+ main_topic="$(echo "$st_in"| awk 'BEGIN { FS = "/" } ; { print $1 }')"
+ full_topic="$(echo "$st_in"| awk '{ print $1 }')"
+ file_base="$(echo "$st_in"| awk '{ print $1 }')"
+ if [ "$main_topic" == "trend" ]; then
+ date="$(echo "$st_in"| awk '{ print $2 }')"
+ date_base="$(echo "${date:0:10}" | tr -t "-" "_")" # e.g. 2022_10_26
+ year="$(echo "${date:0:4}")" # e.g. 2022
+ content="$(echo "$st_in"| cut --complement -f 1)"
+ output_dir="/home/pi/$file_base/$year"
+ output_file="/home/pi/$file_base/$year/$date_base.txt"
+ write_to_file "$output_dir" "$output_file" "$content"
+ #printf "writing %s to file %s \n" "$content" "$output_file"
+ elif [ "$main_topic" == "status" ]; then
+ # ...
+ fi
+done < <(mosquitto_sub -h 192.168.1.121 -t trend/+ -t status/+ -F %t"\t"%p)
--- /dev/null
+// This program reads the analog pin A0 of an Arduino Nano 33 IoT.
+// A0 is connected to the ouptut of a voltage integrator
+// RESET_PIN is used to reset the integrator
+// The analog value is sent using a UDP packet to a linux box.
+// Values can be read using the following command:
+// netcat -u -l -p NNNN
+// where NNNN is the number of the port used
+
+#define ADC_RESOLUTION_BITS 12 // ADC resolution in bit (10 or 12)
+#define RESET_PIN 2 // digital pin used to reset the integrator
+#define INTEGRATION_TIME 30000 // integration time in milliseconds. default:30000
+#define SAMPLES 90 // number of analog measurements to average
+
+int const scale = pow(2, ADC_RESOLUTION_BITS) - 1;
+
+#include <SPI.h>
+#include <WiFiNINA.h>
+#include <WiFiUdp.h>
+#include "arduino_secrets.h"
+
+int status = WL_IDLE_STATUS;
+char ssid[] = SECRET_SSID; // network SSID
+char pass[] = SECRET_PASS; // network password
+
+// IP configuration
+IPAddress myip(192, 168, 1, 115);
+IPAddress gateway(192,168,1,1);
+IPAddress subnet(255,255,255,0);
+IPAddress server_ip(192, 168, 1, 121);
+unsigned int server_port = 2390;
+WiFiSSLClient client;
+
+char packetBuffer[255]; // buffer to hold incoming packet
+char Measure_CA[12];
+String Measure;
+double ADC_measure;
+
+WiFiUDP Udp;
+
+void setup() {
+
+ analogReadResolution(ADC_RESOLUTION_BITS);
+ pinMode(RESET_PIN, OUTPUT);
+
+ // check for the WiFi module:
+ if (WiFi.status() == WL_NO_MODULE) {
+ while (true);
+ }
+
+ // attempt to connect to Wifi network:
+ WiFi.config(myip, dns, gateway, subnet);
+ while (status != WL_CONNECTED) {
+ status = WiFi.begin(ssid, pass);
+ delay(10000);
+ }
+
+ Udp.begin(server_port);
+
+}
+
+
+void loop() {
+
+ resetIntegrator();
+
+ if (WiFi.status() != WL_CONNECTED){ // reconnect if connection is lost
+ WiFi.end();
+ do {
+ status = WiFi.begin(ssid, pass);
+ delay(2000);
+ } while (status != WL_CONNECTED);
+ Udp.begin(server_port);
+ }
+
+ Udp.beginPacket(server_ip, server_port);
+ ADC_measure = Integrate(INTEGRATION_TIME);
+ Measure = String(ADC_measure, 6);
+ Measure.toCharArray(Measure_CA, 12);
+ Udp.write(Measure_CA);
+ Udp.write("\n");
+ Udp.endPacket();
+
+}
+
+
+void resetIntegrator() {
+ // reset the integrator, 0 output closes the JFET switch
+ digitalWrite(RESET_PIN, LOW);
+ delay(50);
+ digitalWrite(RESET_PIN, HIGH);
+}
+
+
+double Integrate(int int_time) {
+ // integrate for int_time milliseconds
+ int i;
+ double sum = 0.0;
+ double out;
+ delay(int_time);
+
+ for (i=0; i < SAMPLES; i++){
+ sum += analogRead(A0);
+ }
+
+ out = 3.3 * (sum / SAMPLES) / scale;
+ return out;
+}