This commit is contained in:
Dustin Brunner 2022-08-27 11:45:59 +02:00
commit 3f63276da7
7 changed files with 2153 additions and 0 deletions

62
README.md Normal file
View File

@ -0,0 +1,62 @@
# Influx-DB frequency logger with JDS6600 signal generator
A simple python script to write measured frequency values into a Influx-DB database.
## Python installation on Ubuntu
```
sudo apt-get update
sudo apt-get install python3
sudo apt-get install python3-pip
```
## Python Dependencies:
```
pip install pyserial
pip install influxdb
```
## running the script (automatically)
The script is auto-detecting the USB-port, where the signal generator is connected to. After that it connects to a Influx-DB-server and then publishes the measured frequency value every second to this server. Make sure to fill your login credentials into the `influxdb_config.py`-file.
To auto start the script at the sotartup of the server we configure a systemd-service. I assume the repository with this script is cloned into the home directory of one user.
```
sudo nano /etc/systemd/system/readfreq_influxdb.service
```
Write into this file:
```
[Unit]
Description=readfreq_influxdb python script
After=multi-user.target
[Service]
Type=simple
Restart=always
RestartSec=5
ExecStart=/usr/bin/python3 /home/user/software_python/readfreq_influxdb.py
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
```
No the autostart can be activated:
```
sudo systemctl enable readfreq_influxdb.service
sudo systemctl start readfreq_influxdb.service
sudo systemctl status readfreq_influxdb.service
```
To deactivate it, use the command `sudo systemctl disable readfreq_influxdb.service`.
# Sources/useful links:
- Python Lib (MIT License): https://github.com/on1arf/jds6600_python
- https://github.com/thomaseichhorn/funcgen
- https://www.thomaschristlieb.de/ein-python-script-mit-systemd-als-daemon-systemd-tut-garnicht-weh/
<br><br>
<p xmlns:dct="http://purl.org/dc/terms/" xmlns:cc="http://creativecommons.org/ns#" class="license-text">This work by <span property="cc:attributionName">Dustin Brunner</span> is licensed under <a rel="license" href="https://creativecommons.org/licenses/by/4.0">CC BY 4.0<img style="height:15px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" /><img style="height:15px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1" /></a></p>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons Lizenzvertrag" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a><br />Dieses Werk von <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Dustin Brunner</span> ist lizenziert unter einer <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Namensnennung 4.0 International Lizenz</a>.

View File

@ -0,0 +1,17 @@
import warnings
import serial
import serial.tools.list_ports
found_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'CH340' in p.description or 'USB Serial' in p.description #CH340 for Windows, USB Serial for Linux
]
if not found_ports:
raise IOError("No JDS6600-device found")
if len(found_ports) > 1:
warnings.warn('Multiple JDS6600-devices found - using the first')
portname = found_ports[0]
print(portname)

View File

@ -0,0 +1,16 @@
import os
import serial.tools.list_ports
ports = serial.tools.list_ports.comports(include_links=True)
print("Serial Ports:")
print("path \t\t| name \t\t| description")
print("-------------------------------------------------------------------------")
for port in ports :
print(port.device + " \t\t| " + port.name + "\t\t| " + port.description)
print("-------------------------------------------------------------------------")
os.system("pause")

10
influxdb_config.py Normal file
View File

@ -0,0 +1,10 @@
# ----------------
# Config file
# ----------------
# Config for the Influxdb-server
influxdb_host = '192.168.1.10'
influxdb_port = 8086
influxdb_user = 'user'
influxdb_password = '123456'
influxdb_dbname = 'frequency'

1893
jds6600.py Normal file

File diff suppressed because it is too large Load Diff

101
readfreq_influxdb.py Normal file
View File

