Components and supplies
Arduino UNO
Apps and platforms
Blynk
Arduino IDE
Project description
Code
casa.ino
arduino
This do everything
1/* 2Little Home by Filippo 3This code is licensed under Creative Commons BY license. If reuse you should link the page where you find this code. 4 5Have a nice making! 6*/ 7 8/************************************************************** 9 * 10 BLYNK comments 11 12 * 1. Optional, but recommended. 13 * Connect additional USB-serial adapter to see the prints. 14 * 15 * 2. Edit auth token and upload this sketch. 16 * 17 * 3. Run the script (script located in "scripts" folder of library root, 18 * e.g. 'blynk-library/scripts') for redirecting traffic to server: 19 * 20 * for Windows: 21 * 1. Open cmd.exe 22 * 2. write : (your way to blynk-ser.bat folder) example: "cd C:\\blynk-library-0.3.1\\blynk-library-0.3.1\\scripts" 23 * 3. write : "blynk-ser.bat -c COM4" (where COM4 is port with your Arduino) 24 * 4. And press "Enter" , press "Enter" and press "Enter" 25 * 26 * for Linux and OSX: 27 * 28 * ./blynk-ser.sh (may need to run with sudo) 29 * 30 * You can specify port, baud rate, and server endpoint like this: 31 * ./blynk-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port> 32 * 33 * For instance : 34 * ./blynk-ser.sh -c /dev/ttyACM0 -b 9600 -s blynk-cloud.com -p 8442 35 * 36 * Run blynk-ser.sh -h for more information 37 * 38 * Be sure to select the right serial port (there may be multiple). 39 * 40 * Attention! 41 * Arduino IDE may complain with "programmer is not responding". 42 * You need to terminate script before uploading new sketch. 43 * 44 * 4. Start blynking! :) 45 * 46 **************************************************************/ 47 48#include <SoftwareSerial.h> 49SoftwareSerial SwSerial(2, 3); // RX, TX 50#define BLYNK_PRINT SwSerial 51#include <BlynkSimpleSerial.h> 52#include <SimpleTimer.h> 53 54 55SimpleTimer timer; 56 57 58// You should get Auth Token in the Blynk App. 59char auth[] = "a8659c1939b645a88afeca5928a0118a"; 60 //variables 61int rpin=A0, doorsensor=A1, button=A2; //which pin read? 62int value; //read value 63int parsed; //value after map 64int setmin=790, setmax=960; //Values for fotoresistence (low=light) 65unsigned long cmil, pmil=0; //current and previous millis 66int trpin=5,tr; 67int buzz=4; 68 69class led{ 70 private: 71 int pin; 72 bool state; 73 unsigned long current; 74 unsigned long previous; 75 public: 76 led(int a){pin=a;pinMode(pin, OUTPUT);state=0;previous=millis();}; 77 led(){}; 78 void set_on(){digitalWrite(pin,HIGH);state=1;}; 79 void set_off(){digitalWrite(pin,LOW);state=0;}; 80 void blynk(); 81 void set_pin(int a){pin=a;pinMode(pin,OUTPUT);}; 82}; 83 84class alarm{ 85 private: 86 led alarmled; 87 bool state; 88 int sensor; 89 public: 90 alarm(int a, int b){alarmled.set_pin(a); sensor=b; state=0;}; 91 void set_on(){alarmled.blynk();state=1;}; 92 void set_off(){alarmled.set_off();state=0;}; 93 bool is_on(){return state;}; 94 bool warning(){if(state==1&&analogRead(sensor)>10) return 1; else return 0;}; 95}; 96 97void led::blynk(){ 98 current=millis(); 99if(state) if((current-previous)>800) {set_off(); previous=current;} 100if(!state) if((current-previous)>800) {set_on(); previous=current;} 101} 102 103alarm door= alarm(9,doorsensor); 104led test= led(13); 105 106 107 void fakeloop(){ 108 test.blynk(); 109 if(digitalRead(button)) door.set_on(); else door.set_off(); 110 //light 111 cmil=millis(); 112 value=analogRead(rpin); 113 parsed=map(value,setmin,setmax,0,1023); 114 parsed=constrain(parsed,0,1023); //no overscale 115tr=map(parsed,0,1023,0,255); 116analogWrite(trpin,tr); 117if(door.warning()) {tone(buzz,440); Blynk.email("fili27182@gmail.com", "#arduino", "Warning, door's open!!"); Blynk.notify("Warning"); 118} else noTone(buzz); 119//if(cmil-pmil>900){Serial.print("read:");Serial.println(value);Serial.print("write:");Serial.println(tr);Serial.println(); pmil=cmil;} 120//delay(1); 121} 122 123void setup() 124{ 125 SwSerial.begin(9600); 126 Blynk.begin(auth); 127 timer.setInterval(1000L, fakeloop); 128} 129 130void loop() 131{ 132 Blynk.run(); 133 timer.run(); 134} 135 136
Downloadable files
Fritzing
http://fritzing.org/projects/little-home
Fritzing
http://fritzing.org/projects/little-home
Comments
Only logged in users can leave comments