Installing InfluxDB Ver 2 on RPi4

So my image running InfuxDB became corrupted and I was unable to recover so I decided to take the jump to InfluxDB 2.x.

The days of the TICK stack are gone and all contained now in a single Influx install.

Start with a fresh install of 64-Bit Ubuntu Server. (tutorial here)

Note: With Influx version 2.x there is a new protocol for writing data and essentially an upgrade to 2.x will mean all devices writing to Influx will need their code updated also. . . . . fun. Also Grafana should be updated to version 7.x+ if you also run this side by side, even more fun.

sudo apt-get update
sudo apt-get upgrade -y

wget -qO- https://repos.influxdata.com/influxdb.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/influxdb.gpg > /dev/null

export DISTRIB_ID=$(lsb_release -si); export DISTRIB_CODENAME=$(lsb_release -sc)

echo "deb [signed-by=/etc/apt/trusted.gpg.d/influxdb.gpg] https://repos.influxdata.com/${DISTRIB_ID,,} ${DISTRIB_CODENAME} stable" | sudo tee /etc/apt/sources.list.d/influxdb.list > /dev/null

sudo apt-get update && sudo apt-get install influxdb2

sudo apt-get install fail2ban -y; sudo apt-get install ntp -y; sudo apt-get install ntpstat -y

#Did not use hashed out commands yet, was required on raspbian but not sure about ubuntu
#systemctl stop systemd-timesyncd
#systemctl disable systemd-timesyncd
#/etc/init.d/ntp stop
#/etc/init.d/ntp start

sudo reboot

Confirm everything is working:

sudo service influxdb status
ntpstat

You can also head to the InfluxDB configuration page on: http://192.168.1.xxx:8086

Further work if upgrading from InfluxDB 1.x to 2.x

  • Update devices running Shell scrips / .sh code
  • Update devices running Python .py code
  • Update Arduino/ESP devices running .ino code

That’s it!

Resources I used:

  • https://ubuntu.com/tutorials/how-to-install-ubuntu-on-your-raspberry-pi#1-overview
  • https://docs.influxdata.com/influxdb/v2.1/install/?t=Linux

Using DS18B20 on RPi (python) w/bonus writing to InfluxDB

This is how I used 3 x DS18B20 digital temperature sensors wired to a RPi. I bought the versions sealed in a casing as plan to put them outdoors. All data handling done in python and being written to InfluxDB and finally displayed on Grafana.

If you want to first familiarise yourself with python and InfuxDB see an earlier post.

Connect the sensors to the RPi as shown below:

Image Source: Scott Campbell https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/

Sensors can be connected in parallel and no extra resistors required. I soldered onto the ribbon cable and used servo connectors to connect each sensor, this would make it easier to pass through glands later on. See my final setup below:

Anyway starting from a fresh install setup the RPi:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-influxdb

Enable the One-Wire interface for the DS18B20 by opening the below file:

sudo nano /boot/config.txt

And add the below to the bottom of the file:

dtoverlay=w1-gpio

Next exit (Ctrl + x) & reboot:

sudo reboot

Next lets see if device detected:

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls

In my case: (when I only connected 1 sensor, the others showed up when connect them)

28-3c01d60708e8 w1_bus_master1

is displayed. Now enter: (change the X’s to your own address or hit tab to auto-fill)

cd 28-XXXXXXXXXXXX
cat w1_slave

The raw temperature reading output by the sensor will be show as below:

4f 01 55 05 7f a5 81 66 3b : crc=3b YES
4f 01 55 05 7f a5 81 66 3b t=20937

Here the temperature reading is t=20937, which means a temperature of 20.937 degrees Celsius.

Great so we are reading a single sensor fine, lets create a python file to do all the above for us:

cd /home/pi
nano temp.py

Fill the file with the below: Remember to update the below:
– The IP of your InfluxDB instance along with database details.
– The address of your DS18B20 sensors

import os
import glob
import time

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

