Thursday, July 5, 2018

CAP - Water Fountain Power Consumption Issues

I got the thing working but...

I actually got this thing working months ago, but didn't find the time to write the followup. Pics!
Testing the raspi camera and Movidius stick. I deleted lines of code from an example to get this to work. So easy!

Not my finest work, but I was jamming at this point.

A very capable board, for a very simple task. Overkill also brings expediency, I barely had to lift a finger to get the wireless connection going (after I settled on Modbus).

The arrangement looking pretty good.

Yah came together nicely.

I bought this drill bit sometime ago. One of my favorite tools because it does the job so well. Figuring out where to land this hole was a delicate and careful process. I didn't want to ruin the sink.

Faucet nicely installed. Very happy with how it turned out.


I used a spare monitor to work on the pi locally, to get the camera right and set thresholds. I augmented the example I used to detect the cat with an on/off filter for the solenoid and a modbus library for connected to the ESP.


Here's the full test


... the battery runs out in a couple of days!!?

This thing was supposed to last for a month! What happened?

Turns out I didn't really understand the ESP specifications. The low current mode I was basing my design around disables the wifi transceiver which makes this somewhat useless for the cat. Really the ESP gives me an easy wireless interface to the solenoid control, and that's it.

Using Deep Sleep with an Interrupt

I need to get the standby current consumption of the solenoid controller down to miliamps. 5mA would be great, and get me nearly all the way to the goal of a month between charges.
My solution is to use a simple transmitter/receiver pair from the Sparkfun catalog to toggle a ultra-low-current watchdog chip typically used to reset microcontrollers, to reset the microcontroller (ie. wake it up) when the Pi sees a cat. From there we keep the controller alive by toggling the modbus coil. When the cat is finished and the Pi no longer sees a cat, the toggling stops and the controller goes back to sleep.


Of course there are some complications.

The radio works, pretty well...

This is the radio:
Transmitter
https://www.sparkfun.com/products/10535



Receiver
https://www.sparkfun.com/products/10533















These little radios are handy. Very low current consumption, around 4.5mA (if I am reading the spec-sheet correctly). They provide a single channel, on-off, so essentially a serial interface. They have a drawback though... When the channel is quiescent, they are very noisy. The trick to using these things is to either discard the first few pulses as a kind of preamble, or to always be transmitting a pulse. 10Hz was the lowest frequency I tried that seemed to keep the noise away.

Watchdog circuit

555 chips can be used to create a watchdog circuit, but they actually consume a fair amount of current, 10mA typically. Nowadays you can find a very very low current watchdog chip typically used for microcontroller applications. I selected the UCC3946. It consumes up to 18uA. Nothing to blush at. I selected a timing capacitor to set the WD period to 10ms.

Proper toggling

To wake the chip up I just need to slow my GPIO toggle period to 10Hz. To keep the controller in whatever state it's currently in, asleep or awake, I just toggle it at 100Hz or faster.

More efficient solenoid driver

During the time when this was working...somewhat I started on a sprinkler controller and started learning more about solenoid driver circuits. Obviously with a sprinkler controller, especially one running on a battery, you want power efficiency wherever you can manage it.

Spike and hold

Spike and hold uses a voltage spike to open the solenoid and a lower voltage to keep the solenoid open. So I can lower the power consumption of the solenoid driver by using such a circuit. I used a network analyzer to figure out the inductance and series resistance of the solenoid and designed a spike and hold circuit like the ones described here.

Conclusion

I learned a lot about these ESP chips, and their low power modes. From here I am wrapping up some modifications to the code to use the radio and we'll see if this works better. Here's a picture of the new design!

Tuesday, March 20, 2018

CAP - Water Fountain: Engineering

Intro

My cat only drinks from the tap. He doesn't know how to turn the water on or off. So I need a system to get him water when he wants it.

Requirements

  • Need
    • Clean install
    • Provide water when cat goes to the sink
      • Don't trigger on humans
    • Turn water off when cat is done
    • Run for a month between charges
  • Want
    • Tweet/ notify when cat gets water for peace of mind
    • Notify on low battery
    • Easily integrated and removed

Engineering

Clean install

I picked a sink I can easily modify to include a spout, and he's used to drinking from. There is no outlet nearby that I can wire to cleanly. So I'll have to use a battery.

A clean install means no visible wires or tubes running through the sink. 

Provide water: turn off/on when cat comes around

Control

