Skip to content

Commit

Permalink
unoserver2.ino: Removed the second sendStart() in rfwrite(). This see…
Browse files Browse the repository at this point in the history
…ms to remove the problem of radio freezing after few days of opration.

                Also changed the return type of readRFM69() to return the lenght of the str.buf.
NaaradTopic2: addTime() called if "rf_fail" is found.
              Using comPort.myreadline()
comPort: Added the myreadline() function which recreates the Serial.readline() functionality by reading one char at a time.
naarad: Added a 1-sec delay after opening the com port connection for it to settle down.
  • Loading branch information
sanbee committed Jul 10, 2019
1 parent 075ff7f commit 700983a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
37 changes: 30 additions & 7 deletions BaseStation/unoserver2/unoserver2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ void loop()
// The RFM_SEND switch above loads the RFM_SEND command in
// TX_payload queue. readRFM69() call below is the one which
// should POP out the payload from TX_payload (it's a 2D array),
if ((msg = readRFM69())!=NULL)
if (readRFM69()!=0)
{
Serial.println(msg);
Serial.println(str.buf);
str.reset();
}
}
Expand Down Expand Up @@ -327,12 +327,34 @@ static void rfwrite(const Payload& P)
{
{
rf12_sleep(-1); //wake up RF module
while (!rf12_canSend()) rf12_recvDone();
rf12_sendStart(0, &P, sizeof P);
rf12_sendWait(1); //wait for RF to finish sending while in IDLE (1) mode (standby is 2 -- does not work with JeeLib 2018)

int i=0;
while (!rf12_canSend()) {rf12_recvDone();i++;}
rf12_sendStart(0, &P, sizeof P);
rf12_sendWait(1); //wait for RF to finish sending while in IDLE (1) mode (standby is 2 -- does not work with JeeLib 2018)
// rf12_sendStart(0, &P, sizeof P);
// rf12_sendWait(1); //wait for RF to finish sending while in IDLE (1) mode (standby is 2 -- does not work with JeeLib 2018)
// rf12_sleep(0); //put RF module to sleep

// int i=0;
// for(i=0;i<100;i++)
// if (rf12_canSend())
// {
// // while (!rf12_canSend()) rf12_recvDone();
// rf12_sendStart(0, &P, sizeof P);
// rf12_sendWait(1); //wait for RF to finish sending while in IDLE (1) mode (standby is 2 -- does not work with JeeLib 2018)
// rf12_sendStart(0, &P, sizeof P);
// rf12_sendWait(1); //wait for RF to finish sending while in IDLE (1) mode (standby is 2 -- does not work with JeeLib 2018)
// // rf12_sleep(0); //put RF module to sleep
// break;
// }
// else
// {
// rf12_recvDone();
// delay(10);
// }
// if (i > 0)
// Serial.println("{\"rf_fail\":1,\"source\":\"ERROR RFM_SEND\",\"Try\":"+String(i)+(" }\0"));
}
}
//####################################################################
Expand Down Expand Up @@ -412,7 +434,7 @@ static bool processACK(const int rx_nodeID, const int rx_rx, const int rx_supply
#define RX_HDR_OK() (((rf12_hdr & RF12_HDR_CTL) == 0))
#define RX_NODEID() ((rf12_hdr & 0x1F))

static char* readRFM69()
static byte readRFM69()
{
int payload_nodeID=NOTHING_TO_SEND;
bool isACK=false;
Expand Down Expand Up @@ -456,7 +478,8 @@ static char* readRFM69()
lastPktSent[listenerNdx] = millis();
TX_counter[listenerNdx]++;
}
return (str.fill==0)?NULL:str.buf;
// return (str.fill==0)?NULL:str.buf;
return str.fill;
}
//####################################################################
void writeOne()
Expand Down
7 changes: 4 additions & 3 deletions RPi/NaaradServer/NewServer/NaaradTopic2.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def __init__(self, name, uno, pktHndlr, hLength=6*60*60*1000.0):
# topicsSubscriberList["SensorDataSink"]).
def run(self):
while 1:
line="";
try:
line =self.uno.readline().rstrip();
except (AttributeError, UniocodeDecodeError) as excpt:
line =self.uno.myreadline().rstrip();
except (AttributeError, UnicodeDecodeError) as excpt:
print("Could not decode to utf-8: %s" %excpt);
line="";
#line =self.uno.getSerial().readline();
Expand All @@ -56,13 +57,13 @@ def run(self):

print("@@@: "+line);
#line = self.pktHndlr.addTimeStamp(line);
line = Utils.addTimeStamp("time",line);
#print("###: "+line);

rlock = threading.RLock();
with rlock:
try:
if (("rf_fail" in line)):
line = Utils.addTimeStamp("time",line);
jdict = json.loads(line);# The JSON parser
# print jdict;

Expand Down
38 changes: 36 additions & 2 deletions RPi/NaaradServer/NewServer/comPort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#Arduino (UNO in this case) related code lives here.
#
import serial as serial;
#from __future__ import print_function

class comPort:
'''
Expand All @@ -19,6 +20,9 @@ def __init__(self, port='/dev/ttyACM0', baudrate=19200):
self.com1.bytesize=serial.EIGHTBITS;
self.com1.parity=serial.PARITY_NONE;
self.com1.stopbits=serial.STOPBITS_ONE;
self.com1.timeout=10;
self.SOF = '{';
self.EOF = '}';

def getSerial(self):
return self.com1;
Expand All @@ -35,7 +39,37 @@ def send(self,str):
self.com1.write((str+"\n").encode());

def read(self,errors='ignore'):
return self.com1.read().decode(errors=errors);
#return self.com1.read().decode(errors=errors);
return self.com1.read().decode();

def readline(self,errors='ignore'):
return self.com1.readline().decode(errors=errors);
#return self.com1.readline().decode(errors=errors);
print("Waiting...");
tt=self.com1.readline().decode();
print("### "+str(tt));
return tt;

def myreadline(self):
# FIND START OF FRAME
sensor_data="";
temp="";
temp=self.com1.read().decode();
if (temp==""):
print("TO");
return temp;

while (temp != self.SOF):
# print(temp,end='');
#print('!'+temp);
temp=self.com1.read().decode();

# RECORD UNTIL END OF FRAME
# print(temp,end='');
while (temp != self.EOF):
sensor_data += str(temp);
temp = self.com1.read().decode();
# print(temp,end='');

sensor_data += str(temp);
return sensor_data;

1 change: 1 addition & 0 deletions RPi/NaaradServer/NewServer/naarad.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initNaarad():
#uno=comPort(port=settings5.NAARAD_COMPORT,baudrate=9600);
uno=comPort(port=settings5.NAARAD_COMPORT);
uno.open();
time.sleep(1);
#
# Instantiate the object that encapsulates communcation to the packet
# radio (RFM64CW) connected to UNO.
Expand Down

0 comments on commit 700983a

Please sign in to comment.