from influxdb import InfluxDBClient
client = InfluxDBClient(host='192.168.1.XXX', port=8086)
#client.get_list_database()
client.switch_database('YOUR_DATABASE')

sensor_1 = '/sys/bus/w1/devices/28-3c01d60708e8/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-3c01d60711da/w1_slave'
sensor_3 = '/sys/bus/w1/devices/28-3c01d6072d92/w1_slave'

sensor_1_t = 0
sensor_2_t = 0
sensor_3_t = 0

def read_temp(sensor):
    f = open(sensor, 'r')
    lines = f.readlines()
    f.close()

    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c

sensor_1_t = read_temp(sensor_1)
sensor_2_t = read_temp(sensor_2)
sensor_3_t = read_temp(sensor_3)

print("Sensor 1 (Inside Shed): " + str(sensor_1_t))
print("Sensor 2 (Outside): " + str(sensor_2_t))
print("Sensor 3 (Soil): " + str(sensor_3_t))

json_body = [
    {
        "measurement": "YOUR_MEASUREMENT",
        "tags": {
            "Device": "YOUR_DEVICE",
            "ID": "YOUR_ID"
        },
        "fields": {
            "i_temp": sensor_1_t,
            "o_temp": sensor_2_t,
            "s_temp": sensor_3_t
        }
    }
]
client.write_points(json_body)

Exit the nano editor while saving using Ctrl + x and hitting Y to save. Make the file executable:

chmod +x temp.py

Run the python file:

python temp.py

Data will be written to your database along with the text outputted to the console. Okay now lets get it to run/log every 15mins by:

crontab -e
*/15 * * * * /usr/bin/python /home/pi/temp.py

Ctrl + x to exit

I then setup a Grafana display to show the sensors (all in the same location for now)

Settings used for above display.

That’s it!

Resources I used:
https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/

Python (RPi) logging to InfluxDB

Simple way to test that you can write to InfluxDB on a RaspberryPi using python. I decided to document as it’s a prerequisite for a later project.

Starting from a fresh install setup the RPi:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-influxdb

It is assumed you have a database ready to go but if not, log into the Device/RPi hosting InfluxDB and create as per below:

influx
create database YOUR_DATABASE
exit

Anyway back to our other device, create a python file:

cd /home/pi
nano test.py

Fill the file with the below:

from influxdb import InfluxDBClient
client = InfluxDBClient(host='192.168.1.XXX', port=8086)
#client.get_list_database() # Use this line if you want to read databases available
client.switch_database('YOUR_DATABASE')

json_body = [
    {
        "measurement": "YOUR_MEASUREMENT_TABLE",
        "tags": {          #Example Tags
            "Device": "Test_Device",
            "ID": "Test_Temperature"
        },
        "fields": {        #Example Fields
            "i_temp": 19,
            "o_temp": 20,
            "s_temp": 21
        }
    }
]

client.write_points(json_body)

Exit the nano editor while saving using Ctrl + x and hitting Y to save. Make the file executable:

chmod +x test.py

Run the python file:

python test.py

Data will be written to your database, if required you can check this on your device hosting InfluxDB by:

influx
use YOUR_DATABASE
select * from YOUR_MEASUREMENT_TABLE

That’s it!

Resources I used:
https://www.circuitbasics.com/raspberry-pi-ds18b20-temperature-sensor-tutorial/

IOT Geiger Counter (InfluxDB)

Of course every household needs a Geiger Counter and I bought this kit to do all the fancy 400/500V voltage work along with an SBM-20 Geiger-Muller Tube on eBay for the actual radiation detecting. Typically it seems people hook this up to a tablet etc. and run an app but my plan was to log to InfluxDB. It can also operate stand-alone which is why I added a handy display (Nokia 5110).

Finished IOT Geiger Counter