ESP 8266 Thing from Sparkfun promises low-current draw and WiFi, nice!

Actuation

Use a solenoid valve:
DIGITEN DC 12V 1/4" Inlet Feed Water Solenoid Valve Quick Connect N/C normally Closed no Water Pressure

Sensing

Sensor selection/ design will be a trade off between accuracy and visibility.

IR sensor

  • Pros
    • Pretty standard way of doing automatic sinks
    • Cheap sensors
  • Cons
    • Wires
    • Very visible sensor
    • Compact or clean looking versions are hard to find ($$)
    • Might trigger when I'm using the sink (not desired)

Capacitive something...

  • Pros
    • Clever sensor placement/ design could exclude everything but the cat
    • Minimal
  • Cons
    • Might be visible
    • Not really straightforward

Piezo loadcell

  • Pros
    • Minimal, very clean, invisible
  • Cons
    • Difficult to design against non-cat triggers (extra weight in the sink will cause it to fire)

Machine vision

  • Pros
    • Fun to make
    • Could become very good at cat-only triggering
  • Cons
    • Overkill
    • Visible sensor/ camera

Run for a month between charges

Add up all the current consumers and estimate Oppie's drinking habits. Pretty simple.

  • ESP8266
  • Awake: ~200mA
  • Standby: ~0.9mA
  • Solenoid valve ~ 400mA

Oppie drinks water for ~ 10 minutes a day. 31 days in a month = 310 minutes.

I picked this thing:

The ESP may actually consume less current while running, but assuming some leakages here and there keeps things conservative.

Conclusions

  • Control and actuation is pretty straightforward. No decisions.
  • Battery pack looks like it should be sufficient
  • I'll use machine vision to detect the cat. Especially considering how cool/easy this thing looks:
Image result for intel movidius
Plus the fact that I can get this to trigger only on cats is really neat.


Sunday, January 7, 2018

Timber Spider

Getting there first

My parents are part of a timeshare pool in which they can trade weeks at their location for weeks at other places in the pool. Weeks are submitted to the pool by other owners, at semi-random, and are reserved on a first-come-first-serve basis. Some places are reserved more quickly than others. My dad checks the website that lists the locations almost every day. I thought... why not every hour? It was decided to send an MMS any time a really good week was added, and an email anytime a week of any quality was added or removed. Hopefully by getting alerts sent to our phones we could secure our favorite weeks before anyone else.

Making a robospider

After a few iterations I decided that scrapy was the right tool. It has a lot of stackoverflow stuff, a bunch of automation, processing pipelines, etc.
The availability website is behind a password protected login so I needed to use a spider that could handle the authentication step. I decided to use the InitSpider as it handles authentication before crawling.
Next I told the spider to seek out the "next >" links on the page for creating followup requests. The availabilities are listed in a standard table, so I parse each row as a scrapy Item. I set up an item processing pipeline and activated it to do the alerting.

Alert system

