Air Conditioner Controlled via PC or Internet - Logic and Code

Basic Theory

I knew I was going to mount a solenoid in front of the air conditioner's power button. To help me figure out what else I would need, I put together a quick flowchart in my head.

Remote AC Flowchart 1

I knew I would need four basic parts.

To keeps things simple, I would remotely log in to my PC as my Internet connection. The PC would be connected to the microcontroller through a USB port (technically, a USB to UART converter). The solenoid wasn't a problem. I had to think of a good power sensor.

I could try to sense when there's current flowing through the power cable. How can I do that?

OK. So current sensing isn't the way to go. How about something intrinsic to the air conditioner itself? Well, the fan is always on as long as the air conditioner is on. How can I sense air flow?

Sensing Air Flow

Although air flow sensors do exist, I wanted something fast and cheap. Quagmire - Giggity

My first thought was to use some sort of pinwheel (original picture source).

Pinwheel Girl

The sensor electronics could be a shaft encoder, IR beam, etc. It can be anything that converts rotational motion into some sort of electrical signal. I thought to use a switch similar to baseball cards in the spokes of a bicycle tire. I could attach electrical ground to the tire through its axle, and I would use aluminum foil as the "baseball card."

Then, I realized the tire was completely unnecessary. Why not use aluminum foil on both sides of the switch?

Foil Airflow Switch

Using the Switch

Equipped with my free switch, I went back to my mental flowchart, inserting the logic the switch. A new problem had to be accounted for: if I poll the switch while the air conditioner is on, the pieces of foil may be touching due flapping in the wind. I could take multiple samples, but that only decreases inaccuracy without removing inaccuracy entirely.

To solve this, I would poll the switch. If the switch is closed, I would turn use the microcontroller's interrupt-on-change feature. After waiting a couple of seconds, I would check the IOC flag. If it is set, then the switch has toggled, possibly more than once, since I first polled it. I don't need to enable the interrupt to use this; I can just check the interrupt flag after a few seconds.

Remote AC Flowchart 2

Feedback

Finally, I want to know what happened. If the device didn't work or isn't connected, I want to know. This is more for peace of mind than anything else.

Once everything is done, check the switch again using the same method as above. Then, tell the me via the PC what happened.

Remote AC Flowchart 3

Source Code

The microcontroller C code is written for the PIC16F88 using the SourceBoost BoostC compiler. I didn't draw schematics because I think the comments are sufficient. Also note that I used the internal weak pull-up resistor for the foil switch; do not connect an external resistor like the diagram above. The pin connected to the solenoid/actuator is connected to a solenoid driver. The solenoid draws too much current to connect it directly to the microcontroller.

The PC source code is written for Linux using the GCC compiler (tested on Fedora 14). Remember, to access the serial port on Linux, your user account has to be part of the "dialout" group. Otherwise, you'll have to run this as root (not recommended).