Notes:
This type of detector is designed to detect Beta & Gamma rays. (it cannot detect Alpha rays but this sensor can be added easily if wanted.

What it does:
– Listens & counts pulses for 60 seconds
– After 60 seconds writes this value to InfluxDB
– Updates display with current metrics. (last 60 second reading, average reading, max reading, estimated dosage & the current IP address)

Connecting it up:

LCD PinESP8266 Labelled PinESP8266 GPIO PinGeiger Detector Board
1 – RSTD0GPIO 16
2 – CED1GPIO 5
3 – DCD2GPIO 4
4 – DIND3GPIO 0
5 – CLKD4GPIO 2
6 – Vcc3.3V3.3V
7 – Backlight3.3V (on)3.3V
8 – GroundGroundGroundGround
D5GPIO 14Int (interrupt)
VU/ 5VVU / 5V5V

Code:
See latest code at my GitHub (or below):
– Requires Adafruit libraries. Link 1, Link 2
– Requires Running Average Library
– Requires InfluxDb library

#include <ESP8266WiFi.h>
#include <InfluxDb.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "RunningAverage.h"

#define INFLUXDB_HOST "192.168.1.XXX"
#define WIFI_SSID "XXXXXXXXXXXX"
#define WIFI_PASS "XXXXXXXXXXXX"
#define DATABASE "XXXXXXXXXXXX"
#define MEASUREMENT "XXXXXXXXXXXX"
#define DEVICE "XXXXXXXXXXXX"
#define ID "Geiger_Counter"
#define LOG_PERIOD 60000

Influxdb influx(INFLUXDB_HOST);
Adafruit_PCD8544 display = Adafruit_PCD8544(2, 0, 4, 5, 16); //LCD 1.5 Inch (Nokia 5110)84×48 (x,y) pixels
RunningAverage raMinute(60);

//################
int debug = 1; //#
//################

int loopCount = 0;
int cpm = 0;
int cpm_max = 0;
int cpm_1hr_avg = 0;
int cpm_ravg = 0;
int counts = 0;
int cal_factor = 1;
int wifiStatus;

unsigned long currentMillis;
unsigned long previousMillis; //variable for time measurement

void setup(){                                               
  Serial.begin(9600);      // start serial monitor
  delay(1000);
  Serial.println("");
  Serial.println("");
  Serial.println("Setup Routine of ESP8266 Geiger Counter");

  display.begin();
  display.setContrast(55);
  display.display();        // show adafruid splashscreen
  delay(2000);
  display.clearDisplay();   // clears the screen and buffer

  pinMode(LED_BUILTIN, OUTPUT); //D4
  digitalWrite(LED_BUILTIN, HIGH); //Turns it off

  pinMode(14, INPUT_PULLUP);                  // set pin INT0 input for capturing GM Tube events / GPIO5 = D1
  attachInterrupt(14, tube_pulse, FALLING); //defines interrupts

  raMinute.clear();

  influx.setDb(DATABASE);

  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0); display.setTextSize(1);display.print("Up Hrs: ");display.setCursor(48,0);display.print("0");
  display.setCursor(0,9); display.setTextSize(1);display.print("CPMi 1m:");
  display.setCursor(0,17);display.setTextSize(1);display.print("CPM avg:");
  display.setCursor(0,25);display.setTextSize(1);display.print("CPM Max:");
  display.setCursor(0,33);display.setTextSize(1);display.print("uSv/hr: ");
  display.setCursor(0,41);display.setTextSize(1);display.print("Not Connected");
  display.display();

    wifiStatus = WiFi.status();
    if (wifiStatus != WL_CONNECTED) {   
      new_connection();
    }
    else {
        display.fillRect(0,41,84,48, WHITE);
        display.setCursor(0,41);
        display.print(WiFi.localIP());
        display.display();
    }
    

  if (debug == 1) {Serial.println("Setup Complete.");}
}

