Uncategorized

MixologyBot: Glueing raspberry pi and arduino together with a python daemon

I have been incredibly busy. Friday I had an hour on the laser cutter and managed to complete three Makelangelo 2 kits and then stopped working. I was able to fill one existing order and then the other two sold the same day. I really need to learn about scaling up production. I’ve got parts printing every day to fill back orders on Delta 3, Delta 4, and HOG drive kits. For those of you who have had to wait for your order, I’m truly sorry. It will never happen again because I am never again selling something that isn’t already in stock.

While the parts are being produced I’ve been doubling down by working on the code for the MixologyBot. The only piece of software missing is a bit of “glue” to connect the web pages on the Raspberry Pi to the Arduino. Here’s what I’ve got so far.

[code]#!/usr/bin/python
# -*- coding: utf-8 -*-

# python daemon to connect PHP and Arduino
# [email protected] 2013

from daemon import runner
from time import gmtime, strftime
import serial
import MySQLdb as mdb
import lockfile
import time
import sys

# config
PIDFILE = ‘/tmp/ardaemon.pid’
LOGFILE = ‘/var/log/ardaemon.log’

class App():
def __init__(self):
self.stdin_path = ‘/dev/null’
self.stdout_path = LOGFILE
self.stderr_path = LOGFILE
self.pidfile_path = PIDFILE
self.pidfile_timeout = 5

def run(self):
con = mdb.connect(‘localhost’,’root’,’43jkdf71′,’waddle_igby’)
ser = serial.Serial(‘/dev/ttyACM0’,57600)
cansend = False

while True:
# anything to receive?
numbytes = ser.inWaiting()
if numbytes > 0:
# grab whatever is incoming and put it in the db.
line=con.escape_string(ser.read(numbytes))
if line == ‘>’:
cansend = True
from_cursor = con.cursor()
from_cursor.execute("INSERT INTO `from_arduino` SET transmit_time=NOW(), message=%s",str(line))
from_cursor.close()
con.commit()
#print strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ‘ ‘ + (("<< " + line))

# anything to transmit?
from_cursor = con.cursor()
from_cursor.execute("SELECT * FROM `to_arduino` ORDER BY id LIMIT 1")
if cansend==True and from_cursor.rowcount > 0:
# send it, then take it out of the buffer.
line = from_cursor.fetchone()
from_cursor.close()
ser.write(line[2])
#print strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ‘ ‘ + ((">> (" + str(line[0]) + ") " + line[2]))
del_cursor = con.cursor()
del_cursor.execute("DELETE FROM `to_arduino` WHERE id=%s LIMIT 1",str(line[0]))
#print strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ‘ ‘ + ((str(del_cursor.rowcount) + " rows affected.\n"))
del_cursor.close()
con.commit()
else:
from_cursor.close()

# now take a break.
time.sleep(0.25);

# run the thing
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()[/code]

Many special thanks to the lovely people in irc.freenode.net#python for their patience and help. If you use this program, link back to me, send an email, or something. With this the last major software hurdle is