Historical MySQL Bug#2, opened 12 Sep 2002, states that MySQL Connector/J doesn’t make toast. It hasn’t been fixed for more than 14 years. I’ve finally created a patch for it.
First of all: why only fix this for MySQL Connector/J? We should make sure the server can do this for any implementation! With this fix, now MySQL server (starting with version 5.1) can make toast.
There are few dependences though (see assembled setup picture):
Patch:
|
1 |
#!/usr/bin/python<br>import sys<br>from time import sleep<br><br>if len(sys.argv) == 2 and sys.argv[1].isdigit():<br> toast_time = sys.argv[1]<br>else:<br> toast_time = 10<br><br>print "Toasting for " + str(toast_time) + " seconds..."<br><br>import pifacedigitalio as p<br>try:<br> p.init()<br> p.digital_write(7,1)<br> sleep(float(toast_time))<br> p.digital_write(7,0)<br>except (KeyboardInterrupt, SystemExit):<br> print "Exiting and turning off heat..."<br> p.digital_write(7,0)<br> sys.exit(1)<br><br>print "Your toast is ready! Enjoy! " |
|
1 |
char* make_toast(<br> UDF_INIT *initid<br>, UDF_ARGS *args<br>, char* result<br>, unsigned long* length<br>, char *is_null<br>, char *error<br>){<br> FILE *pipe;<br> char line[1024];<br> unsigned long outlen, linelen;<br> char buf[40];<br><br> result = malloc(1);<br> outlen = 0;<br> sprintf(buf, "make_toast %s", args->args[0]);<br><br> pipe = popen(buf, "r");<br><br> while (fgets(line, sizeof(line), pipe) != NULL) {<br> linelen = strlen(line);<br> result = realloc(result, outlen + linelen);<br> strncpy(result + outlen, line, linelen);<br> outlen = outlen + linelen;<br> }<br><br> pclose(pipe);<br><br> if (!(*result) || result == NULL) {<br> *is_null = 1;<br> } else {<br> result[outlen] = 0x00;<br> *length = strlen(result);<br> }<br><br> return result;<br>}<br> |
Usage:
|
1 |
mysql> call make_toast(300) |
Demo picture (thanks to my colleague Fernando Laudares Camargos), actual video will follow:
Implementation details:
The relay switch powers on the toaster oven, and no modifications are needed to the oven itself. Make sure the timer is set to 30 min initially, the Raspberry Pi/MySQL UDF will now control how long you toast the bread.
The setup wiring is super easy (but may be counterintuitive if you are used to working with Arduino): use the output pins (image) and connect 5v on the PiFace to the “+” sign on the relay switch, and one of the pins to the “-” sign on the relay switch.
Software install
|
1 |
# gpasswd -a mysql gpio<br># gpasswd -a mysql spi |
|
1 |
mysql> call make_toast(300); |
Enjoy your toast when it is hot!