void loop(){                                              
  currentMillis = millis();
  if(currentMillis - previousMillis > LOG_PERIOD){
    cpm = counts * cal_factor;                        

    raMinute.addValue(cpm);
    cpm_ravg = raMinute.getAverage();

    if (cpm > cpm_max){
      cpm_max = cpm;
    }
    
    Serial.print("CPM: ");                         
    Serial.println(cpm);                          

    display.fillRect(48,0,40,40, WHITE);
    display.display();
    display.setCursor(48,0);display.print(loopCount*.0166);
    display.setCursor(48,9);display.print(cpm);
    display.setCursor(48,17);display.print(cpm_ravg);
    display.setCursor(48,25);display.print(cpm_max);
    display.setCursor(48,33);display.print(cpm_ravg*0.0057);
    display.display();

    Serial.println("Attempting to write to DB");   
    counts = 0;

    InfluxData row(MEASUREMENT);
    row.addTag("Device", DEVICE);
    row.addTag("ID", ID);
    row.addValue("CPM", cpm);  
    row.addValue("LoopCount", loopCount);
    row.addValue("RandomValue", random(0, 100));
  
    wifiStatus = WiFi.status();
    while ( wifiStatus != WL_CONNECTED )
        {
          new_connection();
        }
  
    influx.write(row);
    if (debug == 1) {Serial.println("Wrote Data.");}
  
    //WiFi.mode(WIFI_OFF); // Probably turn off Wifi if want to save battery
    //WiFi.forceSleepBegin();
    //delay( 1 );
  
    status_blink();
    previousMillis = currentMillis;
    loopCount++;
  }
}

ICACHE_RAM_ATTR        //Needed to fix ISR not in IRAM boot error
void tube_pulse(){     //procedure for capturing events from interrupt
  counts++;
}

void new_connection() {
  
    wifiStatus = WiFi.status();
    
    if (wifiStatus != WL_CONNECTED) {   
       
        WiFi.mode(WIFI_STA);
        WiFi.begin(WIFI_SSID, WIFI_PASS);
        int loops = 0;
        int retries = 0;
        display.fillRect(0,41,84,48, WHITE);
        display.setCursor(0,41);
        display.print("Not Connected");
        display.display();
       
        while (wifiStatus != WL_CONNECTED)
        {
          retries++;
          if( retries == 300 )
          {
              if (debug == 1) {Serial.println( "No connection after 300 steps, powercycling the WiFi radio. I have seen this work when the connection is unstable" );}
              WiFi.disconnect();
              delay( 10 );
              WiFi.forceSleepBegin();
              delay( 10 );
              WiFi.forceSleepWake();
              delay( 10 );
              WiFi.begin( WIFI_SSID, WIFI_PASS );
          }
          if ( retries == 600 )
          {
              if (debug == 1) {Serial.println( "No connection after 600 steps. WiFi connection failed, disabled WiFi and waiting for a minute" );}
              WiFi.disconnect( true );
              delay( 1 );
              WiFi.mode( WIFI_OFF );
              WiFi.forceSleepBegin();
              delay( 10 );
              retries = 0;
              
              if( loops == 3 )
              {
                  if (debug == 1) {Serial.println( "That was 3 loops, still no connection so let's go to deep sleep for 2 minutes" );}
                  Serial.flush();
                  ESP.deepSleep( 120000000, WAKE_RF_DISABLED );
              }     
          }
          delay(50);
          wifiStatus = WiFi.status();
        }
        
        wifiStatus = WiFi.status();
        Serial.print("WiFi connected, IP address: ");Serial.println(WiFi.localIP());
        display.fillRect(0,41,84,48, WHITE);
        display.setCursor(0,41);
        display.print(WiFi.localIP());
        display.display();
    }
}

void status_blink() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level   
  delay(100);
  digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
  delay(100);
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level   
  delay(100);
  digitalWrite(LED_BUILTIN, HIGH);   // Turn the LED on (Note that LOW is the voltage level
}

ToDo:
– Comment code
– Add in check at start of code to see if tube functioning.
– I would like to update the running average to 60min average but not enough time to currently do these 10 lines of code.
– Add in control (on/off ) for the LCD backlight, buzzer & Wifi for battery consumption.
– Perhaps would be nice to log to SD card also, not sure if I still have enough I/O for that.
– The ‘case’ is a very rough and not worthy of sharing, a nicer more bespoke would be ideal.
– Add radiation symbol on splash screen.

