I want to connect multiple arduino's together for home automation.
I'm just a noob and know only the basics of arduino programming.
What I'm looking for is an easy to use I2C library to copy variables to other arduino's.
Something that looks as simple like this...
Transmit int i to arduino id 4.
And on the receiving arduino something like...
Receive int i from arduino id 4 or something.
Just something much easier than the standard I2C library.
Or maybe a verry clear tutorial on how to copy variables.
It can be easier with an library specially made for copying variables from one arduino to another I think.
From what I have seen you need maybe 40 lines of code to copy one variable. And when you want to copy one more variable you need to modify allot of code.
I seems like so much code can be done in the background.
The only thing I need to define should be...
Recieve or transmit, what variable name, and to what device ID.
If there is not a library made for simple copying variables, maybe someone can point me to an tutorial that does copying of variables to other arduinos in a I2C bus?
Not just sending high of low.
You can't actually copy a random variable, that is a variable called fred on one machine to a variable of the same name on the other because when code is compiled all the variable names are lost. You can only transfer data or blocks of data over.
You are wrong about needing 40 lines of code to copy a value over, how do you think you will do it?
I saw a library made for serial line that copy's variables. Both structures need to be identical for it to work.
I was thinking something similair maybe be made for I2C.
Is it posible to send a variable name, for example...
int tempsensor01 = 26;
int tempsensor02 = 24;
If I want to update both tempsensor01 and 02 to the master from a slave.
Is it posible to send a data stream something like...
"tempsensor01=26,tempsensor02=24."
And make a code on the master that sees the int name, and write's the value after "=", and plit on the ","? Or something like that.
I know it's not the most efficient data stream, but It may make programming allot easier because you never need to adjust the code on the master when I want to transmit one aditional int.
Ideally you'd have the master expecting a certain number of values from the slave, or a data in a certain known format that it can extract one or more values from, and populate the local variable(s).
Is there any reason you're expecting the number of temp sensors (as in your example) to change? If so, is it up to a known maximum number or is the slave device sending values not entirely in your control?
Also, why is the name of the variable important to the point you'd like to maintain it across the slave and master?
I'm not sure if this is exactly what you're looking for but I did write an article about how to make an I2C slave device using an Arduino. It talks about how to set up a basic register map and sending data when requested, etc... You can read it here DssCircuits.com is for sale | HugeDomains . Maybe you can pull some code from it.
I noticed you're building some kind of home automation system, are you aware that basic I2C communication is typically restricted to short distances such as several inches and if you're lucky a couple of feet? If you're hard wiring the Arduinos further apart you may want to look at a more stable communication protocol.
There is a library around that essentially does this in the background. Both Arduinos have to have the same data structure and it syncs them by moving blocks of bytes to and fro. I think it's only good for 2 Arduinos though.
Anyway this is probably not the best way to do a HA system I think. Even if you get it working there will be race and timing issues because it takes time for a variable to replicate around a system.
If I2C only goes a little distance, it is less interresting to me.
For my home automation I have one arduino mega that controls everything.
But to make wiring easier I want for example to use a single arduino to monitor for posible roof leakage. These are 8 water sensors. I wanted to place one arduino close to the roof, and with 2 wires pass through all 8 variables to overwrite the variables on the arduino mega that can decide how to act on the detections.
I was also thinking of making my own protocol for this. Something slow, but it should work...
on the "roof arduino" use one digital out pin. And on the Mega one digital in.
The "roof arduino" sends for example 1sec high for sensor 1, than 1 sec low, than high of low depending on the sensor value.
For sensor two start with 2 sec high, pauze with 1 sec low, than high of low depending on the sensor value again.
It's like some kind of morse code. But this should be easy with long distances and it gets the information acros.
De Mega just listen on that pin and knows that pin is only for water sensors.
Just an example, the idea is sound I think.
I was just hoping on something a bit easier.
Maybe serial communication is an option for longer distances.
But you can't use multiple arduino's communication over the same serial line.
So I'm limited to the serial connections on the MEGA.
Serial can handle long distances with cat-6 cable?
Why does I2C not work? Higher speeds?
If I use serial, whats the easies way to sync some variables?
I' am just a noob, maybe using different serials is easier/faster than using multidrop? (don't know what that is).
And can I use softserial when all the serials are used? No distance difference, or do I need to set a lower speed for 10-20m?
Serial can handle long distances with cat-6 cable?
Yes but for a real long distance you need to change the voltage to RS232 levels.
Yes I2C is much faster.
You can't sync variables as such as I have said. You have a block of data and you sync that. Then you can make that block into a structure.
However you should have no need to syncronise variables, it is a bad idea to try. Write your code so that this is not needed. As you are a beginner you might think this is the solution but it is not.
Oke, so I need to find some tutorials that explain how to bundle all the variables into one data block.
Send it, receive it and break it down and update the new variable data.
Only downside is that you need to send all data every time, not only the updated data because the block that is received need to be the same size.
And when I want to add a new variable to be send I need to change both master and slave code because of the new block size.
You need to look at struts, these are basic C constructs.
You will not find an Arduino tutorial on this because it is a stupid thing to try and do. Even an experienced programmer would tie himself in knots trying to do this, you have no chance. But you seem to be fixed on this way of doing things. It is a classic beginners error to think of an overly complex system and then fall over trying to implement it. To ask questions not about how to solve a problem but to cover one aspect of how you think a solution should be.
I am sorry to be brutal but please abandon this idea of synchronising data variables and get a decent system of distributed control.
I'am lost, you say, don't bundle variables to make a data block, than send and divide the data block and write the variables on the master?
For now, all I'm trying to do is to get 6 sensor readings (6 variables) on "arduino 1" to "arduino 2" 15 meters away.
Whats the best and easiest way to do this? (no need for wireless)
What kind of communication, and what kind of programming?
Use serial data, use it like it is used from processing, send either all six values or a tag saying what data sensor it is following by the data and then an end of package byte. There are lots of examples of this PaulS posts these regularly.
Only downside is that you need to send all data every time
That's not the only downside, but certainly a major one. If you wind up with 50 sensors around the house you could be trying to sync 100 or even 3-400 bytes depending on the nature of the values. All this for a single sensor change.
And then there are the timing issues I mentioned before.
And who is going to make decisions based on this data, why does the door bell Arduino need to know the temperature of the swimming pool (for example).
Normally you have a central controller that does know all this stuff and if that's the case the knowledge is not needed elsewhere.
That's not to say that there aren't reasons and ways to distribute the intelligence, but I'm guessing that's way above you at this point.
Now the simplest method is to use X point-to-point serial (using RS232 or RS485) links, and for a small system there's no reason not to do this. You can use Softserial for this purpose, it will be slower but that's not an issue here I would say.
The problem with this approach is that it's not very scalable, ie if the system grows its hard to add nodes. But it is very simple so I would advocate that rather than a multi-drop network.
So you can send variables in a string like
<12.34.56.78.90.AB>
For example
Serial.print ("<12.34.56.78.90.AB>");
Where each value between the dots is for a sensor. When you receive the string you chop it up into the component parts.
As Mike said there are numerous examples on this forum.
The idea behind working with multiple arduino's is to decrease the amount of wirering.
And to decrease cpu usage.
For example, I want to control all the light with PWM. These are activated by motionsensors or switches.
When I want smooth transitions from lightlevels I need allot of cpu power to make all kinds of these smooth transitions at ones.
When I have one arduino with 4 TLC5940's, I only have to send the target brightness, and the arduino animate it in more detail, not delaying any other code on the master.
(thats the idea, not sure if it works this way).
And when I have 10 smoke detectors, on arduino can read them out all the time, but only transmit to the master when something changes, and not let the master keep checking them all the time.
But I have lots of shift registers for unlimited digital outputs, TLC5940's for unlimited PWM outputs for lighting and planning to buy two Centipede's for 128 aditional digital inputs, to make one "giga arduino" with lots of I/O's.
Only looking for a way to have some aditional analog inputs for temp and light sensors.