====== Arduino board to board kommunikáció ====== Gyakran bele lehet szaladni az Arduino-kkal abba a helyzetbe, hogy egy kártya már nem elég egy adott feladat megoldásához, vagy csak egyszerűen valamely funkcionalitást tovább szeretnénk bővíteni, de nincs hová, mert elfogyott a szabad pin, vagy a memória. Az is előfordul, hogy mondjuk van egy Wemos board-unk, ami ugye tud Wifi-t, meg internet oldalt generál, de a kapcsolók, szenzorok beolvasásával nem ezt akarjuk terhelni (mert úgy sincs elég pin hozzá). Ilyenkor mellé csaphatunk egy "buta" Uno-t, ami futtatja a helyi programot, és a Wemos csak a Wifi-vel lesz elfoglalva. ===== Master - slave I²C egyirányú kommunikáció ===== A megvalósításhoz ezesetben mindkét board-on 2-2 analóg-jelre lesz szükségünk, ezeket az alábbiak szerint kell "drótoznunk": {{:wiki:arduino:master_slave_board.png?600|Master - slave I²C szinkron soros protokoll}} ==== A master board küldő-programja ==== // Wire Master Writer // by Nicholas Zambetti // Demonstrates use of the Wire library // Writes data to an I2C/TWI slave device // Refer to the "Wire Slave Receiver" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include void setup() { Wire.begin(); // join i2c bus (address optional for master) } byte x = 0; void loop() { Wire.beginTransmission(8); // transmit to device #8 Wire.write("x is "); // sends five bytes Wire.write(x); // sends one byte Wire.endTransmission(); // stop transmitting x++; delay(500); } ==== A slave board fogadó programja ==== // Wire Slave Receiver // by Nicholas Zambetti // Demonstrates use of the Wire library // Receives data as an I2C/TWI slave device // Refer to the "Wire Master Writer" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include void setup() { Wire.begin(8); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output } void loop() { delay(100); } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while (1 < Wire.available()) { // loop through all but the last char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character } int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer } forrás: https://www.arduino.cc/en/Tutorial/MasterWriter ===== Master - slave UART kommunikáció ===== A [[hu:arduino:start#arduino_soros_kommunikacio|soros (UART) kommunikáció]] megvalósításhoz itt mindkét board-on a tx - rx digitális pin-ekre lesz szükségünk. A vezetékezést ebben az esetben így kell megvalósítani: {{:wiki:arduino:master_slave_board_uart.png?600|Master - slave UART kommunikáció}} ==== Küldő board programja ==== //Sender Code char str[4]; void setup() { Serial.begin(9600); } void loop() { int value=1234; //this would be much more exciting if it was a sensor value itoa(value, str, 10); //Turn value into a character array Serial.write(str, 4); } ==== Fogadó board programja ==== //Receiver Code char str[4]; void setup() { Serial.begin(9600); Serial1.begin(9600); } void loop() { int i=0; if (Serial1.available()) { delay(100); //allows all serial sent to be received together while(Serial1.available() && i<4) { str[i++] = Serial1.read(); } str[i++]='\0'; } if(i>0) { Serial.println(str,4); } } forrás: http://robotic-controls.com/learn/arduino/arduino-arduino-serial-communication