Resources I used:
https://mightyohm.com/blog/2014/11/a-spotters-guide-to-the-sbm-20-geiger-counter-tube/

That’s it!

ESP8266 & Stepper Motors

Nothing outrageously complicated but when I used the default arduino stepper library to control my 28BYJ-48 stepper motors on my ESP8266 they only turned one direction. With modifications to the library it was possible to get one stepper motor working correctly but the ESP crashed if I tried to control 2 stepper motors, solution sketch below:

What did work:
– Sketch below. (Note: No fancy acceleration/deceleration but I didnt need it)

int pos_rot = 0;  
int pos_elev = 0; 

int rot_counter = 0;
int elev_counter = 0;

int next_rot = -1;
int next_elev = 1;

int step_delay = 10; //Delay between steps in ms

const int motor_pin_1 = 16; // ESP D0
const int motor_pin_2 = 5;  // ESP D1
const int motor_pin_3 = 4;  // ESP D2
const int motor_pin_4 = 0;  // ESP D3
const int motor_pin_5 = 2;  // ESP D4
const int motor_pin_6 = 14; // ESP D5
const int motor_pin_7 = 12; // ESP D6
const int motor_pin_8 = 13; // ESP D7

void setup() {
  pinMode(motor_pin_1, OUTPUT); // Blue
  pinMode(motor_pin_2, OUTPUT); // Pink
  pinMode(motor_pin_3, OUTPUT); // Yellow
  pinMode(motor_pin_4, OUTPUT); // Orange
  pinMode(motor_pin_5, OUTPUT);
  pinMode(motor_pin_6, OUTPUT);
  pinMode(motor_pin_7, OUTPUT);
  pinMode(motor_pin_8, OUTPUT);
  
  // Begin Serial communication at a baud rate of 9600:
  Serial.begin(115200);
  delay(100);Serial.println("Leaving Setup");delay(100);
}

void loop() {
    //test_sweep();
    rot(2038);delay(2000);rot(-2038);   // 2038 steps clockwise followed by the same counterclockwise
    elev(1019);delay(2000);elev(-1019); // 1019 steps clockwise followed by the same counterclockwise
}

void test_sweep(){
      Serial.print("Rotating CCW, pos_rot = ");Serial.println(pos_rot);

    while (pos_rot < 2038) {
    rot(1);
    pos_rot = pos_rot + 1;
 
        if ((pos_rot < 1019) && ((pos_rot % 2) == 0)) {
          elev(2);          
        }
        else if ((pos_rot > 1019) && ((pos_rot % 2) == 0)) {
          elev(-2);
        }
  }
    while (pos_rot > 0) {
    rot(-1);
    pos_rot = pos_rot - 1;
 
        if ((pos_rot > 1019) && ((pos_rot % 2) == 0)) {
          elev(2);
        }
        else if ((pos_rot < 1019) && ((pos_rot % 2) == 0)) {
          elev(-2);
        }
  }
}

void elev(int num_steps) {
    if (num_steps > 0) {
        while (elev_counter < num_steps) {        
          elev_step(next_elev);
          next_elev = next_elev +1;
          if (next_elev > 7) { next_elev = 0;}
          elev_counter = elev_counter +1;
        }
    }
    else {
        num_steps = abs(num_steps);
        while (elev_counter < num_steps) {        
          elev_step(next_elev);
          next_elev = next_elev - 1;
          if (next_elev < 0) { next_elev = 7;}
          elev_counter = elev_counter +1;
        }    
    }
    elev_counter = 0;
}

void rot(int num_steps) {
    if (num_steps > 0) {
    
        while (rot_counter < num_steps) {        
          rot_step(next_rot);
          next_rot = next_rot +1;
          if (next_rot > 7) { next_rot = 0;}
          rot_counter = rot_counter +1;
        }
    }
    else {
        num_steps = abs(num_steps);
        while (rot_counter < num_steps) {        
          rot_step(next_rot);
          next_rot = next_rot - 1;
          if (next_rot < 0) { next_rot = 7;}
          rot_counter = rot_counter +1;
        }    
    }

    rot_counter = 0;  
}

