Hi there.
I'm stuck on a problem. Here is the scenario:
1 Mega 2560 acting as the main node of the network (node 00)
4 Pro Mini 5v with 328 uC, acting as nodes 01, 02, 03 and 04. (Currently i'm testing it with only node 01, as i didnt assembled the other nodes yet)
Project Goal: Control an Cargo Elevator with 4 stops .
=> 1 wireless device on each floor with one 7-seg display and 4 buttons switch.
Problem: The Pro Mini is failing 99% of the times it tries to send a packet to the Main node, in the other hand, the main node achieved a success rate of 98% when sending packets to the Pro Mini. Guess a hardware problem may be ruled out here.
Note: This is work in progress, on early stages of development, its lacking of polishment and optimizations on code.
Mega 2560 (Main Node) Code:
/*
Elevator Control
by: Luiz A. Tessarolli
Aug. 2013
*/
//Imports
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <RF24Network.h>
#include <RF24Network_config.h>
#include <Sync.h>
#include "printf.h"
//Variables Declarations
#define NODE_COUNT 2
int Floor=-1; //Holds the floor the elevator cart is in
byte Door[4] = {
0}; //Gets/Sets the floor elevator door status, 1 = open, 0 = closed
byte Call[4] = {
0}; //Holds elevator calls
byte ComDesce = 30; //Pino Comando Descer
byte ComSobe = 31; //Pino Comando Subir
byte Status; //Gets/Sets the current elevator status
const byte STOPPED = 0; //Elevator is not moving, everything if ok, ready to move
const byte UP = 1; //Elevator is going up
const byte DOWN = 2; //Elevator is going down
const byte DOOROPEN = 3; //Elevator door is open
const byte WAITING = 4; //Elevator is waiting for current action to finish
const byte INITIALIZATION = 5; //Elevator is on initialization state, on this state it will go down until it hits a level sensor (Floor <> -1)
const byte UNK = 99; //Elevator is in an Unknown (Error) State
const byte interval = 500;
unsigned long last;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(10,10,0,177);
EthernetServer server(80);
// nRF24L01(+) radio attached to SPI and pins 49 & 53
RF24 radio(49,53);
// Network uses that radio
RF24Network network(radio);
// Address of the main node
const uint16_t this_node = 00;
// Address of the others nodes
const uint16_t node[4]={
01,02,03,04};
// Structure of our packets
struct pkt_rx //Gets the sensors and buttons state from nodes
{
uint16_t node; //Node Address
byte Door; //Door Open Sensor
byte Floor; //Floor Level Sensor
byte Btn[4]; //Button Press
};
struct pkt_tx //Sends Data to Nodes
{
uint16_t node; //Node Destination
byte Chamou[4]; //Holds elevators Calls
byte DoorOpen[4]; //For blinking led buttons
byte Floor; //Current Floor Number for 7seg display
byte UnlockDoor; //Signal to unlock the door
};
//Initialization
void setup(){
pinMode(ComDesce, OUTPUT);
pinMode(ComSobe, OUTPUT);
Floor = -1; //Inicializa com Floor = -1
Stop(); //Certifica que elevador esta parado.
Status = INITIALIZATION;
Serial.begin(57600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
printf_begin();
SPI.begin();
radio.begin();
network.begin(/*channel*/ 57, /*node address*/ this_node);
radio.printDetails();
/*
while (Floor == -1){
wireless();
checkDoors();
Floor = getCurrentFloor();
Desce(-1);
debug();
delay(1);
}
*/
}
//Main Loop
void loop(){
wireless();
ethernet();
navigate();
debug();
delay(1);
}
//User Functions
void wireless(){
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
pkt_rx msg;
network.read(header,&msg,sizeof(msg));
/*
uint16_t node; //Node Address
byte Door; //Door Open Sensor
byte Floor; //Floor Level Sensor
byte Btn[4]; //Button Press
*/
Door[msg.node-1]=msg.Door;
if (msg.Floor==1) //Sets Floor Level Variable
Floor = msg.node-1;
for (int i=0;i<4;i++){
if (msg.Btn[i]==1) Call[i]==1;
}
Serial.println("Received packet: ");
Serial.print("Node: ");
Serial.println(msg.node);
Serial.print("Door: ");
Serial.println(msg.Door);
Serial.print("Floor: ");
Serial.println(msg.Floor);
Serial.print("Buttons: {");
Serial.print(msg.Btn[0]);
Serial.print(",");
Serial.print(msg.Btn[1]);
Serial.print(",");
Serial.print(msg.Btn[2]);
Serial.print(",");
Serial.print(msg.Btn[3]);
Serial.println("}");
Serial.println();
//digitalWrite(13, HIGH);
}
if (millis() - last > interval){
//send packet to mobiles
last = millis();
/* uint16_t node; //Node Destination
int Chamou[4]; //Holds elevators Calls
int DoorOpen[4]; //For blinking led buttons
int Floor; //Current Floor Number for 7seg display
int UnlockDoor; //Signal to unlock the door
*/
for (int i=1; i<=NODE_COUNT; i++){
byte DoorUnlock=0;
if (Status == STOPPED && Floor==i-1) DoorUnlock=1;
Serial.print("Sending node 0"); Serial.print(i);Serial.print("...");
pkt_tx msg = {
node[i-1], {Call[0],Call[1],Call[2],Call[3]}, {Door[0],Door[1],Door[2],Door[3]}, Floor, DoorUnlock };
RF24NetworkHeader header(/*to node*/ node[i-1]);
bool ok = network.write(header,&msg,sizeof(msg));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
}
}
void debug(){
if (millis() % 2000 == 0){
Serial.print("Floor: ");
Serial.print(Floor, DEC);
Serial.print(" Status: ");
Serial.println(Status, DEC);
}
}
//Executa comando para elevador Subir
void Sobe(int destFloor){
//Status = WAITING;
if (destFloor >= Floor){ //Se o andar de destino for maior ou igual ao andar atual, nao faz nada
Stop(); //para o elevador
return;
}
if (Status == DOOROPEN){ //Se a porta estiver aberta Para o elevador
Stop();
return;
}
digitalWrite(ComSobe, HIGH);
Status = DOWN;
if (Floor = destFloor)
Stop();
}
//Executa comando para elevador Descer
void Desce(int destFloor){
//Status = WAITING;
if (Floor <= destFloor && destFloor > -1){ //Se for -1 é para localizar o elevador. Nao da para descer nestas condicoes
Stop();
return;
}
if (Status == DOOROPEN){ //Se a porta estiver aberta Para o elevador
Stop();
return;
}
if (Status != DOOROPEN){
digitalWrite(ComDesce, HIGH);
Status = DOWN;
}
if (destFloor == -1){ //Procura andar mais proximo
if (Floor > -1) //encontrou
Stop();
}
else{ //Operação normal
if (Floor == destFloor) //chegou no andar desejado
Stop();
}
}
void navigate(){
Status = STOPPED;
boolean door=false;
for (int i=0;i<4;i++){
if (Door[i]) door=true;
}
if (door) Status=DOOROPEN;
for (int i=1;i<=NODE_COUNT;i++){
if (Call[i]==1){
if (i>Floor)
Sobe(i);
else if (i<Floor)
Desce(i);
}
}
}
//Stop the Elevator
void Stop(){
if (Status!=DOOROPEN)
Status = STOPPED;
digitalWrite(ComDesce, LOW);
digitalWrite(ComSobe, LOW);
}
String buffer;
int i;
void ethernet(){
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
buffer="";
Serial.println("new client");
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.print(c);
if (c=='*'){
//buffer[i] = '\0';
Serial.println(buffer);
if (buffer=="st"){
client.print(millis());
}
else{
client.print(0);
}
client.stop();
}
else{
buffer += c;
//Serial.print(buffer);
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("\nclient disonnected");
}
}