Import initial sketches with sample configuration files
authorEmmanuel Lacour <elacour@easter-eggs.com>
Tue, 29 Dec 2020 09:36:53 +0000 (10:36 +0100)
committerEmmanuel Lacour <elacour@easter-eggs.com>
Tue, 29 Dec 2020 09:36:53 +0000 (10:36 +0100)
cave/cave-config.h.in [new file with mode: 0644]
cave/cave.ino [new file with mode: 0644]
station-meteo/station-meteo-config.h.in [new file with mode: 0644]
station-meteo/station-meteo.ino [new file with mode: 0644]

diff --git a/cave/cave-config.h.in b/cave/cave-config.h.in
new file mode 100644 (file)
index 0000000..4a1402d
--- /dev/null
@@ -0,0 +1,13 @@
+// Enable debug on serial port
+#define DEBUG 1
+// Wifi
+#define WIFI_SSID "XXXXXX"
+#define WIFI_PASS "xxxxxx"
+// InfluxDB
+#define INFLUXDB_IP {w,x,y,z}
+#define INFLUXDB_PORT xxxx
+// Polling interval (ms)
+#define POLL_INT 10000
+// Location informations
+#define CITY "xxxxx"
+#define LOCATION "xxxxx"
diff --git a/cave/cave.ino b/cave/cave.ino
new file mode 100644 (file)
index 0000000..868901f
--- /dev/null
@@ -0,0 +1,68 @@
+#include <ESP8266WiFi.h>
+#include <WiFiUdp.h>
+#include <HCSR04.h>
+
+#include "cave-config.h"
+
+const char*    wifi_ssid = WIFI_SSID;
+const char*    wifi_pass = WIFI_PASS;
+
+// Grafana
+IPAddress      influxdb_ip(INFLUXDB_IP);
+long           influxdb_port = INFLUXDB_PORT;
+unsigned int   localPort = 2390;
+// MilliSeconds
+long unsigned  poll_int = POLL_INT;
+String city = CITY;
+String location = LOCATION;
+WiFiUDP Udp;
+
+// TRIGGER, ECHO
+UltraSonicDistanceSensor distanceSensor(14, 12);
+
+void setup() {
+
+  #if defined(DEBUG)
+  Serial.begin(115200);
+  Serial.print("Connecting to ");
+  Serial.println(wifi_ssid);
+  #endif
+
+  WiFi.mode(WIFI_STA);
+  WiFi.begin(WIFI_SSID, WIFI_PASS);
+
+  while (WiFi.status() != WL_CONNECTED) {
+    delay(500);
+    #if defined(DEBUG)
+    Serial.print(".");
+    #endif
+  }
+
+  #if defined(DEBUG)
+  Serial.println("WiFi connected");
+  Serial.println("IP address: ");
+  Serial.println(WiFi.localIP());
+  #endif
+}
+
+void loop() {
+  // FIXME: get temperature from sensor and use it here
+  float temp = 20.0;
+  double water_dist = distanceSensor.measureDistanceCm(temp);
+  #if defined(DEBUG)
+  Serial.println("Distance: "+String(water_dist));
+  #endif
+  sendToInfluxDB("water_well,city="+city+",location="+location, "distance", String(water_dist));
+  delay(POLL_INT); 
+}
+
+void sendToInfluxDB(String measure, String key, String value) {
+  String line = measure+" "+key+"="+value;
+  int line_length = line.length()+1;
+  char influx_line[line_length];
+  line.toCharArray(influx_line, line_length);
+  Udp.begin(localPort);
+  Udp.beginPacket(influxdb_ip, influxdb_port);
+  Udp.write(influx_line);
+  Udp.endPacket();
+}
diff --git a/station-meteo/station-meteo-config.h.in b/station-meteo/station-meteo-config.h.in
new file mode 100644 (file)
index 0000000..4a1402d
--- /dev/null
@@ -0,0 +1,13 @@
+// Enable debug on serial port
+#define DEBUG 1
+// Wifi
+#define WIFI_SSID "XXXXXX"
+#define WIFI_PASS "xxxxxx"
+// InfluxDB
+#define INFLUXDB_IP {w,x,y,z}
+#define INFLUXDB_PORT xxxx
+// Polling interval (ms)
+#define POLL_INT 10000
+// Location informations
+#define CITY "xxxxx"
+#define LOCATION "xxxxx"
diff --git a/station-meteo/station-meteo.ino b/station-meteo/station-meteo.ino
new file mode 100644 (file)
index 0000000..41236ef
--- /dev/null
@@ -0,0 +1,167 @@
+// D4: temperature sensor
+// D2: rain sensor
+
+// Include the libraries we need
+
+#include <SPI.h>
+#include <WiFiNINA.h>
+#include <WiFiUdp.h>
+#include <OneWire.h>
+#include <DallasTemperature.h>
+#include "station-meteo-config.h"
+
+// Wifi configuration
+char wifi_ssid[] = WIFI_SSID; //  your network SSID (name)
+char wifi_pass[] = WIFI_PASS;    // your network password (use for WPA, or use as key for WEP)
+int wifi_status = WL_IDLE_STATUS;     // the Wifi radio's status
+
+// Influxdb configuration
+byte influxdb_host[] = INFLUXDB_IP;
+int influxdb_port = INFLUXDB_PORT;
+unsigned int udp_localport = 2390;
+String city = CITY;
+String location = LOCATION;
+
+// Temperature sensor
+#define ONE_WIRE_BUS 4
+
+// Rain fall sensor
+int rainfall_sensor_pin = 2;
+float rain_incr = 0.2794; // this is mm/m2 for each sensor tick 
+int rainfall_ticks = 0;
+
+// Timing
+unsigned long previousMillis=   0;
+unsigned long previousMillis2=  0;
+
+// Initiate udp client
+WiFiUDP udp;
+
+// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
+OneWire oneWire(ONE_WIRE_BUS);
+
+// Pass our oneWire reference to Dallas Temperature. 
+DallasTemperature sensors(&oneWire);
+
+/*
+ * The setup function. We only start the sensors here
+ */
+void setup(void)
+{
+  // start serial port
+  Serial.begin(9600);
+
+  // Start up the onewire library for temp sensor
+  sensors.begin();
+  
+  pinMode(rainfall_sensor_pin, INPUT);
+  attachInterrupt(digitalPinToInterrupt(rainfall_sensor_pin), getRain, RISING);
+
+  // check for the WiFi module:
+  if (WiFi.status() == WL_NO_MODULE) {
+    Serial.println("Communication with WiFi module failed!");
+    // don't continue
+    while (true);
+  }
+
+  String fv = WiFi.firmwareVersion();
+  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
+    Serial.println("Please upgrade the firmware");
+  }
+
+  // attempt to connect to Wifi network:
+  while (wifi_status != WL_CONNECTED) {
+    Serial.print("Attempting to connect to SSID: ");
+    Serial.println(wifi_ssid);
+    // Connect to WPA/WPA2 network
+    wifi_status = WiFi.begin(wifi_ssid, wifi_pass);
+
+    // wait 10 seconds for connection:
+    delay(10000);
+  }
+
+  Serial.println("Connected to wifi");
+  printWifiStatus();
+
+  udp.begin(udp_localport);
+}
+
+/*
+ * Main function, get and show the temperature
+ */
+void loop(void)
+{ 
+
+  String temp_influx_line;
+  String rain_influx_line;
+  float rainfall_1min(0);
+  float tempC(0);
+  
+  // Get rainfall height
+  rainfall_1min = rainfall_ticks * rain_incr;
+  rainfall_ticks = 0;
+  Serial.print("Rain mm/m2: ");
+  Serial.println(rainfall_1min);
+  Serial.println("Sending rain height to influxdb...");
+  rain_influx_line = String("rain,city="+city+"location="+location+" value=" + String(rainfall_1min, 2));
+  udp.beginPacket(influxdb_host, influxdb_port);
+  udp.print(rain_influx_line);
+  udp.endPacket();
+
+  // Temperature
+  tempC = get_temperature();
+  // Check if reading was successful
+  if(tempC != DEVICE_DISCONNECTED_C) 
+  {
+    Serial.print("Temperature for the device 1 (index 0) is: ");
+    Serial.println(tempC);
+    temp_influx_line = String("temperature,city="+city+",location="+location+" value=" + String(tempC, 2));
+    // send the packet
+    Serial.println("Sending UDP packet...");
+    udp.beginPacket(influxdb_host, influxdb_port);
+    udp.print(temp_influx_line);
+    udp.endPacket();
+  } 
+  else
+  {
+    Serial.println("Error: Could not read temperature data");
+  }
+  delay(POLL_INT);
+}
+
+
+float get_temperature() {
+  float tempC(0);
+  // call sensors.requestTemperatures() to issue a global temperature 
+  // request to all devices on the bus
+  Serial.print("Requesting temperatures...");
+  sensors.requestTemperatures(); // Send the command to get temperatures
+  Serial.println("DONE");
+  // After we got the temperatures, we can print them here.
+  // We use the function ByIndex, we have only one sensor.
+  tempC = sensors.getTempCByIndex(0);
+  return tempC;
+}
+
+
+void printWifiStatus() {
+  // print the SSID of the network you're attached to:
+  Serial.print("SSID: ");
+  Serial.println(WiFi.SSID());
+
+  // print your board's IP address:
+  IPAddress ip = WiFi.localIP();
+  Serial.print("IP Address: ");
+  Serial.println(ip);
+
+  // print the received signal strength:
+  long rssi = WiFi.RSSI();
+  Serial.print("signal strength (RSSI):");
+  Serial.print(rssi);
+  Serial.println(" dBm");
+}
+
+void getRain ()
+{
+  rainfall_ticks ++ ; 
+}