void rot_step(int go_step) {
  switch (go_step) {
      case 0:  // 0001
        digitalWrite(motor_pin_1, 0);  // Blue
        digitalWrite(motor_pin_2, 0);  // Pink
        digitalWrite(motor_pin_3, 0);  // Yellow
        digitalWrite(motor_pin_4, 1);  // Orange
      break;
      case 1:  // 0011
        digitalWrite(motor_pin_1, 0);
        digitalWrite(motor_pin_2, 0);
        digitalWrite(motor_pin_3, 1);
        digitalWrite(motor_pin_4, 1);
      break;
      case 2:  //0010
        digitalWrite(motor_pin_1, 0);
        digitalWrite(motor_pin_2, 0);
        digitalWrite(motor_pin_3, 1);
        digitalWrite(motor_pin_4, 0);
      break;
      case 3:  //0110
        digitalWrite(motor_pin_1, 0);
        digitalWrite(motor_pin_2, 1);
        digitalWrite(motor_pin_3, 1);
        digitalWrite(motor_pin_4, 0);
      break;
      case 4:  // 0100
        digitalWrite(motor_pin_1, 0);
        digitalWrite(motor_pin_2, 1);
        digitalWrite(motor_pin_3, 0);
        digitalWrite(motor_pin_4, 0);
      break;
      case 5:  // 1100
        digitalWrite(motor_pin_1, 1);
        digitalWrite(motor_pin_2, 1);
        digitalWrite(motor_pin_3, 0);
        digitalWrite(motor_pin_4, 0);
      break;
      case 6:  //1000
        digitalWrite(motor_pin_1, 1);
        digitalWrite(motor_pin_2, 0);
        digitalWrite(motor_pin_3, 0);
        digitalWrite(motor_pin_4, 0);
      break;
      case 7:  //1001
        digitalWrite(motor_pin_1, 1);
        digitalWrite(motor_pin_2, 0);
        digitalWrite(motor_pin_3, 0);
        digitalWrite(motor_pin_4, 1);
      break;
    }
    delay(step_delay);
}

void elev_step(int go_step) {
  //Serial.println(go_step);
  switch (go_step) {
      case 0:  // 0001
        digitalWrite(motor_pin_5, LOW);
        digitalWrite(motor_pin_6, LOW);
        digitalWrite(motor_pin_7, LOW);
        digitalWrite(motor_pin_8, HIGH);
      break;
      case 1:  // 0011
        digitalWrite(motor_pin_5, LOW);
        digitalWrite(motor_pin_6, LOW);
        digitalWrite(motor_pin_7, HIGH);
        digitalWrite(motor_pin_8, HIGH);
      break;
      case 2:  //0010
        digitalWrite(motor_pin_5, LOW);
        digitalWrite(motor_pin_6, LOW);
        digitalWrite(motor_pin_7, HIGH);
        digitalWrite(motor_pin_8, LOW);
      break;
      case 3:  //0110
        digitalWrite(motor_pin_5, LOW);
        digitalWrite(motor_pin_6, HIGH);
        digitalWrite(motor_pin_7, HIGH);
        digitalWrite(motor_pin_8, LOW);
      break;
      case 4:  // 0100
        digitalWrite(motor_pin_5, LOW);
        digitalWrite(motor_pin_6, HIGH);
        digitalWrite(motor_pin_7, LOW);
        digitalWrite(motor_pin_8, LOW);
      break;
      case 5:  // 1100
        digitalWrite(motor_pin_5, HIGH);
        digitalWrite(motor_pin_6, HIGH);
        digitalWrite(motor_pin_7, LOW);
        digitalWrite(motor_pin_8, LOW);
      break;
      case 6:  //1000
        digitalWrite(motor_pin_5, HIGH);
        digitalWrite(motor_pin_6, LOW);
        digitalWrite(motor_pin_7, LOW);
        digitalWrite(motor_pin_8, LOW);
      break;
      case 7:  //1001
        digitalWrite(motor_pin_5, HIGH);
        digitalWrite(motor_pin_6, LOW);
        digitalWrite(motor_pin_7, LOW);
        digitalWrite(motor_pin_8, HIGH);
      break;
    }
    delay(step_delay);
}

