control plug outlet via web with arduino

Hello every1 i try to make a project for my univercity and i have a few questions.My project is that i controll a plug outlet by a web page.I use arduino-0022 and i have put this code on the arduino UNO:

#include <SPI.h>
#include <WString.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 240 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
int speakerPin = 2; // speaker connected to digital pin 9

int ledPin = 8; // LED pin

int inputPin3 = 3; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

boolean LEDON = false; //LED status flag
String readString = String(30); //string for fetching data from address

void setup(){
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(ledPin, OUTPUT);
pinMode(inputPin3, INPUT); // declare pushbutton as input
Serial.begin(9600);
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output

}

void loop(){

Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 30)
{
readString.concat(c); //store characters to string
}
if (c == '\n') { //if HTTP request has ended

ledstatus(); //LED Status Sub
htmlcontent(); //LED Html Content Sub

readString=""; //clearing string for next read
client.stop(); //stopping client
delay(100);
}
}
}
}
}

void htmlcontent(){
Client client = server.available();
client.println("HTTP/1.1 200 OK"); // now output HTML data starting with standart header
client.println("Content-Type: text/html");
client.println();
client.print("");
client.println("Activate Outlet

");
client.print("Outlet status: ");
if (LEDON)
client.println("ON");
else
client.println("OFF");
client.println("

");

client.println("");
}

void ledstatus(){
if(readString.equalsIgnoreCase("L=1")) //lets check if LED should be lighted
{
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
}

and my problem is that i try to controll a ssr relay drom arduino to enable outlet,but i think that i need a tranzistor...but i dont know what type of tranzistor i need and how to conect in the way from arduino to relay.

and my problem is that i try to controll a ssr relay drom arduino to enable outlet,but i think that i need a tranzistor...but i dont know what type of tranzistor i need and how to conect in the way from arduino to relay.

Whether you need a transistor at all, and what kind you need, if you need one, would depend on which SSR you have. Post a link to that, and we can tell you what else, if anything, you need, and how to connect it.

I have buy an ssr relay from siemens with ( 4-30 Vdc controll and 230 Vac 4A output )

http://common.leocom.jp/datasheets/156412_27638.pdf

this one but with 230 Vac not 400Vac

Loot at the top of page 3. It shows that the minimum control voltage (that needed to activate the relay) is either 2.5V or 4V, depending on exactly which SSR you have. Since you were not explicit, we can't tell you specifically which voltage you need to supply.

Not that it really matters, as the UNO will supply 5V, which exceeds the turn-on voltage of any of the relays listed.

Note also that the minimum control current is 3 mA, and that the maximum is 30mA, regardless of model. The UNO can safely supply 30mA of current from a digital pin.

So, you can simply connect pin 3 of the SSR to a digital pin, and connect pin 4 of the SSR to ground. Connect the device to be controlled to pins 1 and 2 of the SSR.

Set the digital pin HIGH to turn the device on, and LOW to turn it off.

No transistors required.

Thank you very much for help. :slight_smile:

i wont to ask on more thing. i would apreciate if you take a look at the programm that i wright before.I upload it in my arduino (arduino uno,software 0022) and i didnt take voltage from pin8 and the led status didnt change from off to on on the site when i click to change it.

and i didnt take voltage from pin8 and the led status didnt change from off to on on the site when i click to change it.

I don't understand the first part of this statement. Pin 8 is defined as an output pin. It may not ever be set HIGH.

You have a Serial.begin() statement, but no Serial.print() statements. Print what you get back from the client, if anything.

     if (readString.length() < 30)

Since readString is a String object, and can grow itself to accommodate additional characters (though not very efficiently), why are you limiting the string to 30 characters? Let it grow to at least 100.

I change it to 100 but still nothing change.I am not good at programming and this is my first project that has web controll and i create this prog just by trying take ideas from other projects i saw.Do you have any link of an example of controling outlet from web page because i cant find something like that.Or did you know how can i make the programm to work.I just wont to controll a digital output from web and i cant :~

What is your client receiving?
if (client.available()) {
char c = client.read();
Serial.print(c);
if (readString.length() < 30)

It receives:

GET / HTTP/1.1
GET /?L=1 HTTP/1.1

It receives:

GET / HTTP/1.1
GET /?L=1 HTTP/1.1

Well, then, this code:

if(readString.equalsIgnoreCase("L=1")) //lets check if LED should be lighted

will always fail. You need to see if the returned string contains L=1, not is L=1.

befor i make some changes to the code that i send you i have my program like this:

#include <WString.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 240 }; // ip in lan
byte gateway[] = { 122, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(80); //server port
int speakerPin = 2; // speaker connected to digital pin 9

int ledPin = 8; // LED pin

int inputPin3 = 3; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status

boolean LEDON = false; //LED status flag
String readString = String(30); //string for fetching data from address

void setup(){
Ethernet.begin(mac, ip, gateway, subnet);
pinMode(ledPin, OUTPUT);
pinMode(inputPin3, INPUT); // declare pushbutton as input
Serial.begin(9600);
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output

}

void loop(){

Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 30)
{
readString.append(c); //store characters to string
}
if (c == '\n') { //if HTTP request has ended

ledstatus(); //LED Status Sub
htmlcontent(); //LED Html Content Sub

readString=""; //clearing string for next read
client.stop(); //stopping client
delay(100);
}
}
}
}
}

void htmlcontent(){
Client client = server.available();
client.println("HTTP/1.1 200 OK"); // now output HTML data starting with standart header
client.println("Content-Type: text/html");
client.println();
client.print("");
client.println("Activate Outlet

");
client.print("Outlet status: ");
if (LEDON)
client.println("ON");
else
client.println("OFF");
client.println("

");

client.println("");
}

void ledstatus(){
if(readString.contains("L=1")) //lets check if LED should be lighted
{
digitalWrite(ledPin, HIGH); // set the LED on
LEDON = true;
}else{
digitalWrite(ledPin, LOW); // set the LED OFF
LEDON = false;
}
}

butthe prog when i verify i take this errors :
sketch_apr11a.cpp: In function 'void loop()':
sketch_apr11a:44: error: 'class String' has no member named 'append'
sketch_apr11a.cpp: In function 'void ledstatus()':
sketch_apr11a:82: error: 'class String' has no member named 'contains'

so after some reserc i change this two commands:
1)append----->concat
2)contains----->equalsignorecase
3)and i put at the start: #include <spi.h>

so did i make a mistake? and if i did what commands i have to put to change the ones they bring errors?

The indexOf() function most nearly replaces the contains() function.

Now i take this on serial monitor:
GET / http/1.1
GET /?L=1 HTTP/1.1
and the outlet status on web page starts from on and never change even if i activate outlet.

I put this command if(readString.indexOf(“L=1″)>0) and it works perfectly thank you very much for your help. :smiley: :smiley: :smiley: