[OpenTRV-interest] Programatically sending command to an OpenTRV board

Alasdair Macdonald EMAIL ADDRESS HIDDEN
Thu Nov 6 14:11:30 GMT 2014


I've been gathering the output from an OpenTRV device since May, and
having switched the heating on in the past couple of weeks, I'm now
trying to send commands to it, programatically. And failing.
(Receiving data has been working perfectly).

I am using fhem (which is written in perl). I created a module for the
board using code from a couple of existing hardware devices as a
basis, primarily referring to the RFXcom RFXtrx433 module. This device
connects to a PC via a USB cable and appears the same as the OpenTRV
board, as a /dev/ttyUSBx device. The logic that allows me to read data
from the RFXtrx433 required very little change for OpenTRV. There is
one additional factor that is of interest; fhem has a "single stage"
model and a "two stage" model; since the RFXcom device sits between a
computer and remote "input" devices (weather / PRI sensors) and
"output" devices (remotely controlled wall sockets & light switches),
this device uses the two stage model, and the RXFcom hardware doesn't
sense or report any interesting data from itself. For the OpenTRV
board, I amended code to fit the single stage model, because I *do*
want to pick up data from the device itself. I am not yet capturing
data from any remote devices via the OpenTRV board; I think that if I
upgrade the firmware I should be able to do so (at which point I'll
need to reactivate some two stage code).


But my problem is figuring out how to write to the board. I can
manually send an "S" (with end of line characters?) from the Arduino
IDE serial monitor, and receive interesting data, but the timing is
very picky. As far as I can tell, I must enter my data AFTER the ">"
prompt has appeard, but before the line feed that follows it and
causes the text within the serial monitor window to scroll up.


I note the following lines of code in UI_Minimal.cpp:


<code>
  // Generate and flush prompt character to the user, after a CRLF to
reduce ambiguity.
  Serial.println();
  Serial.print(CLIPromptChar);
  // Idle a short while to try to save energy, waiting for serial TX
end and possible RX response start.
  flushSerialSCTSensitive();


  // Wait for input command line from the user (received characters
may already have been queued)...
  // Read a line up to a terminating CR, either on its own or as part of CRLF.

<followed by the logic that reads serial input>

<end code>


I can do things in fhem at the point when I have read a ">" prompt. I
have been able to observe that I am receiving 0x0d0a line endings from
the OpenTRV, and I have attempted to execute logic for writing to the
serial port around the point where I receive the prompt. But I don't
know exactly when I should be trying to do so, and not a single
attempt has succeeded so far. I shall of course also need to use a
suitable write command, and fhem provides me with more than one
choice, so I'm not certain that I'm doing that part of it right
either.

fhem has some logic (the background to which is beyond me) that
apparently automatically executes a Device_Read subroutine when (and
only when) there is data  available in the serial port. Comments in
the code, and info gleaned from the fhem website (much of which is
written in German) state that this behaviour is specific to linux
(which I am using); that a Windows host cannot achieve this in the
same way. I'm not clear if this is inherent to perl, or if it is
behaviour of a perl module that fhem uses.

So all I know is that I am launched in a routine that is successfully
reading bytes from the serial device; it's reading the characters one
at a time, hence I can choose to act after I receive the prompt, or
after I receive the carriage return after the prompt, or after the
line feed that follows it.

I've had no success with this for several days, so I'm asking for
advice. I don't believe anyone else here has referred to fhem, so I
don't expect advice that is specific to fhem. But I think I stand a
better chance if I have an answer to the following questions.


1. When exactly I should "reply" to the ">" prompt (I'm sorry, it
might be obvious to some, from UI_Minimal.cpp, but C is somewhat
foreign to me; actually perl is too). I thought that it should be
after the ">" but before the line endings, but since I'm having no
success, i'm asking here, to be sure.


2. Should I, or MUST I send line endings after my "S" command, and if
so, what might be preferred? The comment that I quoted within the code
above suggests that either CR or CRLF would be acceptable. Actually,
I'm not sure how I would vary this anyway, but I'm asking, just in
case it really matters.


3. Is it possible that the current source for UI_Minimal.cpp is
sufficiently different from my board that I cannot rely upon the
current source? (Change date shown by sourceforge is 2014-03-02)

My board: BoardVersion board V0.2 REV2; code 2014/Mar/23 18:16:08


4. Any other advice about how to programmatically send a command to an
OpenTRV board?


5. Are there instructions for flashing the board with new firmware, so
I can receive data from my remote device, per discussion in May?


References:

http://sourceforge.net/p/opentrv/code-0/2676/tree//trunk/Arduino/V0p2_Main/UI_Minimal.cpp

http://www.fhemwiki.de/wiki/DevelopmentModuleIntro

English translation; note the "Execution of modules" section:

https://translate.google.com/translate?hl=en&sl=de&tl=en&u=http%3A%2F%2Fwww.fhemwiki.de%2Fwiki%2FDevelopmentModuleIntro



For writing to my board, I am using the DevIo_SimpleWrite function in
DevIO.pm; within this is a "syswrite" command:

https://github.com/ntruchsess/fhem-mirror/blob/master/fhem/FHEM/DevIo.pm


More information about the OpenTRV-interest mailing list