11 svi Pametna rasvjeta
Gimnazija Požega
Rasvjetom u kući upravlja mikrokontroler. Koristi tri senzora (PIR, ultrazvučni senzor za mjerenje udaljenosti, i senzor za pritisak). Rasvjetom je također moguće upravljati preko interneta koristeći browser. Dodali smo i mogućnost otvaranja garažnih vrata preko interneta, ali i preko senzora pokreta.
Pokušali smo na najjednostavniji način prikazati spajanje komponenti. Tipkalo nije identičan senzor koji smo koristili, nego se radi o tipu senzora kućne izrade.
Shemu ovoga projekta možete preuzeti ovdje.
#define BLYNK_PRINT /*
#include
Servo myservo;
int servopos = 180; //pocetna vrijednost servo motora (vrata zatvorena)
bool vrata = true; // true – zatvorena, false – otvorena
bool onlinevrata = true; //varijabla koja omogucuje otvaranje vrata preko interneta
bool onlineA = true; //varijabla koja omogucuje paljenje/gasenje svjetla u sobi A preko interneta
bool onlineB = true; //varijabla koja omogucuje paljenje/gasenje svjetla u sobi B preko interneta
bool onlineC = true; //varijabla koja omogucuje paljenje/gasenje svjetla u sobi C preko interneta
bool onlineD = true; //varijabla koja omogucuje paljenje/gasenje svjetla u sobi D preko interneta
#define pr A1 //fotorezistor na pinu A1
#define pirB 0 //senzor pokreta na pinu 0
#define pirD 1 //senzor pokreta na pinu 1
#define ledA 2 //svjetla sobe A na pinu 2
#define ledB 3 //svjetla sobe B na pinu 3
#define ledC 4 //svjetla sobe C na pinu 4
#define ledD 5 //svjetla sobe D na pinu 5
#define trigPin 6 //ultrazvucni senzor udaljenosti ,trgger na pinu 6
#define echoPin 7 //ultrazvucni senzor udaljenosti ,echo na pinu 7
//////////////////////////////////////////////////////////////////////////////////////////////////
#include
#include
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = “ARDUINO”; // your network SSID (name)
char pass[] = “Arduino123”; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
//////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode(pr, INPUT);
pinMode(pirB, INPUT);
pinMode(pirD, INPUT);
pinMode(ledA, OUTPUT);
pinMode(ledB, OUTPUT);
pinMode(ledC, OUTPUT);
pinMode(ledD, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myservo.attach(8); //servo motor za podizanje garaznih vrata
//////////////////////////////////// Kreiraj mrezu (LAN) ////////////////////////////////
//Initialize serial and wait for port to open:
Serial.begin(9600);
/*
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
*/
Serial.println(“Access Point Web Server”);
//pinMode(led, OUTPUT); // set the LED pin mode
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue
while (true);
}
// by default the local IP address of will be 192.168.1.1
// you can override it with the following:
// WiFi.config(IPAddress(10, 0, 0, 1));
// print the network name (SSID);
Serial.print(“Creating access point named: “);
Serial.println(ssid);
// Create open network. Change this line if you want to create an WEP network:
status = WiFi.beginAP(ssid);
if (status != WL_AP_LISTENING) {
Serial.println(“Creating access point failed”);
// don’t continue
while (true);
}
// wait 3 seconds for connection:
delay(3000);
// start the web server on port 80
server.begin();
// you’re connected now, so print out the status
printWiFiStatus();
//////////////////////////////////////////////////////////////////////////////////////////////////////////
}
void printWiFiStatus() { ///////////////////// za otvaranje vrata preko mreze///////////////////////////
// print the SSID of the network you’re attached to:
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(” dBm”);
// print where to go in a browser:
Serial.print(“To see this page in action, open a browser to http://”);
Serial.println(ip);
}
bool distSens(){ // funkcija koja vraca ocitanu udaljenost sa ultrazvucnog senzora udaljenosti
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print(“dist: “);
Serial.println(distance);
if (distance < 17.5){ return (true); } else{ return (false); } //return(distance); } // stanje pir = digiralRead(pir); // stanje pr = analogRead(pr); void garaza(){ // otvori – zatvori vrata if (vrata) { // ako su vrata zatvorena -> otvori
for (servopos = 170; servopos >= 20; servopos–){
myservo.write(servopos);
delay(5);
//Serial.println(“otvaram vrata”);
}
vrata = false;
Serial.println(“vrata otvorena”);
}
else { //vrata su otvorena -> zatvori
for (servopos = 20; servopos <= 170; servopos++){ myservo.write(servopos); delay(5); //Serial.println(“zatvaram vrata”); } vrata = true; Serial.println(“vrata zatvorna”); } } int L[5]= {0, 0, 0, 0, 0}; int br = 0; bool night = true; // true – noc, false – dan bool isNight(){ Serial.print(“pr: “); Serial.println(analogRead(pr)); L[br] = analogRead(pr); br++; if (br >= 5){
br = 0;
}
if (L[0]<820 && L[1]<820 && L[2]<820 && L[3]<820 && L[4]<820){
Serial.println(“<820”); return (true); } if (L[0]>=850 && L[1]>=850 && L[2]>=850 && L[3]>=850 && L[4]>=850){
Serial.println(“>=850”);
return (false);
}
return(night);
} // koristiti: night = isNight() // očitavanja zahtjevaj svakih 500 ms
bool pokretD(){
if (digitalRead(pirD)){
return(true);
}
else{
return (false);
}
}
bool pokretB(){
if (digitalRead(pirB)){
return(true);
}
else{
return (false);
}
}
void loop() {
delay(100);
if (Serial.available() > 0){
char data = Serial.read();
if (data == ‘a’){
onlinevrata = false;
garaza();
}
}
//////////////////////////////////////////////// otvaranje vrata preko mreže /////////////////////////////////////////////////
// compare the previous status to the current status
if (status != WiFi.status()) {
// it has changed update the variable
status = WiFi.status();
if (status == WL_AP_CONNECTED) {
byte remoteMac[6];
// a device has connected to the AP
Serial.print(“Device connected to AP, MAC address: “);
WiFi.APClientMacAddress(remoteMac);
Serial.print(remoteMac[5], HEX);
Serial.print(“:”);
Serial.print(remoteMac[4], HEX);
Serial.print(“:”);
Serial.print(remoteMac[3], HEX);
Serial.print(“:”);
Serial.print(remoteMac[2], HEX);
Serial.print(“:”);
Serial.print(remoteMac[1], HEX);
Serial.print(“:”);
Serial.println(remoteMac[0], HEX);
} else {
// a device has disconnected from the AP, and we are back in listening mode
Serial.println(“Device disconnected from AP”);
}
}
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println(“new client”); // print a message out the serial port
String currentLine = “”; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client’s connected
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == ‘\n’) { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that’s the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what’s coming, then a blank line:
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-type:text/html”);
client.println();
// the content of the HTTP response follows the header:
client.print(“Otvori/Zatvori garazna vrata.
“);
client.print(”
“);
client.print(“Upali/Ugasi svjelo u prostorji A.
“);
client.print(”
“);
client.print(“Upali/Ugasi svjelo u prostorji B.
“);
client.print(”
“);
client.print(“Upali/Ugasi svjelo u prostorji C.
“);
client.print(”
“);
client.print(“Upali/Ugasi svjelo u prostorji D.
“);
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = “”;
}
}
else if (c != ‘\r’) { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was “GET /H” or “GET /L”:
if (currentLine.endsWith(“GET /GH”) && vrata) { // GET /GH otvara garazu
garaza();
onlinevrata = false;
}
if (currentLine.endsWith(“GET /GL”) && !vrata) { // GET /GL zatvara garazu
garaza();
onlinevrata = false;
}
if (currentLine.endsWith(“GET /AH”)) { // GET /AH pali svjetlo u sobi A
digitalWrite(ledA, HIGH);
onlineA = false;
}
if (currentLine.endsWith(“GET /AL”)) { // GET /AH gasi svjetlo u sobi A
digitalWrite(ledA, LOW);
onlineA = true;
}
if (currentLine.endsWith(“GET /BH”)) { // GET /BH pali svjetlo u sobi B
digitalWrite(ledB, HIGH);
onlineB = false;
}
if (currentLine.endsWith(“GET /BL”)) { // GET /BH gasi svjetlo u sobi B
digitalWrite(ledB, LOW);
onlineB = true;
}
if (currentLine.endsWith(“GET /CH”)) { // GET /CH pali svjetlo u sobi C
digitalWrite(ledC, HIGH);
onlineC = false;
}
if (currentLine.endsWith(“GET /CL”)) { // GET /CH gasi svjetlo u sobi C
digitalWrite(ledC, LOW);
onlineC = true;
}
if (currentLine.endsWith(“GET /DH”)) { // GET /DH pali svjetlo u sobi D
digitalWrite(ledD, HIGH);
onlineD = false;
}
if (currentLine.endsWith(“GET /DL”)) { // GET /DH gasi svjetlo u sobi D
digitalWrite(ledD, LOW);
onlineD = true;
}
}
}
// close the connection:
client.stop();
Serial.println(“client disconnected”);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isNight()){ // svjetla rade samo po danu!, provjeravaj pojedinacno senzore i pali/gasi svjetla
if (distSens()){
digitalWrite(ledA, HIGH);
onlineA = true;
}
else if (onlineA){
digitalWrite(ledA, LOW);
}
if (pokretB()){
digitalWrite(ledB, HIGH);
onlineB = true;
}
else if (onlineB){
digitalWrite(ledB, LOW);
}
if (!vrata){
digitalWrite(ledC, HIGH);
onlineC = true;
}
else if (onlineC){
digitalWrite(ledC, LOW);
}
if (pokretD()){
digitalWrite(ledD, HIGH);
onlineD = true;
}
else if (onlineD){
digitalWrite(ledD, LOW);
}
}
else{
digitalWrite(ledA, LOW);
digitalWrite(ledB, LOW);
digitalWrite(ledC, LOW);
digitalWrite(ledD, LOW);
}
// garazna vrata
if (pokretD()){
onlinevrata = true;
}
if (pokretD() && vrata && onlinevrata){
garaza();
}
else if(!pokretD() && !vrata && onlinevrata){
garaza();
}
}
Arduino program ovoga projekta možete preuzeti ovdje.
Projekt su izradili Marin Francuz i Ivor Kakuk uz mentorstvo Sanje Grabusin iz Gimnazije Požega.
Projekt je prijavljen na temu: Internet of Things: Pametna rasvjeta.