Skip to content

Commit

Permalink
started using memset(filtered_onos_message,0,sizeof(filtered_onos_mes…
Browse files Browse the repository at this point in the history
…sage))to clear the arrays in the arduino programs,before i used strcpy(filtered_onos_message,) that doesn't work well
  • Loading branch information
marco committed Dec 24, 2016
1 parent 58245b6 commit b02e5ca
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 95 deletions.
84 changes: 60 additions & 24 deletions arduino_code/onos_plug_node_WPlugavx/onos_plug_node_wplugavx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ char syncMessage[48];
char str_this_node_address[4];
uint8_t main_obj_selected=0;
uint8_t rx_main_obj_selected=0;
char progressive_msg_id=48; //48 is 0 in ascii //a progressive id to make each message unique
//////////////////////////////////End of Standard part to run decodeOnosCmd()//////////////////////////////////


uint8_t radioRetry=4; //todo: make this changable from serialport
uint8_t radioTxTimeout=50; //todo: make this changable from serialport
char received_serial_number[13];
# define gateway_address 1

Expand Down Expand Up @@ -200,7 +202,7 @@ boolean changeObjStatus(char obj_number,int status_to_set){
digitalWrite(relay1_reset_pin,!status_to_set);
digitalWrite(relay2_set_pin,status_to_set);
digitalWrite(relay2_reset_pin,!status_to_set);
delay(100);
delay(50);
digitalWrite(relay1_set_pin,0);
digitalWrite(relay1_reset_pin,0);
digitalWrite(relay2_set_pin,0);
Expand All @@ -210,16 +212,26 @@ boolean changeObjStatus(char obj_number,int status_to_set){
return(1);
}




return(0);

}