That’s it!

Complete Influx Ver. 1.x TICK Stack Disaster Recovery

Note: This only works for Version 1.x (1.6 specifically for me), I have since upgraded to Version 2.x.

My entire system became corrupt one day and while it was technically booting it was not functioning. I did not have proper backups so the road to recovery was long & painful. I now have better emphasis on backups.

Typically all Influx data is backed up by:

influxd backup -portable /media/usb/drive

and restored with

influxd restore -portable /media/usb/drive

I did not have this luxury so I started with copying all the main files to and external drive, these were:

/var/lib/influxdb/data
/var/lib/influxdb/wal
/var/lib/influxdb/meta
/var/lib/kapacitor/kapacitor.db

Okay we are now finished with the corrupted image, do a full fresh install of your system. (tutorial)

Great we are now all setup, insert USB where files were backed up to before. We need to find the UUID of the USB and then edit fstab to mount the USB automatically:

ls -l /dev/disk/by-uuid/
sudo nano /etc/fstab

Add below line to fstab: (edit UUID for your device)

UUID=18A9-9943 /media/usb vfat auto,nofail,noatime,users,rw,uid=pi,gid=pi 0 0

Now we need to tell Influx config to look at memory stick, edit the below file with:

[meta]
  #dir = "/var/lib/influxdb/meta"
  dir = "/media/usb/drive/meta"

[data]
  #dir = "/var/lib/influxdb/data"
  dir = "/media/usb/drive/data"

  #wal-dir = "/var/lib/influxdb/wal"
  wal-dir = "/media/usb/drive/wal"

I also needed to change the user of the files on the USB by:

sudo chown -R influxdb:influxdb /media/usb/drive

We will revert some of the above changes later on.
Note: My original plan was to have all files on the USB drive permanently but as soon as I added the data source in Chronograf everything broke so I undid this. I just used this step to export the data properly.

Reboot system.

Now all your old data should be loaded.

Now we will create a proper backup of the data with the below:

influxd backup -portable /media/usb/drive

Revert all changes in the influxdb.conf file:

sudo nano /etc/influxdb/influxdb.conf 

Now restore all data back to the default locations by:

influxd restore -portable /media/usb/drive

Since it took me a few days to figure out how to restore data I already had the system back up recording data, the above restore does not work if a database is already created so I had to side-load all databases in with:

influxd
CREATE DATABASE my_data_bak
USE my_data_bak
SELECT * INTO my_data..:MEASUREMENT FROM /.*/ GROUP BY *
DROP DATABASE my_data_bak
exit

Finally add back in your Chronograf alerts etc. by:

sudo mv /var/lib/kapacitor/kapacitor.db /var/lib/kapacitor/kapacitor_orig.db 
sudo mv /media/usb/drive/kapacitor.db /var/lib/kapacitor/
sudo chown -R kapacitor:kapacitor /var/lib/kapacitor/kapacitor.db

Future planning would be to keep regular backups with: (you need to do this individually for all databases). See my other post on this.

influxd backup -portable /media/usb-influx/backup
kapacitor backup /media/usb/drive/kapacitor.db

Reboot and we are done!

Resources used to create this page:

Using integral function on Grafana (covert Watt to kWh)

After fighting for longer than I’d like to admit with this function I finally managed to get it working.

I use a single stat visualation and the below queries to give me energy usage in Watt/Hour from my data stored in Watts.

1hr Usage: (Relative time over-ride = 1h)

SELECT integral("Energy_Usage",1h) FROM "esp" WHERE ("Device" = 'esp_03') AND $timeFilter GROUP BY time(3h) 

24hr Usage: (Relative time over-ride = 24h)

