Skip to content

Commit

Permalink
now the lamp node set itself with address 254 at startup and ask the …
Browse files Browse the repository at this point in the history
…onoscenter for a free address
  • Loading branch information
marco committed Sep 3, 2016
1 parent 095074b commit 105f8c2
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 58 deletions.
132 changes: 91 additions & 41 deletions arduino_code/onos_lamp_node.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
// *********** IMPORTANT SETTINGS - YOU MUST CHANGE/CONFIGURE TO FIT YOUR HARDWARE *************
//*********************************************************************************************
#define NETWORKID 100 //the same on all nodes that talk to each other
#define NODEID 2


//Match frequency to the hardware version of the radio on your Feather
//#define FREQUENCY RF69_433MHZ
Expand Down Expand Up @@ -72,9 +72,9 @@ char serial_number[13]="WLightSS0003";

char node_fw[]="5.13";

int this_node_address=2; //i start with 254
int this_node_address=254; //i start with 254

int old_address=2;
int old_address=254;

unsigned long get_address_timeout=0;

Expand All @@ -92,6 +92,7 @@ int received_message_value;
char received_message_answer[rx_msg_lenght+6]="er00_#]";
char received_message_sn[13]="";
int received_message_address=0; //must be int..
char received_serial_number[13];
char filtered_onos_message[rx_msg_lenght+3];
char syncMessage[28];
char str_this_node_address[4];
Expand Down Expand Up @@ -133,6 +134,7 @@ void makeSyncMessage(){


void getAddressFromGateway(){
Serial.println("getAddressFromGateway executed");

//[S_001ga3.05ProminiS0001_#]
makeSyncMessage();
Expand All @@ -142,7 +144,7 @@ void getAddressFromGateway(){
syncMessage[6]='g'; //modify the message to get a address instead of just sync.
syncMessage[7]='a'; //modify the message to get a address instead of just sync.

if (radio.sendWithRetry(gateway_address, syncMessage, strlen(syncMessage),10,500)) {
if (radio.sendWithRetry(gateway_address, syncMessage, strlen(syncMessage))) {
//target node Id, message as string or byte array, message length,retries, milliseconds before retry
//(uint8_t toAddress, const void* buffer, uint8_t bufferSize, uint8_t retries, uint8_t retryWaitTime)
Serial.println("sent_get_address");
Expand Down Expand Up @@ -304,6 +306,45 @@ void decodeOnosCmd(const char *received_message){
}


//[S_254sa123WLightSS0003_#]

else if( received_message_type_of_onos_cmd[0]=='s' && received_message_type_of_onos_cmd[1]=='a' ){

received_message_value=(received_message[8]-48)*100+(received_message[9]-48)*10+(received_message[10]-48)*1;

received_serial_number[0]= received_message[11];
received_serial_number[1]= received_message[12];
received_serial_number[2]= received_message[13];
received_serial_number[3]= received_message[14];
received_serial_number[4]= received_message[15];
received_serial_number[5]= received_message[16];
received_serial_number[6]= received_message[17];
received_serial_number[7]= received_message[18];
received_serial_number[8]= received_message[19];
received_serial_number[9]= received_message[20];
received_serial_number[10]= received_message[21];
received_serial_number[11]= received_message[22];



if (strcmp(received_serial_number,serial_number)!=0) {//onos command not for this node
strcpy(received_message_answer,"er1_sn_#]");
return;
}


if ((received_message_value<0)||(received_message_value>254)){ //status check
received_message_value=0;
//Serial.println(F("onos_cmd_value_error"));
strcpy(received_message_answer,"er0_status_#]");
return;
}


this_node_address=received_message_value;
strcpy(received_message_answer,"ok");

}


/*
Expand Down Expand Up @@ -362,7 +403,7 @@ void setup() {
delay(100);

// Initialize radio
radio.initialize(FREQUENCY,NODEID,NETWORKID);
radio.initialize(FREQUENCY,this_node_address,NETWORKID);
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
Expand All @@ -380,41 +421,6 @@ void setup() {
void loop() {


if (old_address==254){// i have not the proper address yet..



if (old_address!=this_node_address){//the address has changed and i restart radio to use it

radio.initialize(FREQUENCY,NODEID,this_node_address);
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)

radio.encrypt(ENCRYPTKEY);
old_address=this_node_address;
get_address_timeout=millis();

}



if ((millis()-get_address_timeout)>500){ //every 500 ms

get_address_timeout=millis();

getAddressFromGateway(); //ask the gateway for a proper address




}


}


//check if something was received (could be an interrupt from the radio)

if (radio.receiveDone()){
Expand Down Expand Up @@ -490,7 +496,51 @@ void loop() {

radio.receiveDone(); //put radio in RX mode
Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU
}





if (old_address==254){// i have not the proper address yet..



if (old_address!=this_node_address){//the address has changed and i restart radio to use it

radio.initialize(FREQUENCY,this_node_address,NETWORKID);
if (IS_RFM69HCW) {
radio.setHighPower(); // Only for RFM69HCW & HW!
}
radio.setPowerLevel(31); // power output ranges from 0 (5dBm) to 31 (20dBm)

radio.encrypt(ENCRYPTKEY);
old_address=this_node_address;
get_address_timeout=millis();

}


if ((millis()-get_address_timeout)>5000){ //every 5000 ms

get_address_timeout=millis();

getAddressFromGateway(); //ask the gateway for a proper address


}


}









}//END OF LOOP()

void Blink(byte PIN, byte DELAY_MS, byte loops)
{
Expand Down
143 changes: 130 additions & 13 deletions arduino_code/onos_rf69_serial_transponder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ char serial_number[13]="ProminiS0001";
char node_fw[]="5.13";

int this_node_address=1; //must be int..

unsigned long timeout;


#define rx_msg_lenght 61
Expand All @@ -134,8 +134,8 @@ int received_message_value;
char received_message_answer[rx_msg_lenght+6]="er00_#]";
//char received_message_sn[13]="";
int received_message_address=0; //must be int..


char filtered_onos_message[rx_msg_lenght+3];
char data_from_serial [rx_msg_lenght+5];

uint8_t counter;
boolean enable_answer_back=0;
Expand Down Expand Up @@ -503,13 +503,8 @@ void loop()
{


if (radio_enabled==1){

// Wait for a message addressed to us from the client
uint8_t a = 0;//noop


}



Expand All @@ -518,9 +513,12 @@ void loop()



char data_from_serial [rx_msg_lenght+5];
char filtered_onos_message[rx_msg_lenght+3];
unsigned long timeout=millis()+200;

strcpy(data_from_serial,"");

strcpy(filtered_onos_message,"");

timeout=millis()+200;


uint8_t counter0=0;
Expand Down Expand Up @@ -578,7 +576,7 @@ void loop()

counter0=counter0+1;

}
}// end of while rx receive



Expand Down Expand Up @@ -731,7 +729,7 @@ void loop()
Serial.print('\n');
sync_time=millis();
enable_answer_back=0;
// the answer will be : "ok"+receivedmesssage for example: okonos_d05v001sProminiS0001f001_#]
// the answer will be : "ok"+receivedmessage for example: okonos_d05v001sProminiS0001f001_#]

}

Expand All @@ -751,6 +749,125 @@ void loop()
}





//uart reception part concluded




//radio part started


if (radio_enabled==1){

// Wait for a message addressed to us from the client
if (radio.receiveDone()){
//print message received to serial
Serial.print('[');Serial.print(radio.SENDERID);Serial.print("] ");
Serial.print((char*)radio.DATA);
Serial.print(" [RX_RSSI:");Serial.print(radio.RSSI);Serial.print("]");


//check if received message contains Hello World



strcpy(filtered_onos_message,"");

for (uint8_t counter0 = 0; counter0 <= rx_msg_lenght; counter0++) {
filtered_onos_message[counter0]=radio.DATA[counter0];
// Serial.println(filtered_onos_message[counter0]);

//[S_001dw06001_#]
if (counter0<2){
continue;
}
if ( (filtered_onos_message[counter0-2]=='[')&&(filtered_onos_message[counter0-1]=='S')&&(filtered_onos_message[counter0]=='_') ){//
Serial.println("cmd start found-------------------------------");
onos_cmd_start_position=counter0-2;
}


if( (filtered_onos_message[counter0-2]=='_')&&(filtered_onos_message[counter0-1]=='#')&&(filtered_onos_message[counter0]==']') ){//
Serial.println("cmd end found-------------------------------");
onos_cmd_end_position=counter0-2;
break;// now the message has ended
}


}



if ( (onos_cmd_start_position!=-99) && (onos_cmd_end_position!=-99 )){
Serial.println("onos cmd found-------------------------------");
decodeOnosCmd(filtered_onos_message);

if( (received_message_answer[0]=='o')&&(received_message_answer[1]=='k')||(strcmp(received_message_answer,"remote_#]")==0)){//if the message was ok...
//check if sender wanted an ACK
if (radio.ACKRequested()){
radio.sendACK();
Serial.println(" - ACK sent");
}

}
else{
Serial.println("error in message decode i will not send the ACK");
}



}
else{
strcpy(received_message_answer,"nocmd0_#]");
Serial.println("error in message nocmd0_#]");
}




// Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks


}


if(strcmp(received_message_answer,"remote_#]")==0){ //transmit the received data from the node to the serial port


for (uint8_t pointer = 0; pointer <= rx_msg_lenght; pointer++) {
Serial.print(filtered_onos_message[pointer]);

if (pointer<2){
continue;
}

if ((filtered_onos_message[pointer-2]=='_')&&(filtered_onos_message[pointer-1]=='#')&&(filtered_onos_message[pointer]==']') ) {//
break;
}

}

}



radio.receiveDone(); //put radio in RX mode
Serial.flush(); //make sure all serial data is clocked out before sleeping the MCU


}// END OF if (radio_enabled==1){












Expand Down

0 comments on commit 105f8c2

Please sign in to comment.