Category Archives: Coding

Monitoring My House

My standard setup is an Arduino connected to PC either a Raspberry Pi or a nearby PC that can serve as a host.

I decided to relocate my server outside into the garage and I was concerned about how it would cope in the cold and sometimes dusty new environment. My first step was to step up monitoring internally for the server. Disk drive and main board temperatures along with fan speeds would need to be logged. This is covered in detail in my next post.

Once the internal stuff was done I looked at external factors, humidity and temperature being the most important. This is familiar ground for me and setting up a report Arduino was simple as I had a few left from my greenhouses.

With the basics taken care of, I added a sensor to the door to alert if it remained open too long. I also added a check based on the time. The door should be closed between 23:00 and 07:00 most of the time. If it opens between these times I get an alert to my phone to investigate.

I’ll break down each stage in future blog posts.

Tornado Testing

Tornado testing can get complex. Below is a simple snippet for testing a POST request that accesses a suppliedĀ argument. Took me ages to figure out the arguments needed to be urlencoded. Hope this saves you some time when testing your Tornado code.

from tornado import ioloop
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application, RequestHandler
import urllib


class MainHandler(RequestHandler):
    def get(self):
        self.write("Hello, world")

    def post(self):
        name = self.get_argument('name')
        self.write("Hello, {0}".format(name))


class TestHelloWorld(AsyncHTTPTestCase):

    def get_app(self):
        return Application([(r"/", MainHandler)])

    def test_hello_world(self):
        # add any useful header stuff here
        headers = {}

        data = {'name':'Tony'}
        body = urllib.urlencode(data)

        self.http_client.fetch(
            self.get_url('/'),
            self.stop,
            method="POST",
            body=body,
            headers=headers
        )
        response = self.wait()
        self.assertEquals(response.body, 'Hello, Tony')


if __name__ == "__main__":
    application = Application([
        (r"/", MainHandler),
    ])
    application.listen(8888)
    ioloop.IOLoop.instance().start()