SELECT integral("Energy_Usage",1h) FROM "esp" WHERE ("Device" = 'esp_03') AND $timeFilter GROUP BY time(3d) 

7 Day Usage: (Relative time over-ride = 7d)

SELECT integral("Energy_Usage",1h) FROM "esp" WHERE ("Device" = 'esp_03') AND $timeFilter GROUP BY time(21d) 

That’s it!

Installing TICK Stack on RPi4 (InfluxDB Ver 1.6)

Nothing complicated this time, just commands I use to setup my Influx TICK stack from fresh install.

I’ve since migrated to InfluxDB version 2.x so see set-up for that here.

sudo apt-get update
sudo apt-get upgrade
wget -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release
test $VERSION_ID = "10" && echo "deb https://repos.influxdata.com/debian buster stable" | sudo tee /etc/apt/sources.list.d/influxdb.list
sudo apt-get install influxdb
sudo apt install influxdb-client
sudo apt-get update
sudo apt-get install telegraf
sudo apt-get install chronograf
sudo apt-get install kapacitor
sudo systemctl unmask influxdb.service 
sudo systemctl start influxdb 
sudo apt-get install fail2ban
sudo apt-get install ntp
sudo apt-get install ntpstat
systemctl stop systemd-timesyncd
systemctl disable systemd-timesyncd
/etc/init.d/ntp stop
/etc/init.d/ntp start
sudo reboot

Confirm everything is working:

sudo service kapacitor status
sudo service chronograf status
sudo service influxdb status
sudo service telegraf status
ntpstat

You can also head to the Chronograf configuration page on: http://192.168.1.xxx:8888

That’s it!

Backup & Restore Grafana

I had full TICK stack and Grafana running on a RPi4 for a couple of months without issue until suddenly CPU usage went through the roof and reduced functionality (caused by InfluxDB, unknown why) so I need to do a full reinstall, at this point I decided to put Grafana on a separate machine (RPi3), below is how to export Grafana configuration and import onto a different machine.

Export old config by copying the below files to external USB drive:

/var/lib/grafana/grafana.db
/etc/grafana/grafana.ini

After Installing Grafana on new machine:
Note: You can upgrade to the highest minor release of your current Grafana version, I upgraded from 6.3 to 6.7.3. All versions viewable here.

sudo apt update
sudo apt upgrade
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana-rpi_6.7.3_armhf.deb
sudo dpkg -i grafana-rpi_6.7.3_armhf.deb
sudo systemctl unmask grafana-server.service
sudo systemctl start grafana-server
sudo systemctl enable grafana-server.service
sudo reboot

Insert USB into new machine and import the configuration files again:

cd usb-drive/
sudo cp grafana.db /var/lib/grafana/
sudo cp grafana.ini /etc/grafana/

The only other adjustment I had to do was adjust the Grafana Datasources URL from the previous local host to the InfluxDB Server address since they were now on different machines.

That’s it!

Tweeting when Aircraft Overhead

As per my previous post (specifically the “Logging to database: (SQLite)” paragraph) I am logging detected flights to SQL database, with a small bit of code we can tweet when certain aircraft are detected overhead:

First create a twitter account if not done so already.

Next setup tweepy for python and get your twitter authentication tokens using this tutorial: https://realpython.com/twitter-bot-python-tweepy/

Finally replace the last line of the exiting write to database code at my GitHub with:

    for index, row in df1.iterrows():
        if df1['hex'][index] == 'HEX_CODE_YOU_WANT_TO_TWEET_ABOUT': 
            print("Found")
            import tweepy
            # Authenticate to Twitter
            auth = tweepy.OAuthHandler("AUTH_TOKEN","AUTH_TOKEN")
            auth.set_access_token("AUTH_TOKEN","AUTH_TOKEN")
            api = tweepy.API(auth)

            try:
                api.verify_credentials()
                print("Authentication OK")
                api.update_status('Tweet Text' + str(dateTime))

            except:
                print("Error during authentication")
        else:
            print("Hex was: ", df1['hex'][index])

    exit()

That’s it, happy tweeting!