dont know if this is the right place to ask but i guess.
I've got a question about the i2c protocol.
My idea,
I want to hold a master arduino which controls multiple slaves.
The master is communicating with the World Wide Web through a password protected website.
From the website i do click a relais and then the master must communicate with the slave device with that relais to toggle.
But i cant really find out if its possible to send a request to a slave adress with a extra data so it just toggle 1 relais instead of all the relais who are on the arduino..
Is that possible and could maybe someone tell me how., or knows a way to do it on a another way.
Thanks for your response.
But thats what my problem is..
If you start with the master a i2c request to a number ( slave id) then the slave just do a programmed loop..
What i want is that i send the slave id + a extra command and that the slave reads that he is that ID and he reacts to the extra command so a relais 1 goes in.
example:
master got 3 slaves on the line.
Slave 1 got 3 relais on his atmega.
// not tested but this is how I would start it, you should get the idea.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.beginTransmission(2);
Wire.write(10); // command code or other command.
int rv = Wire.endTransmission();
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(5000);
}
// also not tested
void setup()
{
Wire.begin(2); // join i2c bus with address #2
}
void loop()
{
while(Wire.available() == 0); // wait for request
int command = Wire.read();
switch(command)
{
case 10: // handle command 10
Wire.write(sensor.value);
break;
case 11: // handle command 11
RelayON();
Wire.write(relay.state);
break;
case 12:
RelayOFF();
Wire.write(relay.state);
break;
default:
break;
}
}
robtillaart:
OK I got it,
needs some checking to do
BRB
// not tested but this is how I would start it, you should get the idea.
#include <Wire.h>
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire.beginTransmission(2);
Wire.write(10); // command code or other command.
int rv = Wire.endTransmission();
while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(5000);
}
// also not tested :)
void setup()
{
Wire.begin(2); // join i2c bus with address #2
}
void loop()
{
while(Wire.available() == 0); // wait for request
int command = Wire.read();
switch(command)
{
case 10: // handle command 10
Wire.write(sensor.value);
break;
case 11: // handle command 11
RelayON();
Wire.write(relay.state);
break;
case 12:
RelayOFF();
Wire.write(relay.state);
break;
default:
break;
}
}
YAY! thats it! gonna test it tomorrow on my workplace but i didnt see the read and write! thanks alot !
was already thinking of using rs232 and then all the rx's of the slaves togheter and then let the master talk with a write 1 for slave 1 and next the command i want. but this is better! thanks alot!
Well, im trying at the moment but something happens what i dont understand.
i did abit of changing the code to get it working but its not really getting that far.
i've changed the slave code to
#include <Wire.h>
void setup()
{
Wire.begin(2); // join i2c bus with address #2
Serial.begin(9600);
}
void loop()
{
while(Wire.available() == '0'); // wait for request
int command = Wire.read();
Serial.println(command);
}
i changed so i could read out what is comming from the read. also i did change the
while(Wire.available() == '0'); // wait for request, the 0 between ' ' =)
otherwise it isnt even working..
I do get out of my serial this:
-1
-1
-1
and futher.
Maybe you know what happens?
EDIT:
after long searching i still doesnt know what happens.. it looks like they dont understand each other.. and it gets wrong on the slave side, i guess.. i can only see what he does after the wire.read cause i can print that in serial... does anyone knows an other idea?