Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

2020/03/30

Laser printer vs UPS

As an extra wrinkle to the previous post, the new Lexmark is on an UPS. Yes, I can hear you screaming "NEVER PUT A LASER PRINTER ON AN UPS" but I'm a professional, I know what I'm doing. Specifically, I over-engineered my solution.

The problem with a laser printer on a UPS is you might be tempted to print something during a black out. And your UPSes battery will never supply the current your fuser needs. My solution is to build a plug with relays connected to an arduino connected to a computer connected to the UPS. When power goes out, apcupsd on the computer runs a small script that sends a command to the arduino via USB to turn off the relays. The arduino also has a momentary switch. When I push the switch, the arduino turns the relays on. I also have small script that sends the command to turn the relays on.

I probably could have avoided the arduino and used a transistor latch. Power off turns the latch and relays off. Only a button press turns the latch and relays on. But then I couldn't do the following.

One annoying """feature""" of the Lexmark is that it has AirPrint and Wi-fi. I have yet to find out how to turn these off. So the printer spends most of its time turned off. So I wrote a CUPS backend that checks if the printer is on, sends the arduino the "turn on" command if it isn't, then chains to the normal socket backend to actually send the data to the printer.

#!/bin/bash

HWEL=10.0.0.68
LOG=/tmp/hwel-driver.log

if [[ $# == 1 ]] ; then
    exec /usr/lib/cups/backend/socket "$@"
fi

function aping () {
    local host=$1
    if ping -c 1 -q $host >/dev/null ; then
        return 0
    fi
    return 1
}

function ping_wait () {
    local host=$1
    while true ; do
        if ping -c 1 -q $host >/dev/null ; then
            return
        fi
        sleep 2
    done
}


status=$(/sbin/apcaccess status localhost:3552 | grep STATUS | cut -d: -f 2)
if [[ $status =~ ONLINE ]] ; then
    echo "INFO: hwel battery status=$status" | tee -a $LOG >&2
else
    echo "ERROR: hwel battery status=$status" | tee -a $LOG >&2
    exit 17
fi

aping $HWEL || sudo /home/fil/bin/hwel-on
ping_wait $HWEL

export DEVICE_URI=socket://$HWEL:9100
exec /usr/lib/cups/backend/socket "$@"

I put this script in /usr/lib/cups/backend/hwel and tell CUPS to use a new URL.

lpadmin -p hwelraw -v hwel://hwel.localdomain:9100

Yes, my printer is called Hwel. Yes, I name all my printers after Discworld dwarfs.

2011/08/19

Arduino on CentOS 5

So I bit the bullet and bought an Arduino UNO. The packaging should have a label on it "WARNING: BY OPENING THIS PACKAGE YOU RECOGNISE YOU WILL GET NO USEFUL WORK DONE TODAY."



All I wanted was to get one LED to flash. But that requires a avr-cpp-g++, which isn't available for CentOS 5. GRRRR!



So I wasted to much time getting the CentOS 6 SRPMs and getting them to compile. But you may fetch them from my psuedo-repo.

While time was a wasting, a kind soul on IRC sent me a hex file for bitlash which I managed to upload to the board. This allowed me to flash a few LEDs and get on with my work.

Writing code for the Arduino Uno in CentOS 5


RPMs will need from the psuedo-repo
avr-libc-1.7.0-1.noarch.rpm

avr-libc-docs-1.7.0-1.noarch.rpm
avr-binutils-2.20-2.x86_64.rpm
avrdude-5.10-1.x86_64.rpm
avr-gcc-4.5.0-2.x86_64.rpm
avr-gcc-c++-4.5.0-2.x86_64.rpm
gmp-4.3.1-7.x86_64.rpm
gmp-devel-4.3.1-7.x86_64.rpm
gmp-static-4.3.1-7.x86_64.rpm
libmpc-0.8-3.x86_64.rpm
libmpc-devel-0.8-3.x86_64.rpm
mpfr-2.4.1-6.x86_64.rpm
mpfr-devel-2.4.1-6.x86_64.rpm

You will also need arduino-0022.tar.gz, which I installed in /opt/arduino.

Now the IDE works, but it's in Java. Which hates me, hates Linux and I hate Java just as much, just for good measure I also hate IDEs. Working from two tutorials and with some slamming my head into the table, I got it working. The trick was massaging Johan's Makefile into something that worked. I also had to patch /opt/arduino/hardware/arduino/cores/arduino/wiring_private.h.

To start a new project, lets say "blink2"
mkdir blink2

cd blink2
wget http://awale.qc.ca/CentOS/rhel5/Makefile.arduino -O Makefile
wget http://awale.qc.ca/CentOS/rhel5/resetArduino.pl
touch blink2.pde

Now add your code to blink2.pde.

And here is my first Arduino program. It's a 6 LED chaser. It would be more, but I can only find 6 LEDs right now. Pins 13-9 are red, pin 8 is green. The green light stays on twice as long as the others.
#define DELAY 100


int state;
void setup(void) {
int pin;
state = 1;
for(pin=13; pin >=8; pin-- )
pinMode(pin, OUTPUT);
}

void set_state(void) {
int pin;
int mask = 1;
for(pin=13; pin >=8; pin-- ) {
digitalWrite( pin, (state & mask) );
mask <<= 1;
}

}

void loop(void) {

if( state == 1 ) {
state = 3;
}
else {
state = state << 1;
}
/* 0001 1000 = 18 */
/* 0011 0000 = 30 */
if( state == 0x60 ) { /* 0110 0000 = 60 */
state = 0x20;
set_state();
delay( DELAY*4 );
state = 1;
}
/* 0100 0010 = 42 */
else if( state == 0x42 ) {
state = 3;
}
set_state();

delay(DELAY);
}


Now compile and upload it:
make && sudo make upload