@ -0,0 +1,101 @@
from jds6600 import jds6600
import warnings
import serial
import serial.tools.list_ports
import time
from influxdb import InfluxDBClient
from datetime import datetime
import influxdb_config
print("-----------------JDS6600-Reader-----------------")
print("Searching Device ...")
found_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'CH340' in p.description or 'USB Serial' in p.description #CH340 for Windows, USB Serial for Linux
]
if not found_ports:
raise IOError("No JDS6600-device found")
if len(found_ports) > 1:
warnings.warn('Multiple JDS6600-devices found - using the first')
portname = found_ports[0]
print("JDS6600 device found!")
print("Using Port ", portname)
jds = jds6600(portname)
#jds = jds6600("COM4")
print("--------------------------")
# API information calls
print("Devicetype: ", jds.getinfo_devicetype())
print("Serialnumber:", jds.getinfo_serialnumber())
print("--------------------------")
#Disable Both outputs
#print(jds.getchannelenable())
jds.setchannelenable(bool(0), bool(0))
print("Disabeling Outputs ... \t\t OK")
#print(jds.getmode())
jds.setmode('MEASURE')
print("Set Mode Measure ... \t\t OK")
jds.measure_setcoupling('AC')
jds.measure_setgate(1) #Gatetime 1s
jds.measure_setmode("PERIOD")
print("Configure Measurement ... OK")
print("--------------------------")
print("Connecting to Influx-DB ... OK")
dbclient = InfluxDBClient( influxdb_config.influxdb_host,
influxdb_config.influxdb_port,
influxdb_config.influxdb_user,
influxdb_config.influxdb_password,
influxdb_config.influxdb_dbname)
freq_value = 0.0
dbclient_success = False
dbclient_fail_counter = 0
print("--------------------------")
print("Starting to read data ...")
print("--------------------------")
log_count = 0
while 1:
if jds.getmode()[0] != 4: # 4 means mode 'MEASURE'
raise IOError("Measurement-mode is not enabled!")
freq_value = jds.measure_getfreq_p()
json_body = [
{
"measurement": "freq_Hz",
"fields": {
"value": freq_value
}
}
]
dbclient_success = dbclient.write_points(json_body, time_precision='ms')
if dbclient_success == True:
log_count = log_count + 1
if log_count >= 10:
print("f = ", freq_value, " DB OK")
log_count = 0
else:
dbclient_fail_counter = dbclient_fail_counter + 1
print("f = ", freq_value, " DB ERROR ", dbclient_fail_counter)
if dbclient_fail_counter >= 5:
raise IOError("Writing DB entry failed 5 times ... exiting!")
else:
dbclient_fail_counter = 0
time.sleep(1)

54
readfreq_terminal.py Normal file
View File

@ -0,0 +1,54 @@
from jds6600 import jds6600
import warnings
import serial
import serial.tools.list_ports
import time
print("-----------------JDS6600-Reader-----------------")
print("Searching Device ...")
found_ports = [
p.device
for p in serial.tools.list_ports.comports()
if 'CH340' in p.description or 'USB Serial' in p.description #CH340 for Windows, USB Serial for Linux
]
if not found_ports:
raise IOError("No JDS6600-device found")
if len(found_ports) > 1:
warnings.warn('Multiple JDS6600-devices found - using the first')
portname = found_ports[0]
print("JDS6600 device found!")
print("Using Port ", portname)
jds = jds6600(portname)
#jds = jds6600("COM4")
print("--------------------------")
# API information calls
print("Devicetype: ", jds.getinfo_devicetype())
print("Serialnumber:", jds.getinfo_serialnumber())
print("--------------------------")
#Disable Both outputs
#print(jds.getchannelenable())
jds.setchannelenable(bool(0), bool(0))
print("Disabeling Outputs ... \t\t OK")
#print(jds.getmode())
jds.setmode('MEASURE')
print("Set Mode Measure ... \t\t OK")
jds.measure_setcoupling('AC')
jds.measure_setgate(1) #Gatetime 1s
jds.measure_setmode("PERIOD")
print("Configure Measurement ... OK")
print("--------------------------")
while 1:
if jds.getmode()[0] != 4: # 4 means mode 'MEASURE'
raise IOError("Measurement-mode is not enabled!")
print(jds.measure_getfreq_p())
time.sleep(1)