====== Arduino Board-to-Board-Kommunikation ====== Sie können häufig auf Arduinoes stoßen, wenn eine Karte nicht mehr ausreicht, um eine bestimmte Aufgabe zu lösen, oder Sie möchten einfach einige Funktionen weiter erweitern, haben aber keinen Platz, weil Ihnen der freie Pin oder Speicher ausgeht. Es kommt auch vor, dass wir ein Wemos-Board oder ESP8266 haben, das Wifi kennt und eine Internetseite generiert, aber wir möchten es nicht durch Scannen der Schalter und Sensoren laden (weil wir auch nicht genug Pins dafür haben). In diesem Fall können wir ein "dummes" Uno daneben drücken, auf dem das lokale Programm ausgeführt wird, und Wemos / ESP8266 ist nur mit Wifi beschäftigt. ===== Master-Slave-I²C-Einwegkommunikation ===== In diesem Fall benötigen wir 2-2 analoge Signale auf beiden Karten, um sie zu implementieren. Wir müssen sie wie folgt verdrahten: {{:wiki:arduino:master_slave_board.png?600|Master-Slave-I²C-Einwegkommunikation}} ==== Senderprogramm der master board ==== // 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); } ==== Empfängerprogramm der slave board ==== // 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-Kommunikation ===== Um die serielle (UART) Kommunikation zu implementieren, benötigen wir hier die digitalen tx - rx - Pins auf beiden Karten. Die Verdrahtung muss in diesem Fall wie folgt implementiert werden: {{:wiki:arduino:master_slave_board_uart.png?600|Master-Slave-UART-Kommunikation}} ==== Board-Programm senden ==== //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); } ==== Host Board Programm ==== //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); } } Quelle: http://robotic-controls.com/learn/arduino/arduino-arduino-serial-communication