Items gleaned by the spider are passed through a pipeline which assembles them into a dictionary and compares them to an older dictionary for a difference. The older dictionary is shelved when the spider is closed. I decided to shelve the dictionaries as it seemed really easy to do and I thought doing something like a JSON export was overkill. Turns out shelving is really easy (except when it's not).
The comparison creates three lists: additional really good weeks, new weeks, and removed weeks. Later in the pipeline the text message and email is formatted. Both required setup of a dummy gmail account to send to my family. 

Installing and Automating

I had to get scrapy installed in a conda environment (turned out to be a problem) on my pi. I made sure to install the following before attempting to install scrapy.
sudo apt-get install libffi-dev
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
sudo apt-get install python-dev

It took a while for lxml to build on my little pi (~20 minutes). But after that step scrapy seemed to be available. I loaded my spider and hit go and ran smack into my first issue.

Traceback (most recent call last):
  File "/home/pi/berryconda2/lib/python2.7/site-packages/twisted/internet/defer.py", line 1386, in _inlineCallbacks
    result = g.send(result)
  File "/home/pi/berryconda2/lib/python2.7/site-packages/scrapy/crawler.py", line 98, in crawl
    six.reraise(*exc_info)
  File "/home/pi/berryconda2/lib/python2.7/site-packages/scrapy/crawler.py", line 82, in crawl
    yield self.engine.open_spider(self.spider, start_requests)
ImportError: No module named _bsddb

OK so I gotta figure out what this bsddb thing is. Apparently it's some BerkelyDB interface that twisted appears to use.

Getting bsddb3

First thing was to get the BerkeleyDB built for my pi. I followed the install instructions here:

Make sure you install the older version of BDB (5.3.28)

Actually I tried to just pip install bsddb3 but got smacked down.

After a while I decided to drop shelve, as soon as it seemed like it might be causing the bsddb dependency. After dropping it I encountered a new error, ECC Curve.

I went to the Twisted IRC after bashing my skull against the problem for a while and someone confirmed there was some missing stuff from my pyopenssl build. They said they'd try and reproduce the issue. So I waited for a bit.

Forgetting bsddb3, something else is wrong

I tried updating cryptography, twisted, and pyopenssl to the most recent versions I could. I did everything short of updating openssl for the pi to 1.1 (I am was running Jessie). Nothing seemed to work. So I hopped into the IRC for twisted and got some help from runciter. After running some tests they concluded conda was my problem. Specifically berryconda and my old openssl libs. I need to bump to 1.1 but I couldn't easily do that with Jessie. So I decided to upgrade to Stretch. After upgrading to Stretch and uninstalling scrapy, twisted, pyopenssl, and cryptography I installed scrapy again and voila, it works.

Wrapping it up

Last step was to add a simple cron job to execute my script on the hour from 6-23. 

Friday, January 5, 2018

Electronic Component Organizer Android App and Other Progress

On a roll

Lately I've taken it upon myself to really try and push to get this project done. It's been over a year or two in the making and I need to finish it. Unfortunately I keep getting sidetracked by extra things I want to do with it.

For instance, this week I had the idea of using my fancy new bluetooth receiver to serve as the wireless connection to the component organizer. I found some cool projects that people have worked on that provide ways to transfer data from an Android phone to an Arduino using FSK over the audio port (with music as the carrier?). 

So after a quick shopping spree at SF, I have some new toys shipping to me presently. My idea will be to do a pass-thru of the audio signal from the bluetooth receiver, through an Arduino based "box." This box will use an Arduino (or PIC?) to process the signal, and send the results to the box system via 315MHz radio. The results will either be an audio waveform FFT, or the address of a part. So when the box is idle the screen saver will be the FFT of the music passing through the receiver. If an address is requested, the request will be passed via FSK to the "analyzer" and then passed to the organizer.

And just when I thought I was finally finishing this thing...

Django Database and Dajaxice

I did manage to get my Django system running pretty much how I want. I am lazy and didn't want to build my own views for Django when the admin views were all I would ever need, and look so good. So I set to learning about how to modify the Django admin site (extend is the technical word). The framework is pretty easy to deal with. I managed to an ajax based button inside the change_list_results list. And through a bit of hacking managed to get that button to open a serial connection to the organizer Arduino, and send the address of the part. Once that system was running smoothly, I set about organizing my resistors.



Two movies (The Hobbit and There Will Be Blood), and a bunch nachos later and I finally have all my resistors organized. That's not to say I have all the standard 5% resistors in this box system (that would be too many even with each drawer divided into 2), but all the ones I do have are now organized. I don't have to wonder if I have any given resistor because I have then all inventoried.

Looking back, I feel like this project has been one of the most tedious I have ever undertaken. It is taking a lot of will power to not give up until every last resistor I own is properly organized. I mean... I think I went kinda crazy there for a while. Who pays that much attention to their resistor collection? I'm just glad I got it over with...

Android Studio, and Ubuntu 64bit

The next phase of this project is to get the voice search functioning. I am going to use my Android phone for the approach. The idea is to write an app that will use google's speech input API to interpret my speech. Then using tags for each entry, some manually entered, some automatically associated based on component data, search the database for the part. Once a part or list of possible parts is found, provide a "light it up" button that shows where the part is located. Upon clicking the button an address request is sent via the audio connection over bluetooth to the receiver. On it's way out of the bluetooth receiver the audio signal is picked up by the Arduino receiver which is programmed to recognize the FSK message. It relays the address via 315MHz radio to the orgranizer which then lights up the box.

Getting the app ready starts with learning how to write apps for Android, and getting my IDE all set up. I chose to go with Android Studio, because Google's IO conference debut of the IDE looked amazing.

After downloading and "installing" I started it up and started working through a tutorial on the speech recognition API. Android studio has this nice auto compile feature that tells you if your code works. When my setup tried to compile I ran into some issues which I posted, then solved, here.