Simple AC Monitor for Arduino 

A major part of my solar project is to provide emergency lighting for when the power goes out. It doesn't happen very often. Unfortunately, every time the commercial AC goes away (due to storms, etc.), my flashlights are all somewhere else - usually somewhere that would require a flashlight in order to find them.

My thinking was that it would be awesome if I could simply have a couple of strategically placed DC lights that hooked into the solar controller batteries. It would be even better if they would just magically come on when the power went off. Sounds simple, right? Well, I thought so, too. Since I've been enjoying the Arduino stuff so much, and it would be cool to actually make something useful with one, I thought this would be an ideal project.

So off I went to ask my pal Google what it takes to monitor the AC. Google was somewhat helpful, but it looked like everything that had been built was relying on relays, opto-isolation circuits, a used space shuttle, or some other gizmo that wasn't in my junk drawer. I found one interesting thread that talked about using a wall wart, which felt right to me, but I didn't have anything suitable there either.

Then I plugged in my cell phone to charge it up...

There it was! I had a nice regulated 5v (presumably) power source that connected right into the AC. The Fluke quickly revealed it to be a 5.07vDC source (close enough). You should check yours before you hook it up, too, by the way.

So I stripped a USB cable (lots of extras around here), hooked the ground into the Arduino, hooked the positive power lead into an analog pin, and started playing around. Eventually, I ended up with a monitor that has LEDs for status and a really loud buzzer. I've stripped away all but the essentials for you guys so that you can build whatever you like into yours.

Here's how it looks:



... and here's a basic sketch:


/*
Simple AC Power Monitor
J. McNeely
2014-03-16

This sketch allows a cheap and simple way of detecting an AC power
failure. I looked all over for a simple way to do this, and found
that this is very inexpensive, and appears to be quite accurate.

In order for this idea to work, the Arduino must be powered by
batteries or alternative sources (mine is the solar gear).

This sketch is just a basic starter setup to be incorporated into
whatever larger project you have in mind.

Best of Luck!
Joe

*/

#define ALARMPIN 2
#define POWERSENSEPIN A1

float voltage = 5;
float voltageThreshold = 4.5;

float powerVal = 1023;
float powerValPrv = powerVal;
int powerState = 1;
int powerStatePrv = powerState;
long lastPowerDebounceTime = 0;
long debouncePowerDelay = 5000; // Delay before power status is considered to have changed.
int alarmState = LOW;
int alarmFlag = 0;


void setup() {
pinMode(POWERSENSEPIN, INPUT_PULLUP);
pinMode(ALARMPIN, OUTPUT);
powerVal = analogRead(POWERSENSEPIN);
voltage = powerVal * (5.0 / 1023.0);
if (voltage > voltageThreshold) {
powerState = 1;
alarmState = HIGH;
powerValPrv = powerVal;
} else {
powerState = 0;
alarmState = LOW;
powerValPrv = powerVal;
}
digitalWrite(ALARMPIN, alarmState)
Serial.begin(9600);
Serial.println("Commercial Power Monitor");
Serial.println("------------------------");
Serial.println();
}

void loop()
{
if ((millis() - lastPowerDebounceTime) > debouncePowerDelay) {
powerVal = analogRead(POWERSENSEPIN);
voltage = powerVal * (5 / 1023.0);
if (voltage > voltageThreshold) {
powerState = 1;
setAlarm(0);
} else {
powerState = 0;
setAlarm(1);
}
}

if (powerState != powerStatePrv) {
lastPowerDebounceTime = millis();
setPowerChange();
}
}

void setPowerChange() {
// This is a stub. More complex actions may be hooked into this function.
powerStatePrv = powerState;
Serial.print("\nCommercial Power Changed: ");
if (powerState == 1) {
Serial.println("Power Restored");
} else if (powerState == 0) {
Serial.println("Power Failure");
}
}

void setAlarm(int alarmAction) {
// This is a stub. The ALARMPIN could be connected to an LED, buzzer, relay, whatever.
if (alarmAction == 0) {
alarmFlag = 0;
alarmState = LOW;
digitalWrite(ALARMPIN,alarmState);
} else if (alarmAction == 1) {
alarmFlag = 1;
alarmState = HIGH;
digitalWrite(ALARMPIN,alarmState);
}
}


