]> git.devmsg.it Git - giulio/.git/commitdiff
Current integrator + MQTT examples
authorGiulio Maestrini <giuliomaestrini@posteo.net>
Thu, 3 Nov 2022 16:15:30 +0000 (17:15 +0100)
committerGiulio Maestrini <giuliomaestrini@posteo.net>
Thu, 3 Nov 2022 16:15:30 +0000 (17:15 +0100)
MQTT/log_MQTT.sh [new file with mode: 0644]
MQTT/publish_MQTT.sh [new file with mode: 0644]
arduino/integra.sh [new file with mode: 0644]
arduino/integrator/arduino_secrets.h [new file with mode: 0644]
arduino/integrator/integrator.ino [new file with mode: 0644]

diff --git a/MQTT/log_MQTT.sh b/MQTT/log_MQTT.sh
new file mode 100644 (file)
index 0000000..e46fa63
--- /dev/null
@@ -0,0 +1,32 @@
+#!/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)
diff --git a/MQTT/publish_MQTT.sh b/MQTT/publish_MQTT.sh
new file mode 100644 (file)
index 0000000..2712163
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+# Publish a file line-by-line via MQTT. Lines are deleted in place after they are sucessfully published
+# fin is the input file
+fin="/home/pi/monitoring/TEST.txt"
+flocal="/home/pi/monitoring/TEST_n.txt"
+broker_ip="192.168.1.121"
+
+# create a working copy of the input file
+if ! mv $fin $flocal; then
+       printf "Move error.\n"
+       exit 1
+fi
+
+while read -r i
+do
+       if ! mosquitto_pub -h "$broker_ip" -q 2 -t trend/air -m "${i}"; then
+               printf "MQTT error\n"
+               # append unpublished lines to the original input file
+               echo "$i" >> $fin
+       fi
+done < $flocal
diff --git a/arduino/integra.sh b/arduino/integra.sh
new file mode 100644 (file)
index 0000000..24186e4
--- /dev/null
@@ -0,0 +1,10 @@
+#!/bin/bash
+# legge i valori dall'integratore di corrente
+
+for((;;))
+do
+       out_file=$(date '+/home/pi/trend/corrente/%Y/%Y_%m_%d.txt')
+       ncat -u -l -p 2390 -c "date -I'seconds'" -o /home/pi/trend/corrente/temp.txt
+       # arrange values in a row
+       paste -sd "\t\n" "/home/pi/trend/corrente/temp.txt" >>"$out_file"
+done
diff --git a/arduino/integrator/arduino_secrets.h b/arduino/integrator/arduino_secrets.h
new file mode 100644 (file)
index 0000000..12aae39
--- /dev/null
@@ -0,0 +1,2 @@
+#define SECRET_SSID "XXX"
+#define SECRET_PASS "YYY"
diff --git a/arduino/integrator/integrator.ino b/arduino/integrator/integrator.ino
new file mode 100644 (file)
index 0000000..deec0ec
--- /dev/null
@@ -0,0 +1,107 @@
+// 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;
+}