Hi, community of Arduino.
I'm kind of new to this kind of project so any help is appreciated.
I am making a wifi power outage detector, I have the schematic plan and the program is already made, but I need to know if I'm doing it right or if there was a better way for doing it.
I intend on using a ATMEGA328P-PU, Adafruit's CC3000 wifi breakout and a LM35DZ for the temp
I'm not sure if using a relay will effectively switch between the mains power and the battery. The 4.8V battery is supposed to work directly with the chip and it will be more effective than using a voltage regulator. Sleeping saves the battery. It will connect to a mobile wifi access and upload the power status and the temperature to Xively.
The schematic is attached to the post.
Here's the code:
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <stdlib.h>
#include <string.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <debug.h>
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, SPI_CLOCK_DIV2);
#define WLAN_SSID "SSID"
#define WLAN_PASS "****"
#define WLAN_SECURITY WLAN_SEC_WPA2
#define WEBSITE "api.xively.com"
#define API_key "yourAPIKey"
#define feedID "yourFeedID"
uint32_t ip;
#define redLed 6
#define greenLed 7
#define yellowLed 8
#define powerPin 2
#define tempPin A5
#define sleepTime 450
volatile byte wdt = 0;
void setup(){
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
pinMode(powerPin, INPUT);
pinMode(tempPin, INPUT);
Serial.println(F("\nInitializing..."));
if(!cc3000.begin()){
Serial.println(F("Couldn't begin()! Check your wiring!"));
while(1);
}
setup_watchdog();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
delay(10);
}
void loop(){
int powerState = digitalRead(powerPin);
while(powerState == true){
digitalWrite(greenLed, HIGH);
digitalWrite(yellowLed, HIGH);
ip = 0;
Serial.print(WEBSITE);Serial.print(F("->"));
while(ip == 0){
if(!cc3000.getHostByName(WEBSITE, &ip)){
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
int tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024.0;
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(tempC) + "\"},"
+ "{\"id\" : \"Power Status\",\"current_value\" : \"" + String(powerState) + "\"}]}";
length = data.length();
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if(client.connected()){
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
}
while(client.connected()){
while(client.available()){
char c = client.read();
Serial.print(c);
}
}
client.close();
cc3000.disconnect();
digitalWrite(yellowLed, LOW);
delay(300000);
digitalWrite(greenLed, LOW);
}
while(powerState == false){
digitalWrite(yellowLed, HIGH);
digitalWrite(redLed, HIGH);
ip = 0;
Serial.print(WEBSITE);Serial.print(F("->"));
while(ip == 0){
if(!cc3000.getHostByName(WEBSITE, &ip)){
Serial.println(F("Couldn't resolve!"));
}
delay(500);
}
cc3000.printIPdotsRev(ip);
int tempC = (5.0 * analogRead(tempPin) * 100.0) / 1024.0;
int length = 0;
String data = "";
data = data + "\n" + "{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" : \"Temperature\",\"current_value\" : \"" + String(tempC) + "\"},"
+ "{\"id\" : \"Power Status\",\"current_value\" : \"" + String(powerState) + "\"}]}";
length = data.length();
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if(client.connected()){
Serial.println("Connected!");
client.println("PUT /v2/feeds/" + String(feedID) + ".json HTTP/1.0");
client.println("Host: api.xively.com");
client.println("X-ApiKey: " + String(API_key));
client.println("Content-Length: " + String(length));
client.print("Connection: close");
client.println();
client.print(data);
client.println();
}
while(client.connected()){
while(client.available()){
char c = client.read();
Serial.print(c);
}
}
client.close();
cc3000.disconnect();
digitalWrite(yellowLed, LOW);
digitalWrite(redLed, LOW);
system_sleep();
}
}
void system_sleep() {
for(int x = 0; x < sleepTime; x++){
digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(10);
byte dummy_0 = DDRB;
byte dummy_1 = DDRD;
byte dummy_2 = TWSR;
byte dummy_3 = TWBR;
byte dummy_4 = SPCR;
byte dummy_5 = SPSR;
PRR = 0xEF;
ADCSRA |= (0<<ADEN);
DDRB = 0x00;
DDRD = 0x00;
sleep_enable();
sleep_mode();
// sleeping ...
sleep_disable();
wdt=0;
PRR = 0x00;
ADCSRA |= (1<<ADEN);
DDRB = dummy_0;
DDRD = dummy_1;
TWSR = dummy_2;
TWBR = dummy_3;
SPCR = dummy_4;
SPSR = dummy_5;
}
}
void setup_watchdog() {
MCUSR = 0x00;
WDTCSR |= (1<<WDCE) | (1<<WDE);
WDTCSR = (1<<WDP3) | (1<<WDP0);
WDTCSR |= (1<<WDIE);
}
ISR(WDT_vect) {
wdt++;
}
Thank you!