To test it, just plug the USB charger into a power strip and open the serial monitor in the IDE. Switch off the power strip, and wait for the delay, then you'll see the power failure message in the serial monitor. Turn the power strip back on, and you'll see the restored message. Simple, eh?

Happy Hacking!
Joe
[ add comment ] ( 137 views ) permalink
Another Arduino Sketch for the DHT-11 

If you would prefer a web-based sketch, I've also been playing around with this one. This may require a 2560. I haven't tried it on an Uno...

This one also has a telnet interface. There is no real need for it, I suppose. I just built it because it sounded interesting.

#include <Ethernet.h>
#include <SPI.h>
#include "DHTSensor.h"

// Network config
byte mac[] = { 0xAA, 0x00, 0x00, 0xE1, 0x00, 0x72};
byte ip[] = {192, 168, 100, 72};
byte gateway[] = {192, 168, 100, 254};
byte subnet[] = {255, 255, 255, 0};

// Device config
#define DEVID "F2"
#define SENSORNAME "Upstairs"

// Other globals
#define VERSION "1.3"
#define TELNETPORT 2323
#define WEBPORT 8080
#define DHTTYPE DHT11
#define DHTPIN 2
#define textBuffSize 20 //length of longest command string plus two spaces for CR + LF
String rcvdCmd = "";
int charsReceived = 0;
boolean connectFlag = 0;
unsigned long timeOfLastActivity; //time in milliseconds of last activity
unsigned long allowedConnectTime = 300000; //five minutes
EthernetServer telnetServer(TELNETPORT);
EthernetClient telnetClient = 0;
EthernetServer webServer(WEBPORT);
EthernetClient webClient = 0;
DHT dht(DHTPIN, DHTTYPE);
float humid;
float tempf;

// State Variables
char* commandText = "";
char* devId = DEVID;
char* sensorName = SENSORNAME;

void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600);
telnetServer.begin();
webServer.begin();
Serial.println("Remote Environment Monitor");
Serial.print("Version ");
Serial.println(VERSION);
Serial.println();
Serial.print("Telnet service listening on port ");
Serial.println(TELNETPORT);
Serial.print("Web service listening on port ");
Serial.println(WEBPORT);
Serial.println();
dht.begin();
}

void loop()
{
if (telnetServer.available() && !connectFlag) {
connectFlag = 1;
telnetClient = telnetServer.available();
telnetClient.println("\nRemote Environment Monitor");
telnetClient.print("Device: ");
telnetClient.print(devId);
telnetClient.print(" Sensor: ");
telnetClient.println(sensorName);
telnetClient.println();
showPrompt();
}

if (telnetClient.connected() && telnetClient.available()) getReceivedText();

if(connectFlag) checkConnectionTimeout();

webClient = webServer.available();

if (webClient) {
sendWebPage();
}
}

void showPrompt()
{
timeOfLastActivity = millis();
telnetClient.flush();
charsReceived = 0;
telnetClient.println();
telnetClient.print(devId);
telnetClient.print("> ");
}

void checkConnectionTimeout()
{
if(millis() - timeOfLastActivity > allowedConnectTime) {
telnetClient.println();
telnetClient.println("\n*** SESSION TIMED OUT ***\n");
telnetClient.stop();
connectFlag = 0;
}
}

void getReceivedText()
{
char c;
int charsWaiting;

charsWaiting = telnetClient.available();
do {
c = telnetClient.read();
if(c != 0x0d) {
rcvdCmd += c;
charsReceived++;
charsWaiting--;
}
}
while(charsReceived <= textBuffSize && c != 0x0d && charsWaiting > 0);

if(c == 0x0d) {
parseReceivedText();
showPrompt();
}

if(charsReceived >= textBuffSize) {
telnetClient.println();
showErrorMessage();
showPrompt();
}
}

