Skip to content

Commit

Permalink
modified a lot of parts...now the radio communication is faster and m…
Browse files Browse the repository at this point in the history
…ore stable (tried only one node)
  • Loading branch information
marco committed Dec 25, 2016
1 parent b02e5ca commit e9d315c
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 59 deletions.
22 changes: 14 additions & 8 deletions arduino_code/onos_plug_node_WPlugavx/onos_plug_node_wplugavx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ 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
uint8_t radioRetry=3; //todo: make this changable from serialport
uint8_t radioTxTimeout=20; //todo: make this changable from serialport
char received_serial_number[13];
# define gateway_address 1

Expand Down Expand Up @@ -202,13 +202,13 @@ 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(50);
delay(25);
digitalWrite(relay1_set_pin,0);
digitalWrite(relay1_reset_pin,0);
digitalWrite(relay2_set_pin,0);
digitalWrite(relay2_reset_pin,0);
main_obj_state=status_to_set;
digitalWrite(obj_led_pin,status_to_set);
main_obj_state=status_to_set;
return(1);
}

Expand Down Expand Up @@ -345,6 +345,11 @@ void composeSyncMessage(){
*/
syncMessage[strlen(syncMessage)]=main_obj_state+48; //+48 for ascii translation


Serial.print("composeSyncMessage executed with status:");
Serial.println(main_obj_state);


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]);
Expand Down Expand Up @@ -465,7 +470,7 @@ void decodeOnosCmd(const char *received_message){
Serial.println(received_message[6]);
*/

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


Expand Down Expand Up @@ -548,7 +553,6 @@ void decodeOnosCmd(const char *received_message){
get_decode_time=millis();
*/

main_obj_state=received_message[8]-48;

received_message_value=received_message[9]-48;

Expand Down Expand Up @@ -583,7 +587,7 @@ void decodeOnosCmd(const char *received_message){


if (change_status_ok!=1){

memset(received_message_answer,0,sizeof(received_message_answer)); //to clear the array
Serial.println(F("er_chobjstatus_#]"));
strcpy(received_message_answer,"er_chobjstatus_#]");
return;
Expand Down Expand Up @@ -733,12 +737,13 @@ void handleButton(){
Serial.print("obj_button pressed");

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

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


}


Expand Down Expand Up @@ -880,6 +885,7 @@ void loop() {
if (radio.ACKRequested()){
radio.sendACK();
Serial.println(" - ACK sent");
sync_time=millis();
}
//interrupts(); // Enable interrupts

Expand Down
8 changes: 5 additions & 3 deletions arduino_code/onos_rf69_serial_transponder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ 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 radioTxTimeout=60; //todo: make this changable from serialport

uint8_t counter=0;
char data_from_serial[rx_msg_lenght+5];
Expand Down Expand Up @@ -912,14 +912,16 @@ restart:
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>0){ // 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;
}
skipUartRxMsg=skipUartRxMsg+1;

*/
serial_msg_to_decode_is_avaible=checkAndReceiveSerialMsg();


Expand Down
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ versione4.53 iniziato ad usare pythonserial come libraria dopo test fatti con
The system is not 100% secure to change the status.
There is some problem for onos to receive data from radio..
started using if self.ser.inWaiting()>6: in arduinoserial.py
started using memset(filtered_onos_message,0,sizeof(filtered_onos_message));
started using memset(filtered_onos_message,0,sizeof(filtered_onos_message));
to clear the arrays in the arduino programs,before i used strcpy(filtered_onos_message,"") that doesn't work well
reduced the radio.sendWithRetry timeout from 200ms t0 75ms and increased the retry from 1 to 3

Expand Down
64 changes: 48 additions & 16 deletions scripts_folder/arduinoserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def read_data(self): # thread function
break
buf=''

count=0




Expand Down Expand Up @@ -222,27 +222,50 @@ def read_data(self): # thread function
print "can't flush output"+str(e.args)
errorQueue.put( "can't flush output"+str(e.args) )


waitTowriteUntilIReceive=0
while (self.exit==0):
time.sleep(0.1)
#time.sleep(0.1)

if self.ser.inWaiting()<5: #skip if there is no incoming data
if self.ser.inWaiting()<1: #skip if there is no incoming data
time.sleep(0.1)
continue
else:
##################################################################
try:
byte = self.ser.read(1) # self.usbR.read(1)
#print byte
except:
byte=-1
self.status=0
continue

if byte=="\x00":
continue

if (ord(byte)==10): # 10 is the value for new line (\n) end of packet on incoming serial buffer
print "end of serial packet for /n"
break
else:
#print "in byte="+byte+" end of in byte"
buf=buf+byte


if len(buf)>5:
waitTowriteUntilIReceive=1
if ( (buf.find("[S_")!=-1)&(buf.find("_#]")!=-1) ): #there is a full onos command packet
print "end of serial packet:_#] "
break


if self.ser.inWaiting()>6:
buf=self.ser.read(self.ser.inWaiting())
waitTowriteUntilIReceive=1
if ( (buf.find("[S_")!=-1)&(buf.find("_#]")!=-1) ): #there is a full onos command packet
print "end of serial packet:_#] "
break
if (buf.find("\n")!=-1):
print ("end of line received but no onoscmd found")
break

print(buf)


##################################################################




Expand Down Expand Up @@ -274,7 +297,7 @@ def read_data(self): # thread function



if( (cmd[2]=="o")&(cmd[3]=="k") ): # S_ok003dw060005_# i recived a confirm from the node
if( (cmd[2]=="o")&(cmd[3]=="k") ): # S_ok003dw060005_# i received a confirm from the node


#with lock_serial_input:
Expand All @@ -295,7 +318,7 @@ def read_data(self): # thread function
node_fw=cmd[8:12]
node_address=cmd[3:6]

if ((cmd[6]=="u")&(cmd[7]=="l")): #todo variable data extraction [S_123ul5.24WPlugAvx000810000_#]
if ((cmd[6]=="u")&(cmd[7]=="l")): #todo sensor value data extraction [S_123ul5.24WPlugAvx000810000_#]
obj_value=cmd[24]
obj_number_to_update="0"
priorityCmdQueue.put( {"cmd":"updateObjFromNode","nodeSn":serial_number,"nodeAddress":node_address,"nodeFw":node_fw,"objects_to_update":{obj_number_to_update:obj_value} })
Expand All @@ -313,6 +336,7 @@ def read_data(self): # thread function
errorQueue.put("error receiving serial sync message cmd was :"+cmd+ "e:"+str(e.args) )

priorityCmdQueue.put( {"cmd":"createNewNode","nodeSn":serial_number,"nodeAddress":node_address,"nodeFw":node_fw })
waitTowriteUntilIReceive=0
continue


Expand All @@ -332,14 +356,15 @@ def read_data(self): # thread function
priorityCmdQueue.put( {"cmd":"sendNewAddressToNode","nodeSn":serial_number,"nodeAddress":node_address,"nodeFw":node_fw})


priorityCmdQueue.put( {"cmd":"createNewNode","nodeSn":serial_number,"nodeAddress":node_address,"nodeFw":node_fw })
priorityCmdQueue.put( {"cmd":"createNewNode","nodeSn":serial_number,"nodeAddress":node_address,"nodeFw":node_fw })
waitTowriteUntilIReceive=0
continue


except Exception, e :
print "error receiving serial sync message cmd was :"+cmd+ "e:"+str(e.args)
errorQueue.put("error receiving serial sync message cmd was :"+cmd+ "e:"+str(e.args) )




Expand Down Expand Up @@ -419,16 +444,23 @@ def write(self, data):#test..
#self.ser.flushOutput()
self.ser.flush()
#if self.ser.flush()()>0: #if there is something on the output buffer wait a bit
# time.sleep(0.01)
#time.sleep(0.01)
# print("wait for self.ser.out_waiting self.ser.out_waitingself.ser.out_waitingself.ser.out_waiting")


#start_time=time.time()
#while waitTowriteUntilIReceive==1:

# if (time.time()>(start_time+0.5) ):#2 #timeout to exit the loop
# print "rx after write timeout0"
time.sleep(0.02)
self.ser.write(data)

self.ser.flush()
#while self.ser.inWaiting()<5:
# time.sleep(0.01)

#time.sleep(0.3)
time.sleep(0.5)

#answer=self.ser.read(self.ser.inWaiting())
# if len(self.readed_packets_list)>0:
Expand Down
Binary file modified scripts_folder/arduinoserial.pyc
Binary file not shown.
26 changes: 13 additions & 13 deletions scripts_folder/config_files/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"WPlugAvx0008": {
"hwModelName": "WPlugAvx",
"nodeAddress": "003",
"nodeAddress": "004",
"node_serial_number": "WPlugAvx0008"
}
},
Expand Down Expand Up @@ -209,7 +209,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": 0,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -352,7 +352,7 @@
],
"priority": 10,
"scenarios": [],
"status": "inactive",
"status": 0,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -387,7 +387,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": 0,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -528,7 +528,7 @@
],
"priority": 10,
"scenarios": [],
"status": "inactive",
"status": 1,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -599,7 +599,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": 0,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -705,7 +705,7 @@
],
"priority": 0,
"scenarios": [],
"status": 24,
"status": 25,
"styleDict": {
"0": "background-color:green;",
"1": "background-color:red;",
Expand Down Expand Up @@ -741,7 +741,7 @@
],
"priority": 0,
"scenarios": [],
"status": 1281,
"status": 244,
"styleDict": {
"0": "background-color:green;",
"1": "background-color:red;",
Expand Down Expand Up @@ -785,7 +785,7 @@
"scenario5",
"scenario4"
],
"status": 21,
"status": 4,
"styleDict": {
"0": "background-color:green;",
"1": "background-color:red;",
Expand Down Expand Up @@ -831,7 +831,7 @@
"scenario4",
"scenario8"
],
"status": 21,
"status": 4,
"styleDict": {
"0": "background-color:green;",
"1": "background-color:red;",
Expand Down Expand Up @@ -974,7 +974,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": 1,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -1044,7 +1044,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": "onoswait",
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down Expand Up @@ -1190,7 +1190,7 @@
],
"priority": 0,
"scenarios": [],
"status": "inactive",
"status": 0,
"styleDict": {
"0": "background-color:#A9E2F3;",
"1": "background-color:#8181F7;",
Expand Down

0 comments on commit e9d315c

Please sign in to comment.