From b02cc24abe9237425c540074906e7683ca1b30a5 Mon Sep 17 00:00:00 2001 From: Stephen M Moraco Date: Thu, 3 Sep 2020 15:12:21 -0600 Subject: [PATCH] fix antenna tuning and adjust READMEs --- ISP-lightning-mqtt-daemon.py | 22 +++++++++++---- README-SPI.md | 47 +++++++++++++++++++++++++++++++- README.md | 52 +++++++++++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 9 deletions(-) diff --git a/ISP-lightning-mqtt-daemon.py b/ISP-lightning-mqtt-daemon.py index dab2e3f..8463734 100755 --- a/ISP-lightning-mqtt-daemon.py +++ b/ISP-lightning-mqtt-daemon.py @@ -29,7 +29,7 @@ signal(SIGPIPE,SIG_DFL) -script_version = "2.1.9" +script_version = "2.2.0" script_name = 'ISP-lightning-mqtt-daemon.py' script_info = '{} v{}'.format(script_name, script_version) project_name = 'lightning-detector-MQTT2HA-Daemon' @@ -569,7 +569,10 @@ def send_status(timestamp, energy, distance, strikeCount): statusData = OrderedDict() statusData[LD_TIMESTAMP] = timestamp.astimezone().replace(microsecond=0).isoformat() statusData[LD_ENERGY] = energy - statusData[LD_DISTANCE] = distance + if distance == None: + statusData[LD_DISTANCE] = 'out of range' + else: + statusData[LD_DISTANCE] = distance statusData[LD_COUNT] = strikeCount print_line('Publishing to MQTT topic "{}, Data:{}"'.format(state_topic, json.dumps(statusData))) @@ -693,8 +696,11 @@ def calculate_ring_widths(): def binIndexFromDistance(distance): try: + testDistance = distance + if distance == None: + testDistance = 63 # given distance determine index value for it... NOTE: 1=idx-0 and 63=idx-15 - desiredBinIndex = distanceValueToIndexList.index(distance) + desiredBinIndex = distanceValueToIndexList.index(testDistance) # if we have 1-14 let's translate it into a ring index value [1-[3-7]] if desiredBinIndex > 0 and desiredBinIndex < 15: desiredBinIndex = binIndexesForThisRun[desiredBinIndex - 1] @@ -939,7 +945,9 @@ def report_current_accumulator(topic): detector.set_indoors(detector_afr_gain_indoor) detector.set_noise_floor(default_detector_noise_floor) # Tuning value for the detector -detector.set_tune_antenna(tuning_capacitor) +#detector.set_tune_antenna(tuning_capacitor) +print_line('* calibrate with antenna cap. set to {}'.format(hex(tuning_capacitor)), verbose=True) +detector.full_calibration(tuning_capacitor) # Prevent single isolated strikes from being logged => interrupts begin after 5 strikes, then are fired normally detector.set_min_strikes(detector_min_strikes) @@ -993,7 +1001,11 @@ def handle_interrupt(channel): energy = synth_energy strikes_since_last_alert += 1 - print_line(" -- Energy: " + str(energy) + " - distance: " + str(distance) + "km") + + distanceStr = str(distance) + "km" + if distance == None: + distanceStr = 'out-of-range' + print_line(" -- Energy: " + str(energy) + " - Distance: " + distanceStr) # if we are past the end of this period then snap it and start accumulating all over if last_alert != datetime.min and (current_timestamp - last_alert).seconds > period_in_minutes * 60: diff --git a/README-SPI.md b/README-SPI.md index 9b8c7ec..039708a 100644 --- a/README-SPI.md +++ b/README-SPI.md @@ -149,8 +149,53 @@ Using the command line argument `--config`, a directory where to read the config ```shell python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --config /opt/ISP-lightning-mqtt-daemon ``` +## Antenna Fine Tuning -### Run as Daemon / Service +The AS3935 has a fine tuning adjustment setting for the 500KHz antenna. Our script has a special option we can use to determine the fine-tuning value our board needs. After running the script we then record the value in our config.ini. + +Run the fine tuning option using: + +```shell +python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap +``` + +the output will look something like (yours will be different!): + +```shell +* Please allow a long time for this function to stop. It should take a little over 3 minutes to check test the 16 values +For tuning 0x0: average frequency of 511695.239310 Hz (diff: +731.0) +For tuning 0x1: average frequency of 511706.015761 Hz (diff: +731.6) +For tuning 0x2: average frequency of 507383.793496 Hz (diff: +461.5) +For tuning 0x3: average frequency of 507158.814087 Hz (diff: +447.4) +For tuning 0x4: average frequency of 503960.562001 Hz (diff: +247.5) +For tuning 0x5: average frequency of 504470.100857 Hz (diff: +279.4) +For tuning 0x6: average frequency of 502103.026784 Hz (diff: +131.4) +For tuning 0x7: average frequency of 500849.485221 Hz (diff: +53.1) +For tuning 0x8: average frequency of 498205.778733 Hz (diff: +112.1) +For tuning 0x9: average frequency of 495721.390191 Hz (diff: +267.4) +For tuning 0xa: average frequency of 497672.497232 Hz (diff: +145.5) +For tuning 0xb: average frequency of 494356.500465 Hz (diff: +352.7) +For tuning 0xc: average frequency of 490485.394396 Hz (diff: +594.7) +For tuning 0xd: average frequency of 491429.095599 Hz (diff: +535.7) +For tuning 0xe: average frequency of 489151.314855 Hz (diff: +678.0) +For tuning 0xf: average frequency of 490377.098284 Hz (diff: +601.4) +- Your best tuning capacitor value is 0x7: which is off by +53.1 +``` + +now you need to add the 0x7 from the last line (above) to the config.ini. Look for the **[Sensor]** section and in there add a line: + +```shell +tuning_capacitor = 0x7 +``` + +(*If you have qeustions about this take a look at the `config.ini.dist` file as it shows where the new entry should be.*) + +Save the file, and do a quick test again to make sure the script is ok with your config change. + + +Now you are ready to enable the detector script. + +## Run as Daemon / Service You probably want to execute this script **continuously in the background**. diff --git a/README.md b/README.md index d2b9a9f..2d54512 100644 --- a/README.md +++ b/README.md @@ -93,8 +93,8 @@ You'll need the AS3935 Lightning sensor to be connected (via I2C for now) to you | AS3935 Pin | Module Pin | Raspberry Pi Pin | |-----------------|------------|------------------| | 4 (GND) | GND | 9 (Ground) | -| 5 (VDD) | VCC | 1 (3.3v) | -| 10 (IRQ) | IRQ | 11 (GPIO 17) | +| 5 (VDD) | 3V3 | 1 (3.3v) | +| 10 (IRQ) | INT | 11 (GPIO 17) | | 11 (I2CL) | SCL | 5 (SCL) | | 13 (I2CD) | SDA / MOSI | 3 (SDA) | @@ -140,7 +140,53 @@ Using the command line argument `--config`, a directory where to read the config python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --config /opt/ISP-lightning-mqtt-daemon ``` -### Run as Daemon / Service +## Antenna Fine Tuning + +The AS3935 has a fine tuning adjustment setting for the 500KHz antenna. Our script has a special option we can use to determine the fine-tuning value our board needs. After running the script we then record the value in our config.ini. + +Run the fine tuning option using: + +```shell +python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap +``` + +the output will look something like (yours will be different!): + +```shell +* Please allow a long time for this function to stop. It should take a little over 3 minutes to check test the 16 values +For tuning 0x0: average frequency of 511695.239310 Hz (diff: +731.0) +For tuning 0x1: average frequency of 511706.015761 Hz (diff: +731.6) +For tuning 0x2: average frequency of 507383.793496 Hz (diff: +461.5) +For tuning 0x3: average frequency of 507158.814087 Hz (diff: +447.4) +For tuning 0x4: average frequency of 503960.562001 Hz (diff: +247.5) +For tuning 0x5: average frequency of 504470.100857 Hz (diff: +279.4) +For tuning 0x6: average frequency of 502103.026784 Hz (diff: +131.4) +For tuning 0x7: average frequency of 500849.485221 Hz (diff: +53.1) +For tuning 0x8: average frequency of 498205.778733 Hz (diff: +112.1) +For tuning 0x9: average frequency of 495721.390191 Hz (diff: +267.4) +For tuning 0xa: average frequency of 497672.497232 Hz (diff: +145.5) +For tuning 0xb: average frequency of 494356.500465 Hz (diff: +352.7) +For tuning 0xc: average frequency of 490485.394396 Hz (diff: +594.7) +For tuning 0xd: average frequency of 491429.095599 Hz (diff: +535.7) +For tuning 0xe: average frequency of 489151.314855 Hz (diff: +678.0) +For tuning 0xf: average frequency of 490377.098284 Hz (diff: +601.4) +- Your best tuning capacitor value is 0x7: which is off by +53.1 +``` + +now you need to add the 0x7 from the last line (above) to the config.ini. Look for the [Sensor] section and in there add a line: + +```shell +tuning_capacitor = 0x7 +``` + +(*If you have qeustions about this take a look at the `config.ini.dist` file as it shows where the new entry should be.*) + +Save the file, and do a quick test again to make sure the script is ok with your config change. + + +Now you are ready to enable the detector script. + +## Run as Daemon / Service You probably want to execute this script **continuously in the background**.