void parseReceivedText()
{
if (rcvdCmd == "help") {
showHelpMessage();
} else if (rcvdCmd == "show version") {
telnetClient.print("\nVersion ");
telnetClient.println(VERSION);
telnetClient.println();
} else if (rcvdCmd == "quit") {
closeConnection();
} else if (rcvdCmd == "show reading") {
showReading();
} else if (rcvdCmd == "reset") {
telnetClient.println("\n--- Reset in 5 Seconds");
closeConnection();
delay(5000);
softwareReset();
} else {
showErrorMessage();
}
rcvdCmd = "";
}

void showErrorMessage()
{
telnetClient.println(">>> Unrecognized Command");
}

void closeConnection()
{
telnetClient.println("\n*** DISCONNECT ***\n");
telnetClient.stop();
connectFlag = 0;
}

void showHelpMessage()
{
telnetClient.println("\nSupported commands:\n");
telnetClient.println(" reset Restart the firmware");
telnetClient.println(" show reading Show current temperature/humidity");
telnetClient.println(" show version Show version number");
telnetClient.println(" help Print this help message");
telnetClient.println(" quit Exit telnet session");
telnetClient.println();
}

void showReading() {
humid = dht.readHumidity();
tempf = dht.readTemperature(1);
telnetClient.println("\nCurrent Reading");
telnetClient.println("================================");
telnetClient.print("Device ID : ");
telnetClient.println(devId);
telnetClient.print("Sensor Name : ");
telnetClient.println(sensorName);
telnetClient.print("Temperature : ");
telnetClient.println(tempf);
telnetClient.print("Humidity : ");
telnetClient.println(humid);
telnetClient.println("================================\n");
}

