The document describes an Arduino sketch that uses an Ethernet shield to control 8 relays remotely via a web interface. The sketch allows each relay to be toggled on or off by sending HTTP requests to the Arduino's IP address followed by a number 1-8. For example, requesting 192.168.1.111/$1 would turn on relay 1 while 192.168.1.111/$2 would turn it off.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0 ratings0% found this document useful (0 votes)
306 views46 pages
Proiecte Arduino
The document describes an Arduino sketch that uses an Ethernet shield to control 8 relays remotely via a web interface. The sketch allows each relay to be toggled on or off by sending HTTP requests to the Arduino's IP address followed by a number 1-8. For example, requesting 192.168.1.111/$1 would turn on relay 1 while 192.168.1.111/$2 would turn it off.
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 46
PIR alarm
// Uses a PIR sensor to detect movement, buzzes a buzzer
// more info here: http://blog.makezine.com/projects/pir-sensor-arduino- alarm/ // email me, John Park, at jp@jpixl.net // based upon: // PIR sensor tester by Limor Fried of Adafruit // tone code by michael@thegrebs.com
int ledPin = 13; // choose the pin for the LED int inputPin = 2; // choose the input pin (for PIR sensor) int pirState = LOW; // we start, assuming no motion detected int val = 0; // variable for reading the pin status int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() { pinMode(ledPin, OUTPUT); // declare LED as output pinMode(inputPin, INPUT); // declare sensor as input pinMode(pinSpeaker, OUTPUT); Serial.begin(9600); }
void loop(){ val = digitalRead(inputPin); // read input value if (val == HIGH) { // check if the input is HIGH digitalWrite(ledPin, HIGH); // turn LED ON playTone(300, 160); delay(150);
if (pirState == LOW) { // we have just turned on Serial.println("Motion detected!"); // We only want to print on the output change, not state pirState = HIGH; } } else { digitalWrite(ledPin, LOW); // turn LED OFF playTone(0, 0); delay(300); if (pirState == HIGH){ // we have just turned of Serial.println("Motion ended!"); // We only want to print on the output change, not state pirState = LOW; } } } // duration in mSecs, frequency in hertz void playTone(long duration, int freq) { duration *= 1000; int period = (1.0 / freq) * 1000000; long elapsed_time = 0; while (elapsed_time < duration) { digitalWrite(pinSpeaker,HIGH); delayMicroseconds(period / 2); digitalWrite(pinSpeaker, LOW); delayMicroseconds(period / 2); elapsed_time += (period); } }
Charlieplexing 12 LED s
#define A 12 #define B 11 #define C 10 #define D 9
void setup() { pinMode(4, OUTPUT); // REQUIRED for eKitsZone.com ENC28J60 shield!!! digitalWrite(4, HIGH); // Same as above.
// Serial.begin(57600); ether.setup(mac, ip, port); rsize = sizeof(relay) / 2; for (i = 0; i < rsize; i = i + 1) { pinMode(relay[i], OUTPUT); } runtime = 0; starttime = millis(); }
void loop() { int c; char * param; char * params;
if (params = ether.serviceRequest()) { if (strstr(params, "?status")) { for (i = 0; i < rsize; i = i + 1) { ether.print(digitalRead(relay[i])); } } else { if((strlen(params) > 3) && (!(strstr(params, "favicon.ico")))) { runtime = 0; param = strtok(params, "?&"); while (param != NULL) { if (strstr(param, "p=")) { i = atoi(param + 2) - 1; } else if (strstr(param, "c=")) { c = atoi(param + 2); if (c == 1) { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } digitalWrite(relay[i], HIGH); } else { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } } } else if (strstr(param, "t=")) { runtime = (unsigned long)(atoi(param + 2)) * 1000; if (runtime < 15000) { runtime = 15000; } starttime = millis(); } param = strtok(NULL, "& "); } if (runtime == 0) { runtime = 60000; // 60 sec. default run time (takes 10 seconds for valve to open). starttime = millis(); } }
ether.print("<tt>Network Relay System\n<br>\n"); for (i = 0; i < rsize; i = i + 1) { ether.print("<br>Port #"); ether.print(i+1); ether.print(": <a href='?p="); ether.print(i+1); if (digitalRead(relay[i]) == 1) { ether.print("&c=0'>Off</a> in "); ether.print((runtime - (millis() - starttime))/1000); ether.print(" seconds.\n"); } else { ether.print("&c=1'>On</a>\n"); } } ether.print("<p><a href='/'>Refresh</a>,<a href='/?status'>Status</a>\n"); } ether.respond(); }
if ((unsigned long)(millis() - starttime) > runtime) { for (c = 0; c < rsize; c = c + 1) { digitalWrite(relay[c], LOW); } } }
Web Control Relay - Arduino
Using the same hardware and an ethernet sheild I found and modified an ethernet sketch to control the relays. This sketh controls each rely with a specific URL. I have the IP set to 192.168.1.111. The URL for pin 2 is 192.168.1.111/$1 and 192.168.1.111/$2 to toggle it on or off. pin3 is $3 and $4 pin4 is $5 and $6 pin5 is $7 and $8 pin6 is $9 and $0 pin7 is $A and $B pin8 is $C and $D pin9 is $E and $F
I didn't write this sketch. I just made a few mods. All credit goes to the original author.
/* Web Server Demo thrown together by Randy Sarafan
Allows you to turn on and off an LED by entering different urls.
To turn it on: http://your-IP-address/$1
To turn it off: http://your-IP-address/$2
Circuit: * Ethernet shield attached to pins 10, 11, 12, 13 * Connect an LED to pin D2 and put it in series with a 220 ohm resistor to ground
Based almost entirely upon Web Server by Tom Igoe and David Mellis
Edit history: created 18 Dec 2009 by David A. Mellis modified 4 Sep 2010 by Tom Igoe
*/
#include <SPI.h> #include <Ethernet.h>
boolean incoming = 0;
// Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 }; IPAddress ip(192,168,1,111); //<<< ENTER YOUR IP ADDRESS HERE!!!
// Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80);
// start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.begin(9600); }
void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply
//reads URL string from $ to first blank space if(incoming && c == ' '){ incoming = 0; } if(c == '$'){ incoming = 1; }
//Checks for the URL string $1 or $2 if(incoming == 1){ Serial.println(c);
if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); } }
Program de test modul 8 relee int Relay1 = 13; int Relay2 = 12; int Relay3 = 11; int Relay4 = 10; int Relay5 = 9; int Relay6 = 8; int Relay7 = 7; int Relay8 = 6; void setup() { pinMode(Relay1, OUTPUT); //Set Pin12 as output pinMode(Relay2, OUTPUT); pinMode(Relay3, OUTPUT); pinMode(Relay4, OUTPUT); pinMode(Relay5, OUTPUT); pinMode(Relay6, OUTPUT); pinMode(Relay7, OUTPUT); pinMode(Relay8, OUTPUT); } void loop() { digitalWrite(Relay1, HIGH); //Turn off relay delay(2000); digitalWrite(Relay1, LOW); //Turn on relay delay(2000); digitalWrite(Relay2, HIGH); //Turn off relay delay(2000); digitalWrite(Relay2, LOW); //Turn on relay delay(2000); digitalWrite(Relay3, HIGH); //Turn off relay delay(2000); digitalWrite(Relay3, LOW); //Turn on relay delay(2000); digitalWrite(Relay4, HIGH); //Turn off relay delay(2000); digitalWrite(Relay4, LOW); //Turn on relay delay(2000); digitalWrite(Relay5, HIGH); //Turn off relay delay(2000); digitalWrite(Relay5, LOW); //Turn on relay delay(2000); digitalWrite(Relay6, HIGH); //Turn off relay delay(2000); digitalWrite(Relay6, LOW); //Turn on relay delay(2000); digitalWrite(Relay7, HIGH); //Turn off relay delay(2000); digitalWrite(Relay7, LOW); //Turn on relay delay(2000); digitalWrite(Relay8, HIGH); //Turn off relay delay(2000); digitalWrite(Relay8, LOW); //Turn on relay delay(2000); }
Control relay module with ethernet shield and some nice html shit
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
void loop() { int val = analogRead(pir); float val1 = val*5.00/1023.00; digitalWrite(led, LOW); if (val <650) { Serial.print(val); Serial.print("/1023 = "); Serial.print(val1); Serial.println("V - No motion"); //if the value read is low, there was no motion
} else { Serial.print(val); Serial.print("/1023 = "); Serial.print(val1); Serial.println("V - Motion!"); //if the value read was high, there was motion digitalWrite(led, HIGH); } delay(500); }
Pentru senzori de temperatura #include <OneWire.h> #include <DallasTemperature.h>
// Data wire is plugged into port 10 on the Arduino #define ONE_WIRE_BUS 10 #define TEMPERATURE_PRECISION 12
// Setup a oneWire instance to communicate with any OneWire devices (not ju st Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire);
// arrays to hold device addresses DeviceAddress rightThermometer, midleThermometer, leftThermometer;
void setup(void) { // start serial port Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library use by niq_ro"); Serial.println("---------------------------------------------------");
// method 1: by index if (!sensors.getAddress(rightThermometer, 0)) Serial.println("Unable to find address for Device 0"); if (!sensors.getAddress(midleThermometer, 1)) Serial.println("Unable to find address for Device 1"); if (!sensors.getAddress(leftThermometer, 2)) Serial.println("Unable to find address for Device 2");
// show the addresses we found on the bus Serial.print("Device 0 Address: "); printAddress(rightThermometer); Serial.println();
// set the resolution to 9..12 bit sensors.setResolution(rightThermometer, TEMPERATURE_PRECISION); sensors.setResolution(midleThermometer, TEMPERATURE_PRECISION); sensors.setResolution(leftThermometer, TEMPERATURE_PRECISION);
// function to print a device address void printAddress(DeviceAddress device) { for (uint8_t i = 0; i < 8; i++) { // zero pad the address if necessary if (device[i] < 16) Serial.print("0"); Serial.print(device[i], HEX); } Serial.println(" - "); }
// function to print the temperature for a device void printTemperature(DeviceAddress device) { float tempC = sensors.getTempC(device); Serial.print("Temp C: "); if (tempC == -127.00) { Serial.print("Error"); } else { Serial.print(tempC); Serial.print(" Temp F: "); Serial.print(DallasTemperature::toFahrenheit(tempC)); } } // function to print a device's resolution void printResolution(DeviceAddress device) { Serial.print("Resolution: "); Serial.print(sensors.getResolution(device)); Serial.println(); }
// main function to print information about a device void printData(DeviceAddress device) { Serial.print("Address: "); printAddress(device); Serial.print(" "); printTemperature(device); Serial.println(); }
// call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus Serial.println("---------------------------------------"); Serial.println(" "); Serial.print("Requesting temperatures..."); sensors.requestTemperatures(); Serial.println("DONE"); Serial.println("---------------------------------------"); Serial.println(" ");
/* AC Light Control Updated by Robert Twomey <rtwomey@u.washington.edu> Thanks to http://www.andrewkilpatrick.org/blog/?page_id=445 and http://www.hoelscher-hi.de/hendrik/english/dimmer.htm adapted sketch by niq_ro from http://www.tehnic.go.ro http://www.niqro.3x.ro http://nicuflorica.blogspot.com
IR Remote Kit Test Uses YourDuino.com IR Infrared Remote Control Kit 2 http://arduino-direct.com/sunshop/index.php?l=product_detail&p=153 based on code by Ken Shirriff - http://arcfn.com Get Library at: https://github.com/shirriff/Arduino-IRremote
Bluetooth: // adapted sketch from http://english.cxem.net/arduino/arduino4.php */
#include <LiquidCrystal.h> // use LiquidCrystal.h library for alphanumerical display 1602 LiquidCrystal lcd(13,12,11,10,9,8); /* ------------------- | LCD | Arduino | ------------------- LCD RS pin to digital pin 13 | RS | D13 | LCD Enable pin to digital pin 12 | E | D12 | LCD D4 pin to digital pin 11 | D4 | D11 | LCD D5 pin to digital pin 10 | D5 | D10 | LCD D6 pin to digital pin 9 | D6 | D9 | LCD D7 pin to digital pin 8 | D7 | D8 | LCD R/W pin to ground | R/W | GND | ------------------- */
#include "IRremote.h" //-----( Declare Constants )----- int receiver = 7; // pin 1 of IR receiver to Arduino digital pin 7 //-----( Declare objects )----- IRrecv irrecv(receiver); // create instance of 'irrecv' decode_results results; // create instance of 'decode_results' //-----( Declare Variables )-----
#include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0; // Variable to use as a counter volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero int AC_pin = 3; // Output to Opto Triac int buton1 = 4; // first button at pin 4 int buton2 = 5; // second button at pin 5 int dim2 = 0; // led control int dim = 128; // Dimming level (0-128) 0 = on, 128 = 0ff int pas = 8; // step for count; // version: 4m7 (15.04.2013 - Craiova, Romania) - 16 steps, 4 button & LED blue to red (off to MAX) // version: 7m6.1 (23.01.2014 - Craiova, Romania) - 16 steps, 2 button & LCD1602
int freqStep = 75; // This is the delay-per-brightness step in microseconds.
char incomingByte; // incoming data from serial 9bluetooth)
void setup() { // Begin setup
Serial.begin(9600); // initialization
irrecv.enableIRIn(); // Start the IR receiver (classic remote)
pinMode(buton1, INPUT); // set buton1 pin as input pinMode(buton2, INPUT); // set buton1 pin as input pinMode(AC_pin, OUTPUT); // Set the Triac pin as output attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need Timer1.attachInterrupt(dim_check, freqStep); // Use the TimerOne Library to attach an interrupt
lcd.begin(16, 2); // set up the LCD's number of columns and rows: lcd.clear(); // clear the screen lcd.setCursor(2, 0); // put cursor at colon 0 and row 0 lcd.print("16 steps AC"); // print a text lcd.setCursor(0, 1); // put cursor at colon 0 and row 1 lcd.print("dimmer for bulb"); // print a text delay (3000); lcd.clear(); // clear the screen lcd.setCursor(1, 0); // put cursor at colon 0 and row 0 lcd.print("this sketch is"); // print a text lcd.setCursor(1, 1); // put cursor at colon 0 and row 1 lcd.print("made by niq_ro"); // print a text delay (3000); lcd.clear(); // clear the screen }
void zero_cross_detect() { zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured i=0; digitalWrite(AC_pin, LOW); }
// Turn on the TRIAC at the appropriate time void dim_check() { if(zero_cross == true) { if(i>=dim) { digitalWrite(AC_pin, HIGH); // turn on light i=0; // reset time step counter zero_cross=false; // reset zero cross detection } else { i++; // increment time step counter } } }
//-----( Declare User-written Functions )----- void translateIR() // takes action based on IR code received
// describing Car MP3 IR codes { switch(results.value) { case 0xFFA25D: Serial.println(" CH- "); break;
case 0xFF629D: Serial.println(" CH "); break;
case 0xFFE21D: Serial.println(" CH+ "); break;
case 0xFF22DD: { Serial.println(" PREV "); dim=128; } break;
case 0xFF02FD: { Serial.println(" NEXT "); dim=0; } break;
case 0xFFC23D: Serial.println(" PLAY/PAUSE "); break;
case 0xFFE01F: { Serial.println(" VOL- "); if (dim<127) { dim = dim + pas; if (dim>127) { dim=128; // in vechiul sketch era 127 } } } break;
case 0xFFA857: { Serial.println(" VOL+ "); { if (dim>5) { dim = dim - pas; if (dim<0) { dim=0; // in vechiul sketch era 1 } } } } break;
if(incomingByte == 's') { //step down if (dim>5) { dim = dim - pas; if (dim<0) { dim=0; } } }
if(incomingByte == 'w') { // power is 100% dim=0; } if(incomingByte == 'z') { // power is 0% (off) dim=128; } }
void stelute() { if (dim2<1) lcd.print("----------------"); else if (dim2<9) lcd.print("*---------------"); else if (dim2<17) lcd.print("-*--------------"); else if (dim2<25) lcd.print("--*-------------"); else if (dim2<33) lcd.print("---*------------"); else if (dim2<41) lcd.print("----*-----------"); else if (dim2<49) lcd.print("-----*----------"); else if (dim2<57) lcd.print("------*---------"); else if (dim2<65) lcd.print("-------*--------"); else if (dim2<73) lcd.print("--------*-------"); else if (dim2<81) lcd.print("---------*------"); else if (dim2<89) lcd.print("----------*-----"); else if (dim2<97) lcd.print("-----------*----"); else if (dim2<105) lcd.print("------------*---"); else if (dim2<113) lcd.print("-------------*--"); else if (dim2<121) lcd.print("--------------*-"); else if (dim2>127) lcd.print("---------------*"); }
if (Serial.available() > 0) blustuf(); // if bluetooth is present
if (digitalRead(buton1) == LOW) { if (dim<127) { dim = dim + pas; if (dim>127) { dim=128; // in vechiul sketch era 127 } } } if (digitalRead(buton2) == LOW) { if (dim>5) { dim = dim - pas; if (dim<0) { dim=0; // in vechiul sketch era 1 } } } while (digitalRead(buton1) == LOW) { } delay(10); // waiting little bit... while (digitalRead(buton2) == LOW) { } delay(10); // waiting little bit...
// remote if (irrecv.decode(&results)) // have we received an IR signal? { translateIR(); irrecv.resume(); // receive the next value }
delay (100); lcd.setCursor(2, 0); // put cursor at colon 0 and row 0 lcd.print("power is "); // print a text lcd.print(100*(128-dim)/128); lcd.print("% "); // print a text
lcd.setCursor(0, 1); // put cursor at colon 0 and row 1
dim2=128-dim; // variable use for graphics stelute(); }