Hi All,
sorry, a bit of help please.
I am sure I am just getting mixed up with using my INT's on the ackData variable.
I have two codes master and slave for polling data from door sensors.
I have included the full codes here
the issue I have is that what is being sent from the slave is not what is being shown as the output from theaster, and I know it will be something simple!
Master code:
// MultiTxAckPayload - the master or the transmitter
// works with two Arduinos as slaves
// each slave should the SimpleRxAckPayload program
// one with the adress {'R','x','A','A','A'}
// and the other with {'R','x','A','A','B'}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define CE_PIN D7
#define CSN_PIN D8
const byte numSlaves = 3;
const byte slaveAddress[numSlaves][5] = {
// each slave needs a different address
{'R', 'x', 'A', 'A', 'A'},
{'R', 'x', 'A', 'A', 'B'},
{'R', 'x', 'A', 'A', 'C'}
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
//~ char dataToSend[10] = "Message 0";
char dataToSend[10] = "ToSlvN 0";
char txNum = '0';
int ackData[3] = {0,0,0}; // to hold the two values coming from the slave
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
const char* ssid = "xxxxxxxx";
const char* password = "xxxxxxxx";
const char* mqtt_server = "xxxxxxxxx";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//===============
void setup() {
Serial.begin(115200);
Serial.println(F("Source File = /mnt/SGT/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/MultiTxAckPayload.ino "));
Serial.println("SimpleTxAckPayload Starting");
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3, 5); // delay, count
// radio.openWritingPipe(slaveAddress); -- moved to loop()
}
//=============
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
// showData(); -- moved into send()
}
//================
void send() {
// call each slave in turn
for (byte n = 0; n < numSlaves; n++) {
// open the writing pipe with the address of a slave
radio.openWritingPipe(slaveAddress[n]);
// include the slave number in the message
dataToSend[5] = n + '0';
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print(" ======== For Slave ");
Serial.print(n);
Serial.println(" ========");
Serial.print(" Data Sent ");
Serial.print(dataToSend);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
updateMessage();
}
else {
Serial.println(" Tx failed");
}
showData(n);
Serial.print("\n");
}
prevMillis = millis();
}
//=================
void showData(byte n) {
if (newData == true) {
Serial.print(" Acknowledge data ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.print(ackData[2]);
Serial.println();
newData = false;
if (n == 0) {
snprintf (msg, 50, "%ld", ackData[0]);
client.publish("sensor/window1", msg);
snprintf (msg, 50, "%ld", ackData[1]);
client.publish("sensor/window2", msg);
}
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
slave Code
// SimpleRxAckPayload- the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
#define sensor1 2
#define sensor2 3
#define sensor3 4
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[10]; // this must match dataToSend in the TX
int ackData[3] = {0,0,0}; // the two values to be sent to the master
bool newData = false;
//==============
void setup() {
Serial.begin(115200);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode(sensor3, INPUT_PULLUP);
}
//==========
void loop() {
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
updateReplyData();
newData = true;
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
Serial.print(" ackPayload sent ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.print(ackData[1]);
Serial.print(", ");
Serial.println(ackData[2]);
newData = false;
newData = false;
}
}
//================
void updateReplyData() {
if (digitalRead(sensor1) == LOW){
ackData[0]=1;
} else {
ackData[0]=0;
}
if (digitalRead(sensor2) == LOW){
ackData[1]=1;
} else {
ackData[1]=0;
}
if (digitalRead(sensor3) == LOW){
ackData[2]=1;
} else {
ackData[2]=0;
}
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}