void composeSyncMessage(){

Serial.println("composeSyncMessage executed");
//[S_123ul5.24WPlugAvx000810000x_#]

if (progressive_msg_id<122){ //122 is z in ascii
progressive_msg_id=progressive_msg_id+1;
}
else{
progressive_msg_id=48; //48 is 0 in ascii
}

//[S_123ul5.24WPlugAvx000810000_#]
// example deprecated: [S_001ul3.05WPlugAvx00010167123_#] #lux=167 contact0 is at 0 , 123 minutes on since boot
if (main_obj_state==1){

if (time_continuos_on!=0){
Expand Down Expand Up @@ -247,9 +259,11 @@ void composeSyncMessage(){

char tmp_minutes_time_from_turn_on_array[5];
char minutes_time_from_turn_on_array[5];
strcpy(tmp_minutes_time_from_turn_on_array,"");
strcpy(minutes_time_from_turn_on_array,"");
//strcpy(tmp_minutes_time_from_turn_on_array,"");
//strcpy(minutes_time_from_turn_on_array,"");

memset(tmp_minutes_time_from_turn_on_array,0,sizeof(tmp_minutes_time_from_turn_on_array)); //to clear the array
memset(minutes_time_from_turn_on_array,0,sizeof(minutes_time_from_turn_on_array)); //to clear the array

//itoa (minutes_time_from_turn_on,minutes_time_from_turn_on_array,10); //convert from int to char array
//dtostrf //convert from float to char array
Expand Down Expand Up @@ -283,8 +297,8 @@ void composeSyncMessage(){


int tmp_number=0;
strcpy(str_this_node_address,"");

//strcpy(str_this_node_address,"");
memset(str_this_node_address,0,sizeof(str_this_node_address)); //to clear the array
str_this_node_address[0]='0';
str_this_node_address[1]='0';
str_this_node_address[2]='0';
Expand All @@ -309,26 +323,34 @@ void composeSyncMessage(){
}


strcpy(syncMessage, "");
//strcpy(syncMessage, "");
memset(syncMessage,0,sizeof(syncMessage)); //to clear the array
strcpy(syncMessage, "[S_");
strcat(syncMessage, str_this_node_address);
strcat(syncMessage, "ul");
// strcat(syncMessage, "sy");
strcat(syncMessage, node_fw);
strcat(syncMessage, serial_number);



//[S_123ul5.24WPlugAvx000810000x_#]

/*
if (main_obj_state==0){
strcat(syncMessage,"0");
}
else{
strcat(syncMessage,"1");
}
*/
syncMessage[strlen(syncMessage)]=main_obj_state+48; //+48 for ascii translation

strcat(syncMessage, minutes_time_from_turn_on_array);

syncMessage[strlen(syncMessage)]=progressive_msg_id; //put the variable msgid in the array
//Serial.println(syncMessage[28]);
//Serial.println(strlen(syncMessage));
strcat(syncMessage, "_#]");




Expand All @@ -338,13 +360,13 @@ void composeSyncMessage(){



void sendSyncMessage(){
void sendSyncMessage(uint8_t retry,uint8_t timeout=150){

composeSyncMessage();
syncMessage[6]='u'; //modify the message
syncMessage[7]='l'; //modify the message

if (radio.sendWithRetry(gateway_address, syncMessage, strlen(syncMessage),4,150)) {
Serial.println(" sendWithRetry sendSyncMessage executed");
if (radio.sendWithRetry(gateway_address, syncMessage, strlen(syncMessage),retry,timeout)) {
// note that the max delay time is 255..because is uint8_t
//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)
Expand All @@ -362,18 +384,31 @@ void sendSyncMessage(){
void getAddressFromGateway(){
Serial.println("getAddressFromGateway executed");

//[S_001ga3.05ProminiS0001_#]
//[S_123ga5.24WPlugAvx000810000x_#]

composeSyncMessage();
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.

Serial.println(" sendWithRetry getAddressFromGateway executed");

if (radio.sendWithRetry(gateway_address, syncMessage,strlen(syncMessage),4,150)) {
if (radio.sendWithRetry(gateway_address, syncMessage,strlen(syncMessage),radioRetry,radioTxTimeout)) {
// note that the max delay time is 255..because is uint8_t
//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");
/*
for (char a=0;a<(35);a=a+1){
Serial.print(syncMessage[a]);
}
Serial.println("end_get_address");
*/



skipRadioRxMsg=0; //reset the counter to allow this node to receive query
}

Expand Down Expand Up @@ -698,11 +733,11 @@ void handleButton(){
Serial.print("obj_button pressed");

while (digitalRead(obj_button_pin)==0){ //wait for button release
delay(100);//todo change it smaller
delay(200);//todo change it smaller
}

changeObjStatus(main_obj_selected,!main_obj_state); // this will make a not of current state
sendSyncMessage();
sendSyncMessage(radioRetry+2,radioTxTimeout);

}

Expand Down Expand Up @@ -802,7 +837,8 @@ void loop() {

uint8_t message_copy[rx_msg_lenght+1];

strcpy(filtered_onos_message,"");
//strcpy(filtered_onos_message,"");
memset(filtered_onos_message,0,sizeof(filtered_onos_message)); //to clear the array
Serial.print("msg_start:");
for (uint8_t counter = 0; counter <= rx_msg_lenght; counter++) {
filtered_onos_message[counter]=radio.DATA[counter];
Expand All @@ -827,7 +863,7 @@ void loop() {


}
Serial.println("msg_stop");
Serial.println(":msg_stop");


if ( (onos_cmd_start_position!=-99) && (onos_cmd_end_position!=-99 )){
Expand Down Expand Up @@ -876,7 +912,7 @@ radioTx:



handleButton();




Expand All @@ -894,7 +930,7 @@ radioTx:
get_address_timeout=millis();
Serial.print("radio address changed to:");
Serial.println(this_node_address);
sendSyncMessage();
sendSyncMessage(radioRetry,radioTxTimeout);


}
Expand All @@ -917,7 +953,7 @@ radioTx:

sync_time=millis();

sendSyncMessage();
sendSyncMessage(radioRetry,radioTxTimeout);


}
Expand Down
59 changes: 38 additions & 21 deletions arduino_code/onos_rf69_serial_transponder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ char syncMessage[28];
char str_this_node_address[4];
//////////////////////////////////End of Standard part to run decodeOnosCmd()//////////////////////////////////

uint8_t radioRetry=3; //todo: make this changable from serialport
uint8_t radioTxTimeout=70; //todo: make this changable from serialport

uint8_t counter=0;
char data_from_serial[rx_msg_lenght+5];
Expand All @@ -153,8 +155,8 @@ void composeSyncMessage(){
//[S_001sy3.05ProminiS0001_#]

int tmp_number=0;
strcpy(str_this_node_address,"");

//strcpy(str_this_node_address,"");
memset(str_this_node_address,0,sizeof(str_this_node_address)); //to clear the array
str_this_node_address[0]='0';
str_this_node_address[1]='0';
str_this_node_address[2]='0';
Expand All @@ -178,7 +180,8 @@ void composeSyncMessage(){
}


strcpy(syncMessage, "");
//strcpy(syncMessage, "");
memset(syncMessage,0,sizeof(syncMessage)); //to clear the array
strcpy(syncMessage, "[S_");
strcat(syncMessage, str_this_node_address);
if (first_sync==1 ){
Expand Down Expand Up @@ -496,8 +499,7 @@ boolean checkAndReceiveSerialMsg(){



counter=0;
message_to_decode_avaible=0;



/*
Expand All @@ -512,19 +514,21 @@ boolean checkAndReceiveSerialMsg(){
*/


counter=0;
message_to_decode_avaible=0;
timeout=millis()+200;

onos_cmd_start_position=-99;
onos_cmd_end_position=-99;
memset(data_from_serial,0,sizeof(data_from_serial)); //to clear the array

while (Serial.available() > 0) {

enable_answer_back=1;
// Serial.println(F("im"));
//Serial.println(counter);
// read the incoming byte:
//if (counter==0){
delayMicroseconds(210);//the serial doesnt work without this delay... to change if you change baud rate (increase with lower baud rate)
delayMicroseconds(210);//210 the serial doesnt work without this delay... to change if you change baud rate (increase with lower baud rate)
//}
data_from_serial[counter] = Serial.read();

Expand All @@ -541,6 +545,11 @@ boolean checkAndReceiveSerialMsg(){
Serial.println(counter);
Serial.println(F("end"));
counter=0;
Serial.println("start:");
for (uint8_t pointer = 0; pointer <= rx_msg_lenght; pointer++) {
Serial.print(data_from_serial[pointer]);
}
Serial.println(":end");
continue;
}

Expand All @@ -563,6 +572,7 @@ boolean checkAndReceiveSerialMsg(){
if( (data_from_serial[counter-2]=='_')&&(data_from_serial[counter-1]=='#')&&(data_from_serial[counter]==']') ){//
// Serial.println("cmd end found-------------------------------");
onos_cmd_end_position=counter-2;
break; //i have found a cmd ..I stop to listen to other cmd and analyze it..
}


Expand All @@ -574,7 +584,8 @@ boolean checkAndReceiveSerialMsg(){

uint8_t message_copy[rx_msg_lenght+1];

strcpy(filtered_onos_message,""); //clear the filtered_onos_message array
// strcpy(filtered_onos_message,""); //clear the filtered_onos_message array
memset(filtered_onos_message,0,sizeof(filtered_onos_message)); //to clear the array

for (uint8_t pointer = 0; pointer <= rx_msg_lenght; pointer++) {
filtered_onos_message[pointer]=data_from_serial[onos_cmd_start_position+pointer];
Expand Down Expand Up @@ -609,7 +620,7 @@ boolean checkAndReceiveSerialMsg(){

boolean ForwardSerialMessageToRadio(){

if (radio.sendWithRetry(received_message_address, filtered_onos_message, strlen(filtered_onos_message),1,200)) {
if (radio.sendWithRetry(received_message_address, filtered_onos_message, strlen(filtered_onos_message),radioRetry,radioTxTimeout)) {
// note that the max delay time is 255..because is uint8_t
//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)
Expand Down Expand Up @@ -691,11 +702,11 @@ void sendSerialAnswerFromSerialMsg(){

}


memset(received_message_answer,0,sizeof(received_message_answer)); //to clear the array
strcpy(received_message_answer,"[S_nocmd2_#]");
strcpy(filtered_onos_message,"");

Serial.flush(); //make sure all serial data is clocked out before sleeping the
//strcpy(filtered_onos_message,"");
memset(filtered_onos_message,0,sizeof(filtered_onos_message)); //to clear the array
Serial.flush(); //make sure all serial data is clocked out
enable_answer_back=0;


Expand All @@ -719,7 +730,8 @@ boolean checkAndHandleIncomingRadioMsg(){
onos_cmd_start_position=-99;
onos_cmd_end_position=-99;

strcpy(filtered_onos_message,"");
//strcpy(filtered_onos_message,"");
memset(filtered_onos_message,0,sizeof(filtered_onos_message)); //to clear the array

for (uint8_t counter = 0; counter <= rx_msg_lenght; counter++) {
filtered_onos_message[counter]=radio.DATA[counter];
Expand Down Expand Up @@ -767,8 +779,10 @@ boolean checkAndHandleIncomingRadioMsg(){

}
else{
strcpy(received_message_answer,"[S_nocmd0_#]");
Serial.println(F("error in message nocmd0_#]"));
strcpy(received_message_answer,"[S_nocmd1_#]");
Serial.print(F("error in message nocmd1_#]"));
Serial.print('\n');
Serial.flush(); //make sure all serial data is clocked out
return(0);
}

Expand Down Expand Up @@ -892,12 +906,13 @@ sync:

restart:

strcpy(data_from_serial,"");

strcpy(filtered_onos_message,"");
//strcpy(data_from_serial,"");
//strcpy(filtered_onos_message,"");

memset(data_from_serial,0,sizeof(data_from_serial)); //to clear the array
memset(filtered_onos_message,0,sizeof(filtered_onos_message)); //to clear the array

if (skipUartRxMsg>1){ // skip uart message to allow incoming radio msg to be received
if (skipUartRxMsg>0){ // skip uart message to allow incoming radio msg to be received

skipUartRxMsg=0;
goto radioRxCheck;
Expand All @@ -918,6 +933,8 @@ restart:

else if(enable_answer_back==1) {
Serial.print(received_message_answer);
Serial.print('\n');
Serial.flush(); //make sure all serial data is clocked out
enable_answer_back=0;
}

Expand Down

0 comments on commit b02e5ca

Please sign in to comment.