Hi,
wie evtl. ein paar mitbekommen haben, möchte ich eine LED an einem Arudino Pro Mini von einem RaspberryPi aus steuern:
(bei Bedarf: s. anderes Thema)
Mit dreamshader Hilfe und der RF24 Library habe ich es hinbekommen, allerdings bin ich nicht ganz zufrieden und ich vermute, dass mein Code absolut schlecht programmiert ist. (Kann leider kein C++, nur ein paar Grundlagen von C#)
Hier mal mein .cpp Programm, welches auf dem Pi läuft.
Da ist das Problem, dass ich momentan noch die Message (11) hardcoded angeben muss.
Verwunderlicher weise, wenn ich statt dem sendMessage(11) sendMessage(argc) eintrage, wird immer eine 2 gesendet, egal was ich angebe.
Mein Ziel ist es dabei, das Programm später so aufzurufen:
./led -code 001 -status 1
Spoiler anzeigen
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <RF24/RF24.h>
using namespace std;
// Hardware configuration
RF24 radio(RPI_BPLUS_GPIO_J8_15,RPI_BPLUS_GPIO_J8_24, BCM2835_SPI_SPEED_8MHZ);
// Radio pipe addresses for the 2 nodes to communicate.
const uint8_t pipes[][6] = {"1Node","2Node"};
//const uint64_t pipes[2] = { 0xABCDABCD71LL, 0x544d52687CLL };
bool sendMessage(int action){
//This function send a message, the 'action', to the arduino and wait for answer
//Returns true if ACK package is received
//Stop listening
radio.stopListening();
unsigned long message = action;
printf("Now sending %lu...", message);
//Send the message
bool ok = radio.write( &message, sizeof(unsigned long) );
if (!ok){
printf("failed...\n\r");
}else{
printf("ok!\n\r");
}
//Listen for ACK
radio.startListening();
//Let's take the time while we listen
unsigned long started_waiting_at = millis();
bool timeout = false;
while ( ! radio.available() && ! timeout ) {
//printf("%d", !radio.available());
if (millis() - started_waiting_at > 1000 ){
timeout = true;
}
}
if( timeout ){
//If we waited too long the transmission failed
printf("Oh gosh, it's not giving me any response...\n\r");
return false;
}else{
//If we received the message in time, let's read it and print it
//radio.read( &outBuffer, sizeof(unsigned long) );
//printf("Yay! Got this response %lu.\n\r",outBuffer);
return true;
}
}
int main(int argc, char** argv){
// Print preamble:
printf("RF24/examples/pingtest/\n");
// Setup and configure rf radio
radio.begin();
// optionally, increase the delay between retries & # of retries
radio.setRetries(15,15);
// Dump the configuration of the rf unit for debugging
radio.printDetails();
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1,pipes[1]);
radio.stopListening();
printf("Now sending...\n");
bool ok = sendMessage(11); //sendMessage(argc); //FEHLER
if (!ok){
printf("failed.\n");
}
radio.startListening();
sleep(1);
return 0;
}
Alles anzeigen
Und hier meine .ino Datei, welche aufm Arduino läuft.
Auch hier ist das Problem, wie bekomme ich das vorher gesendete optimal ausgewertet, nicht so quick&dirty wie ich mit dem /10 und %10
Spoiler anzeigen
#include <SPI.h>
#include "RF24.h"
/****************** User Config ***************************/
bool radioNumber = 1;
RF24 radio(9,10);
short led_code = 1;
short led_port = 5;
/**********************************************************/
byte addresses[][6] = {"1Node","2Node"};
void setup() {
Serial.begin(115200);
Serial.println(F("Remote-LED"));
radio.begin();
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_LOW);
// Open a writing and reading pipe on each radio, with opposite addresses
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
// Start the radio listening for data
radio.startListening();
pinMode(led_port, OUTPUT);
}
void loop() {
unsigned long message;
if( radio.available()){
// Variable for the received timestamp
while (radio.available()) { // While there is data ready
radio.read( &message, sizeof(unsigned long) ); // Get the payload
}
radio.stopListening(); // First, stop listening so we can talk
radio.write( &message, sizeof(unsigned long) ); // Send the final one back.
radio.startListening(); // Now, resume listening so we catch the next packets.
Serial.print(F("Empfangen: "));
Serial.println(message);
if((message / 10) == led_code){
if((message % 10) == 1){
digitalWrite(led_port, HIGH);
} else {
digitalWrite(led_port, LOW);
}
}
}
} // Loop
Alles anzeigen
Grüße Logan517