我正在尝试将两个充当发送器的NRF24L01收发器同步到充当接收器的NRF24L01中。
两个发送器都将连接到单独的传感器,而这些传感器的数据正是我试图将其同时发送到接收器中的。结果是两个发送器产生的难以理解的数据冲突。
我的代码如下。如您在下面的接收器代码中所见,我尝试将数据包与发送器分开。
#include <SPI.h>
#include "RF24.h"
RF24 myRadio (8, 9);
byte addresses[6] = {"1Node"};
int accelgyroscope[3];
int ax, ay, az;
int gx, gy, gz;
int LED_GREEN = A3;
int LED_RED = A2;
struct Pack {
int data1;
int data2;
int data3;
int data4;
int data5;
int accx;
int accy;
int accz;
int con1;
int con2;
int data11;
int data22;
int data33;
int data44;
int data55;
int accxx;
int accyy;
int acczz;
} packet;
void setup() {
Serial.begin(9600);
myRadio.begin();
myRadio.setChannel(108);
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openReadingPipe(1, addresses[0]);
myRadio.openReadingPipe(2, addresses[1]);
myRadio.startListening();
Serial.print("LABEL,F1,F2,F3,F4,F5,X,Y,Z,C1,C2");
Serial.println();
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_RED, OUTPUT);
}
void loop() {
if ( myRadio.available()) {
analogWrite(LED_GREEN, 1023);
analogWrite(LED_RED, 0);
myRadio.read( &packet, sizeof(packet) );
Serial.print("TRANSMITTER 1: ");
Serial.print(", ");
Serial.print(packet.data1);
Serial.print(", ");
Serial.print(packet.data2);
Serial.print(", ");
Serial.print(packet.data3);
Serial.print(", ");
Serial.print(packet.data4);
Serial.print(", ");
Serial.print(packet.data5);
Serial.print(", ");
Serial.print(packet.accx);
Serial.print(", ");
Serial.print(packet.accy);
Serial.print(", ");
Serial.print(packet.accz);
Serial.print(", ");
Serial.print(packet.con1);
Serial.print(", ");
Serial.print(packet.con2);
Serial.print(" ");
Serial.print("TRANSMITTER 2: ");
Serial.print(", ");
Serial.print(packet.data11);
Serial.print(", ");
Serial.print(packet.data22);
Serial.print(", ");
Serial.print(packet.data33);
Serial.print(", ");
Serial.print(packet.data44);
Serial.print(", ");
Serial.print(packet.data55);
Serial.print(", ");
Serial.print(packet.accxx);
Serial.print(", ");
Serial.print(packet.accyy);
Serial.print(", ");
Serial.print(packet.acczz);
Serial.print(", ");
Serial.println();
}
else
{
analogWrite(LED_RED, 1023);
analogWrite(LED_GREEN, 0);
}
}
这是我的第一个发射机的代码。
#include <SPI.h> //libraries para sa nrf24l01
#include "RF24.h" //
#include <Wire.h> // MPU-6050 I2C LIBRARY
#include <I2Cdev.h> //
#include <MPU6050.h> //
#define flex_1 A0 //declaring ng variable tsaka pin assignment ng 5 flex sensors
#define flex_2 A1
#define flex_3 A2
#define flex_4 A3
#define flex_5 A6
MPU6050 accelgyro; // variable ng mpu6050
int16_t ax, ay, az; // variables para accelerometer and gyroscope
int16_t gx, gy, gz;
int pinx=2; // pin assignment ng contact sensors
int piny=3;
RF24 myRadio (8, 9); // CSN AT CE PIN ASSIGNMENT
byte addresses[6] = {"1Node"}; // address ng both transmitter and receiver
// structure para masend ng inorder
struct Pack {
int data1;
int data2;
int data3;
int data4;
int data5;
int accx;
int accy;
int accz;
int con1;
int con2;
} packet;
void setup() {
Serial.begin(38400); // para magamit ang serial monitor
Serial.println(F("RF24/Simple Transmit data Test"));
Wire.begin(); //
accelgyro.initialize(); // para maread ang pin ng mpu6050
pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);
pinMode(piny, INPUT);
digitalWrite(piny, HIGH);
/*
accelgyro.setXAccelOffset(2825);
accelgyro.setYAccelOffset(457);
accelgyro.setZAccelOffset(645);
*/
myRadio.begin(); // nrf24l01
myRadio.setChannel(108);
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openWritingPipe(addresses[0]);
}
void loop() {
packet.data1 = analogRead(flex_1);
packet.data1=map(packet.data1, 450, 950, 3000, 4000);
packet.data2 = analogRead(flex_2);
packet.data2=map(packet.data2, 450, 950, 3300, 4500);
packet.data3 = analogRead(flex_3);
packet.data3=map(packet.data3, 450, 950, 3000, 4000);
packet.data4 = analogRead(flex_4);
packet.data4=map(packet.data4, 450, 950, 3000, 4000);
packet.data5 = analogRead(flex_5);
packet.data5=map(packet.data5, 450, 950, 3000, 4000);
packet.accx=ax;
packet.accy=ay;
packet.accz=az;
packet.con1=digitalRead(pinx);
packet.con2=digitalRead(piny);
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
if (myRadio.write(&packet, sizeof(packet))) {
Serial.print(F("flex1 = "));
Serial.print(packet.data1);
Serial.print(F(" flex2 = "));
Serial.print(packet.data2);
Serial.print(F(" flex3 = "));
Serial.print(packet.data3);
Serial.print(F(" flex4 = "));
Serial.print(packet.data4);
Serial.print(F(" flex5 = "));
Serial.print(packet.data5);
Serial.print(F(" accx = "));
Serial.print(packet.accx);
Serial.print(F(" accy = "));
Serial.print(packet.accy);
Serial.print(F(" accz = "));
Serial.print(packet.accz);
Serial.print(F(" con1 = "));
Serial.print(packet.con1);
Serial.print(F(" con2 = "));
Serial.print(packet.con2);
Serial.println();
}
else {
Serial.print(F("Send failed."));
}
}
这是另一个发送器:
#include <SPI.h> //libraries para sa nrf24l01
#include "RF24.h" //
#include <Wire.h> // MPU-6050 I2C LIBRARY
#include <I2Cdev.h> //
#include <MPU6050.h> //
#define flex_1 A0 //declaring ng variable tsaka pin assignment ng 5 flex sensors
#define flex_2 A1
#define flex_3 A2
#define flex_4 A3
#define flex_5 A6
MPU6050 accelgyro; // variable ng mpu6050
int16_t ax, ay, az; // variables para accelerometer and gyroscope
int16_t gx, gy, gz;
int pinx=2; // pin assignment ng contact sensors
int piny=3;
RF24 myRadio (8, 9); // CSN AT CE PIN ASSIGNMENT
byte addresses[6] = {"1Node"}; // address ng both transmitter and receiver
// structure para masend ng inorder
struct Pack {
int data11;
int data22;
int data33;
int data44;
int data55;
int accxx;
int accyy;
int acczz;
} packet1;
void setup() {
Serial.begin(9600); // para magamit ang serial monitor
Serial.println(F("RF24/Simple Transmit data Test"));
Wire.begin(); //
accelgyro.initialize(); // para maread ang pin ng mpu6050
pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);
pinMode(piny, INPUT);
digitalWrite(piny, HIGH);
/*
accelgyro.setXAccelOffset(2825);
accelgyro.setYAccelOffset(457);
accelgyro.setZAccelOffset(645);
*/
myRadio.begin(); // nrf24l01
myRadio.setChannel(108);
myRadio.setPALevel(RF24_PA_MIN);
myRadio.openWritingPipe(addresses[1]);
}
void loop() {
packet1.data11 = analogRead(flex_1);
packet1.data11=map(packet1.data11, 450, 950, 3000, 4000);
packet1.data22 = analogRead(flex_2);
packet1.data22=map(packet1.data22, 450, 950, 3300, 4500);
packet1.data33 = analogRead(flex_3);
packet1.data33=map(packet1.data33, 450, 950, 3000, 4000);
packet1.data44 = analogRead(flex_4);
packet1.data44=map(packet1.data44, 450, 950, 3000, 4000);
packet1.data55 = analogRead(flex_5);
packet1.data55=map(packet1.data55, 450, 950, 3000, 4000);
packet1.accxx=ax;
packet1.accyy=ay;
packet1.acczz=az;
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
if (myRadio.write(&packet1, sizeof(packet1))) {
Serial.print(F("flex1 = "));
Serial.print(packet1.data11);
Serial.print(F(" flex2 = "));
Serial.print(packet1.data22);
Serial.print(F(" flex3 = "));
Serial.print(packet1.data33);
Serial.print(F(" flex4 = "));
Serial.print(packet1.data44);
Serial.print(F(" flex5 = "));
Serial.print(packet1.data55);
Serial.print(F(" accx = "));
Serial.print(packet1.accxx);
Serial.print(F(" accy = "));
Serial.print(packet1.accyy);
Serial.print(F(" accz = "));
Serial.print(packet1.acczz);
Serial.print(F(" con1 = "));
Serial.println();
}
else {
Serial.print(F("Send failed."));
}
}
看到两个发送器,我使用了不同的地址。但是我不确定我做对了吗。
我的代码和.INO文件的全部内容都可以找到here。非常感谢你。我真的希望能有很多帮助。
答案 0 :(得分:0)
您似乎在接收方仅读取一个数据包,并期望它包含两个发送方的数据;但是每个发送器都发送一个单独的数据包。因此,您仅从其中一个发送器(在本例中为发送器1)读取数据,因为这是import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.future.WriteFuture;
import org.apache.asyncweb.common.HttpRequest;
import org.apache.asyncweb.common.HttpResponseStatus;
import org.apache.asyncweb.common.MutableHttpResponse;
import org.apache.asyncweb.common.DefaultHttpResponse;
import org.apache.asyncweb.common.HttpHeaderConstants;
public class HttpProtocolHandler implements IoHandler {
private static final int CONTENT_PADDING = 0; // 101
private final Map<Integer, IoBuffer> buffers = new ConcurrentHashMap<Integer, IoBuffer>();
private final Timer timer;
public HttpProtocolHandler() {
timer = new Timer(true);
}
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
if (!(cause instanceof IOException)) {
cause.printStackTrace();
}
session.close();
}
public Dictionary extractParameters(Map hashParameters){
Dictionary parameters = new Hashtable();
Iterator it = hashParameters.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
parameters.put(pair.getKey(), ((ArrayList) pair.getValue()).get(0) );
// it.remove(); // avoids a ConcurrentModificationException
}
return parameters;
}
public void messageReceived(IoSession session, Object message)
throws Exception {
HttpRequest req = (HttpRequest) message;
String path = req.getRequestUri().getPath(); //path: /echo
String end_point = path;
Dictionary parameters = this.extractParameters(req.getParameters());
String response = "";
/* switch (end_point) {
case "/io":
response= new IOHandler().handleRequest(parameters);
break;
case "/cpu":
response= new CPUHandler().handleRequest(parameters);
break;
case "/db":
response= new DBHandler().handleRequest(parameters);
break;
case "/memory":
response= new MemoryHandler().handleRequest(parameters);
break;
default:
response = "No end point found";
} */
response = "No end point found";
MutableHttpResponse res;
// if (path.startsWith("/size/")) {
// doDataResponse(session, req);
// } else if (path.startsWith("/delay/")) {
// doAsynchronousDelayedResponse(session, req);
// } else if (path.startsWith("/adelay/")) {
// doAsynchronousDelayedResponse(session, req);
// } else {
res = new DefaultHttpResponse();
IoBuffer bb = IoBuffer.allocate(1024);
bb.setAutoExpand(true);
bb.putString(response.toString(), Charset.forName("UTF-8").newEncoder());
bb.flip();
res.setContent(bb);
// res.setHeader("Pragma", "no-cache");
// res.setHeader("Cache-Control", "no-cache");
res.setStatus(HttpResponseStatus.OK);
WriteFuture future = session.write(res);
if (!HttpHeaderConstants.VALUE_KEEP_ALIVE.equalsIgnoreCase(
res.getHeader( HttpHeaderConstants.KEY_CONNECTION))) {
future.addListener(IoFutureListener.CLOSE);
}
}
private void writeResponse(IoSession session, HttpRequest req,
MutableHttpResponse res) {
res.normalize(req);
WriteFuture future = session.write(res);
if (!HttpHeaderConstants.VALUE_KEEP_ALIVE.equalsIgnoreCase(
res.getHeader( HttpHeaderConstants.KEY_CONNECTION))) {
future.addListener(IoFutureListener.CLOSE);
}
}
private void doDataResponse(IoSession session, HttpRequest req) {
String path = req.getRequestUri().getPath();
int size = Integer.parseInt(path.substring(path.lastIndexOf('/') + 1))
+ CONTENT_PADDING;
MutableHttpResponse res = new DefaultHttpResponse();
res.setStatus(HttpResponseStatus.OK);
res.setHeader("ETag", "W/\"" + size + "-1164091960000\"");
res.setHeader("Last-Modified", "Tue, 31 Nov 2006 06:52:40 GMT");
IoBuffer buf = buffers.get(size);
if (buf == null) {
buf = IoBuffer.allocate(size);
buffers.put(size, buf);
}
res.setContent(buf.duplicate());
writeResponse(session, req, res);
}
private void doAsynchronousDelayedResponse(final IoSession session,
final HttpRequest req) {
String path = req.getRequestUri().getPath();
int delay = Integer.parseInt(path.substring(path.lastIndexOf('/') + 1));
final MutableHttpResponse res = new DefaultHttpResponse();
res.setStatus(HttpResponseStatus.OK);
res.setHeader("ETag", "W/\"0-1164091960000\"");
res.setHeader("Last-Modified", "Tue, 31 Nov 2006 06:52:40 GMT");
timer.schedule(new TimerTask() {
@Override
public void run() {
writeResponse(session, req, res);
}
}, delay);
}
public void messageSent(IoSession session, Object message) throws Exception {
}
public void sessionClosed(IoSession session) throws Exception {
}
public void sessionCreated(IoSession session) throws Exception {
}
public void sessionIdle(IoSession session, IdleStatus status)
throws Exception {
session.close();
}
public void sessionOpened(IoSession session) throws Exception {
session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);
}
}
的默认设置。通过为每个变送器调用available()
来检查每个单独管道的可用数据包,其中available(&pipeNum)
是对包含分配的管道号的变量的引用。