void sendWebPage() {
humid = dht.readHumidity();
tempf = dht.readTemperature(1);
boolean currentLineIsBlank = true;
while (webClient.connected()) {
if (webClient.available()) {
char c = webClient.read();
if (c == '\n' && currentLineIsBlank) {
webClient.println("HTTP/1.1 200 OK");
webClient.println("Content-Type: text/html");
webClient.println();
webClient.print("Device:");
webClient.print(devId);
webClient.print(",SensorName:");
webClient.print(sensorName);
webClient.print(",Temperature:");
webClient.print(tempf);
webClient.print(",Humidity:");
webClient.println(humid);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
webClient.stop();
}

void softwareReset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}


[ add comment ] ( 845 views ) permalink
Arduino Sketch for DHT11 Temperature Sensor 

Hi kids. So, I have this thing where I like to know exactly how cold it is in the garage before I head out there. Since I've been wanting to play around with the arduino stuff, this seemed like a good project.

This code pushes the sensor readings over MQTT, which I can then pick up and incorporate into the various places I want it. The code is running on an arduino Uno with a wiznet ethernet shield. I'm running the ethernet through a bridged wifi client until I get some network in the garage.

Here's the sketch:
/*

Environment Monitor
Build for Garage - 1 DHT11 Sensor
2014-01-23

*/

#include <stdarg.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include "DHTSensor.h"
#include <ICMPPing.h>

// Fixed Definitions
#define VERSION "1.2-Garage"
#define DEVID "F3"
#define SENSORNAME "Outdoors"
#define CMDQ "net-command"
#define DHTTYPE DHT11
#define DHTPIN 2

// Network config
byte mac[]= {0xAA, 0x00, 0x00, 0xE1, 0x00, 0x73};
byte ip[]= {192, 168, 100, 73};
byte gateway[]= {192, 168, 100, 254};
byte subnet[]= {255, 255, 255, 0};
byte mqtt_broker[]= {192, 168, 100, 150};

// Function declarations
String splitString(String s, char parser,int index);
void callback(char* topic, byte* payload, unsigned int length);

// Instances
EthernetClient ethClient;
PubSubClient client(mqtt_broker, 1883, callback, ethClient);
DHT dht(DHTPIN, DHTTYPE);

// sockets
SOCKET pingSocket = 3;

// State Variables
char* commandText = "";
char* deviceId = DEVID;
char* cmdTopic = CMDQ;
char* sensorName = SENSORNAME;
char* sensorTemp = "";
char* sensorRh = "";
int sampleDelay = 30000;
long lastSampleTime = 0;
char pingBuffer[256];
int pingDelay = 10000;
long lastPingTime = 0;
int netUp = 1;

// printf work-around
char* p(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(tmp, 128, fmt, args);
va_end (args);
return tmp;
}

void softwareReset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile (" jmp 0");
}

void clientConnect() {
if (client.connect(deviceId)) {
commandText = p("Status:%s:Connected",deviceId);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
commandText = p("Status:%s:%i.%i.%i.%i",deviceId,ip[0],ip[1],ip[2],ip[3]);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
client.subscribe(cmdTopic);
commandText = p("Status:%s:Subscribed to %s",deviceId,cmdTopic);
Serial.println(commandText);
} else {
Serial.println("Connection failed to MQTT");
netUp = 0; // Set network down - loop will retry connect
}
}

// Program setup
void setup() {
delay(200);
Ethernet.begin(mac,ip,gateway,subnet);
delay(200);

dht.begin();

Serial.begin(9600);

Serial.println();
Serial.println("Environment Monitor");
Serial.println(VERSION);
Serial.println();

clientConnect();

Serial.println();
}

char* deblank(char* input)
{
char *output=input;
int k,j;
for (k = 0, j = 0; i<strlen(input); k++,j++)
{
if (input[k]!=' ')
output[j]=input[k];
else
j--;
}
output[j]=0;
return output;
}

// splitString function
String splitString(String s, char parser,int index){
String rs='\0';
int parserIndex = index;
int parserCnt=0;
int rFromIndex=0, rToIndex=-1;

while(index>=parserCnt){
rFromIndex = rToIndex+1;
rToIndex = s.indexOf(parser,rFromIndex);

if(index == parserCnt){
if(rToIndex == 0 || rToIndex == -1){
return '\0';
}
return s.substring(rFromIndex,rToIndex);
}
else{
parserCnt++;
}
}
return rs;
}

void sendHumidity1() {
float hum = dht.readHumidity();
char hum_c[5] = "";

dtostrf(hum,5, 1, hum_c);
sensorRh = hum_c;

commandText = p("Status:%s:RH=%s",deviceId,deblank(hum_c));
client.publish(cmdTopic,commandText);
Serial.println(commandText);
}

void sendTemperature1() {
float tempf = dht.readTemperature(1);
char tempf_c[5] = "";

dtostrf(tempf,5, 1, tempf_c);
sensorTemp = tempf_c;

commandText = p("Status:%s:Temp=%s",deviceId,deblank(tempf_c));
client.publish(cmdTopic,commandText);
Serial.println(commandText);
}

// Callback function
void callback(char* topic, byte* payload, unsigned int length) {
//convert byte to char
payload[length] = '\0';
String strPayload = String((char*)payload);
String strPayloadOrig = strPayload;
strPayload += ":"; // splitString has issues...
String receiveId = splitString(strPayload,':',0);
String receiveCmd = splitString(strPayload,':',1);
String receiveChn = splitString(strPayload,':',2);

if (strPayloadOrig != commandText) {
if (receiveId == deviceId) {
if (receiveCmd == "REQ") {
commandText = p("Status:%s:OK",deviceId);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
}
if (receiveCmd == "RST") {
commandText = p("Status:%s:Reset in 5 seconds",deviceId);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
delay(5000);
softwareReset();
}
if (receiveCmd == "VER") {
commandText = p("Status:%s:%s",deviceId,VERSION);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
}
}
}
}

void loop() {

client.loop();

if ((millis() - lastPingTime) > pingDelay) {
if ( netUp == 0 ) {
delay(5000);
clientConnect(); // Wait 5 seconds, and re-init client
}
ICMPPing ping(pingSocket);
if (ping(4, mqtt_broker, pingBuffer)) {
netUp = 1;
} else {
Serial.println("NetFail");
netUp = 0;
}
lastPingTime = millis();
}

if ((millis() - lastSampleTime) > sampleDelay) {
if ( netUp == 1 ) {
commandText = p("Status:%s:Name=%s",deviceId,sensorName);
client.publish(cmdTopic,commandText);
Serial.println(commandText);
sendTemperature1();
sendHumidity1();
lastSampleTime = millis();
}
}
}



To use it, you'll need a few libraries. The DHT sensor library is from Adafruit. You can read about it here. The MQTT piece uses the PubSubClient library, found here. To handle network glitches, it's using ping to check connectivity, and will reconnect if it can't find the server. The ping library is here. All-in-all, it may not be the most sophisticated sketch, but this one is getting the job done. Good luck!
[ add comment ] ( 270 views ) permalink
The Solar Project 

Now I'll be honest with you, boys and girls. I had only the most vague of thoughts on the topic of alternate energy. Basically, I just sort of wondered what I would do for power if it went off and stayed off for any length of time. That was it. I had gone no further...

Now, if you know me, you know that I love gadgets. You also know that I love money. Consequently, I put a good deal of time and effort into getting more of the former without giving up much of the latter. This is where the Harbor Freight parking lot sales come into the conversation. You see, I'm a regular at those. Now I know some of you are shaking your heads and muttering about how you won't even touch it if it doesn't say "snap-on" in there somewhere, but you have to understand, I'm cheap.

At yesterday's sale, they had one of these:

Solar Panel Kit

It was a return, and the note on the box said "LED doesn't work". I asked the guy if he knew anymore about it, but he basically just confirmed that where normally the LED works, this particular one doesn't. He was very polite about it. So by virtue of the regular sale discount, parking lot sale discount, returned item discount, and probably harmonic convergence, the final price was $75. Like any good DIYer, I thought "I bet I can fix it". In this case I was right! Hey, don't act so surprised... Oh OK, act surprised. Heck, I was.

So now I have a solar project. I'll put the details here. Maybe we can learn together.

Check back later, if you want....
[ add comment ] ( 268 views ) permalink
Removing Moss from Your Roof 

Hello, fellow DIYers!

I just wanted to pass this along, since I've tried it, and it works.

If your roof has overhanging tree limbs, or just plain doesn't get a lot of sun, you may end up with a moss problem. I didn't even know I had one until my insurance company sent me a very unfriendly note about getting rid of it "or else". In my case, they also insisted upon having me hire a contractor to certify the work, blah, blah, blah, etc, etc. Well, I wasn't too pleased with the whole idea. So I took a look at the moderately small amount of moss in question, and asked my friend the internet what I should do about it. Here's the short version:

1. Put on clothes that you wouldn't mind throwing away when you're done. I'll explain later... You should also take a look at what's at the end of your downspouts. If it's alive, you may not want to use this method, or you may want to find a way to direct them to somewhere that doesn't have living stuff.

2. Get your garden hose up there and give it a good spray! Be careful if you need a ladder for this. I wouldn't do the power-washer thing, but a fair amount of pressure will lift up a lot of the moss right away (without lifting up the shingles too much).

3. Mix household bleach and clean water in a sprayer. My sprayer was 10 bucks from Harbor Freight during the parking lot sale. Bleach is about $2 a gollon where I live.

4. Carefully climb up and get close enough to give the moss a REALLY good soak with the bleach and water. I mean really. The sprayer should have an adjustable nozzle. I set mine for "long stream", not "fine spray" so I wouldn't have to climb all the way up. You're likely to get a good deal of blow-back if it's windy. That's why you wanted those old, crappy clothes instead of your Sunday best.

5. Wait a few minutes. I gave it something like 45 minutes to sit.

6. Pull the garden hose back up, and rinse well. The rest of the moss came right off for me. At this point, you've probably guessed why we cared about what was below the downspout. All that bleachy water will be draining there as we hose her down. I'm no expert, but I'm guessing plants don't take well to that kind of treatment.

At the end of the day, I had no moss at all. I also had a new insurance company (one that never saw a hint of moss on my roof), and maybe I made some contractor out there buy his own lunch (no worries, I've got lots of stuff I'm happy to pay him for - just not this one).

That's the whole story, kids. Remember: Be careful when you're playing with bleach on a ladder.

[ add comment ] ( 791 views ) permalink

<Back | 1 | 2 | 3 | Next> Last>>