JTK – Notes and Ramblings Things we have found …

5/18/2020

Earthquake

Filed under: General,Home Automation — taing @ 4:50 pm

An article on the Raspberry Shake got me looking for more on earthquake sensors. I found the Omron D7S. Datasheet here.

The chip can be found at both Mouser and Digikey. It is a small SMT mountable PCB that communicates via i2c. The datasheet provides little info on actually programming the device. Googling about found two libraries that helped shed some light on things.

First, from Omron, a python library on github. The platform of choice is a Pi Zero W with Rasbian Buster Lite. To get started, install git, i2c-tools, python3-smbus, and python3-rp-gpio. Clone the repo and the software is ready.

Connecting the sensor to the Pi only needed four wires – 3.3v, gnd, SDA, SCL. 10k pull-up resistors are used for SDA and SCL. A 10k pull-up was also added for the sensor “settings” pin. Both Vcc and both Ground pins on the sensor need to be wired.

  • Pi pin 1 – Vcc to Sensor pins 1 & 9
  • Pi pin 1 – Vcc to 10K pullup resistors to sensor pins 4, 5 & 7
  • Pi pin 9 – Ground to Sensor pin 6 & 10
  • Pi pin 3 – SDA to sensor pin 5
  • Pi pin 5 – SCL to sensor pin 4

Sensor pins 2 and 3 provide optional interrupts. Pin 3 goes low when a quake is in happening. Pin 2 goes low to signal suggested shutdown due to expected significant damage. These pins are not used in this setup.

sudo i2cdetect -y 1 showed the chip at address 0x55. sudo python3 sample_d7s ran as advertised.

I added a few line of code to have the output go to a Google Sheet and set the unit up in the basement. Details on how this works can be found in an earlier post.

The second library is for Arduino. It appears to provide much greater detail on the registers and parameters on the chip but I have not tested it.

An additional article on How to Make an Earthquake Alarm is a alternative approach.

The chip measures quake intensity not magnitude. The measurement correlates strongly with the Japan Meteorological Agency seismic intensity scale. A discussion on various seismic intensity scales can be found here or copied here.

Omron shared a more detailed datasheet.

5/13/2020

Lightning + Google

Filed under: General,Home Automation — taing @ 1:45 pm

Based on the trick seen here we have added Google Sheets logging to the lightning Python script.

A copy of the example sheet in the original GitHub with a few changes to the column headers, publish the API and that side is all set.

For the Python side we needed to “import urllib.request” and request the url. Then is was as simple as “r = urllib.request.urlopen(urlStr+outStr)”. In this case urlStr is the url from Google and outStr is the data. For example, ‘?type=”Strike”&distance=32&energy=200’.

When publishing the Google Sheet Web API the simple choice is anonymous. I have not looked at handling credentials for Google login in the Python script.

Lightning

Filed under: General,Home Automation — taing @ 1:18 pm

My original Embedded Adventures MOD-1016v8 lightning detector based on the as3935 failed. I ordered a replacement from Sparkfun. The Sparkfun hook-up guide discusses the sensors I2C interface being not as reliable as the SPI interface. Sparkfun also has a github for the PCB. Originally I was using code based on a Python I2C library. So a change is needed.

My first thought was to port the Sparkfun Arduino library over to Raspberry Pi using wiringPi. It should be noted that as of August 2019 the author of wiringPi is no longer supporting it. This was a bit frustrating but after a bit of testing with the scope and bus pirate and a lot of googling. This issues seem to be resolved.

At first there was data on MOSI but nothing coming back. After attaching the Bus Pirate using the setup below:

m 5 (for SPI mode)
Speed: 125khz
Clock polarity: Idle low (default)
Output Clock Edge: Active to idle (default)
Input sample phase: End (NOT default)
CS: /CS (default)
Output type: Normal

I was able to communicate. Notice the Input Phase angle being set to “end”. When it was set to “middle”, the default, writes worked but reads shifted the result one bit to the right effectively dropping the LSB. This turns out to be vital for the wiringPi code also. The standard SPI help page for wiringPi doesn’t mention anything about mode or phase. But our friend google gave me a discussion about possible modes. So instead of the basic wiringPiSPISetup(channel, speed) function I needed to use wiringPiSPISetupMode(channel, speed, mode). Bit 0 is clock phase, bit 1 is clock polarity. The other bits are not possible via wiringPi but as discussed elsewhere can be set by a direct ioctl call.

I also slowed the speed down toe 125khz from the original 2Mhz.

For wiringPi i used the following pins:

GND: pi pin 25
SCK: pi pin 23 (SCLK)
MISO: pi pin 21 (MISO)
MOSI: pi pin 19 (MOSI)
3.3v: pi pin 17
INT: pi pin 15 (GPIO 22)
CS: pi pin 24 (CE0 / GPIO 8)

I considered a version using bcm2835 but didn’t go very far down this road. No work has been done on that front.

I also decided to play a bit with Sparkfun and CircuitPython and sparkfun-circuitpython-qwiicas3935 1.0.3. The complete CircuitPython bundle is on github. The as3935 bundle can also be found on github.

The Pi I was working with is a Pi II with Jesse installed. Installing Python 3.5 or higher on Jesse is more work than simply making a newer Buster Lite sd card. Then it is a matter of following the Sparkfun / AdaFruit instructions to get all of the necessities installed:

sudo apt-get install python3-pip
sudo pip3 install --upgrade setuptools
pip3 install RPI.GPIO
pip3 install sparkfun-circuitpython-qwiicas3935

The secret to getting this to work is realizing that you can’t use CE0/GPIO 8 for CS with the code provided. Circuit Python Bus Device(documentation) can’t manage the pin directly when SPI is in use and the hardware doesn’t handle it correctly either. Using Pi Pin 13 / GPIO 27 / circuit Python board.D27 as CS and things work fine.

An alternative is to change the _read_register() function to use write_readinto() in place of write() followed by readinto(). Using these the system manages CE0 just fine for CS.

The library file sparkfun_qwiicas3935.py is installed at /usr/local/lib/python3.7/dist-packages. My mods can be found here. The key bit is:

<code>self._spi.write_readinto(bytearray([addr,0]), result</code>)
# self._spi.write(bytearray([addr]))
<code># read the next byte after writing the address</code>
# self._spi.readinto(result)

The modified code also comments out the manual CS line manipulation.

Additional resources can be found at Instructables.

A copy of the AS3935 datasheet can be found here.

Powered by WordPress