Serial communication to/from Photon over Wifi

@peekay123 you are spot on about that voltage divider. I can get the 15V to trigger a HIGH, but then the 12V falls in no-mans-land between Min HIGH and Max LOW. I didn’t want to add another psu. Is there a real reason why the analogue voltage read solution is not preferred? I realise the digital read is more reliable but is there any other reason?

@bruce_miranda, if all you are doing is reading “bits” and repeating them then yes, you can use the analog read solution. However, keep in mind that analog reads take time so this will affect the speed at which you can sample your input. With digital pins you can use interrupts to detect changes which is not available with analog reads. My advice is try with analog read to see if you get the results you expect. Otherwise you may need to go with the external comparator. :wink:

1 Like

The symbol rate is 2400 baud, so lets hope the analogue reads can keep up.

The ADC can easily keep up. You can probably do it using normal analogRead, but the example audio3 uses the ADC in DMA mode to automatically save samples in a buffer automatically. The example samples at 32 KHz for audio purposes, but the hardware can go way faster than that and saving one bit per sample would certainly be possible.

@bruce_miranda, 2400 baud represents a maximum of 2400 transitions per second . However since UART outputs are Non-Return-Zero, the number of transitions is likely to be much lower. The trick with using an analog input will be to preserve the “embedded” clock timing. As long as you use a fixed sampling frequency, you can easily reconstruct and maintain the timing on your digital output. At 2400 baud, following Nyquist sampling needs to be done at a minimum 4800Hz if not more.

All this said, looking over your original post, it seems you are reading an RS-232 signal. Why not use a MAX232 transceiver (receive only) to condition the voltages to TTL levels. You could then use the Serial1 port on the Photon. Is this whole analog input thing overthinking the problem?

1 Like

Its not true RS232. It’s the eBus protocol used by Vaillant. Low is +9-12V and high is +15-24v

@bruce_miranda, found this topic on the Mikrocontroller forum:

http://www.mikrocontroller.net/topic/258719

There is a simple interface using an opto-isolator for the eBus receiver shown in the topic’s schematics:

With that you could drive RX on Serial1 directly. :wink:

3 Likes

Using an arduino duemilanove which has an FTDI chip already connected to it, I am able to read the eBus data using a simple voltage divider on A0 and doing a digitalwrite HIGH/LOW to D1 of the arduino board. The FTDI chip seems to be able to take this TTL input and generate the serial output on the USB which my program is able to read fine.
Trying to move this same design over to the Photon and I have hit some difficulties. I’ve adapted the voltage divider to the Photon’s 3.3V max on the analogue input and am able to detect a high or low. But I don’t know how to pass that over the USB port of the Photon to the computer to be read as Serial data. All I can see are 0s and 1s but not the normal HEX strings that I was expecting.

My photon code looks like

   inputvalue = analogRead(inPin);
if (inputvalue > highvalue) Serial.write(HIGH);
if (inputvalue < lowvalue) Serial.write(LOW);

What exactly do you expect?
I don’t think that you’d get a HEX string with that code on Arduino either.
You are only reading one bit at a time, but don’t say how many bits/bytes you want to be translated into one HEX output.

This would give you at least a binary string tho’

if (inputvalue > highvalue) Serial.write('1');
if (inputvalue < lowvalue) Serial.write('0');

My arduino code is below and that works fine. The D1 on the arduino is connected to an FTDI chip and I’m simply writing a HIGH or LOW on the D1. The reading program on the PC opens the com port with the relevant settings. I’m after something similar for the photon.

  inputvalue = analogRead(inPin);
  if (inputvalue > highvalue) digitalWrite(1, 1);
  if (inputvalue < lowvalue) digitalWrite(1, 0);

One digitalWrite() on the Arduino sends one bit to the FTDI but one Serial.write() sends a full byte (0x00 or 0x01) to the computer.

So how do I mimic the digitalWrite on the Arduino to the PC via the FTDI on the Photon’s USB to the PC?

Hi @bruce_miranda

Let me try to jump in here: the USB serial connection on a Photon is available to your code as Serial, so Serial.begin(9600); in setup() and any Serial.write() or Serial.print(‘some chars’); after that. So @ScruffR’s code above with Serial.write(); will send the ASCII character 0 or 1 to the PC via USB. If you want some other character or string, you can substitute it in his code.

To mimic the Arduino/FTDI combo you’d do the same thing as the FTDI.
It collects all the bits till it has a full bit-packet (including start/stop and priority bit) then wraps it up in a byte to transmit and sends that.
In order to do that you’d need to know the exact protocol (number of stop bits, parity none/even/odd) and filter out the protocol “overhead” to create a clean byte which you then can send via Serial.write()

If you want to see how something like that’s done, you can have a look at my implementation of ParticleSoftSerial

https://github.com/ScruffR/ParticleSoftSerial/blob/master/firmware/ParticleSoftSerial.cpp
especially the implementation of void ParticleSoftSerial::rxTimerISR()

or Arduino NewSoftSerial

https://github.com/sirleech/NewSoftSerial/blob/master/NewSoftSerial.cpp

1 Like

I got the clue when @ScruffR said that Serial.write send an entire byte. So guessed that I might have to start reconstructing the byte from the bits that I have gathered from the analogReads. Glad there is an example I can base my code on. I’ll let you guys know how I get on.

Lets hope the Photon is able to do all this byte reconstruction while also reading the DHT sensor and Publishing those values.

If I'm not mistaken, you can only read that sensor every two seconds, so there should be ample time inbetween :slight_smile:

Today when trying out @ScruffR SoftSerial example, I noticed that the Photon was printing a whole load of statements related to the wifi connections, going up an down. I didn’t realise the Photon would print all that on the Serial. Is there a way to get complete control of anything that is printed on the Serial port?

I wouldn’t have thought that’d happen unless you had a SerialLogHandler instantiated without filtering these diagnostic logs.
Can you show the output - and maybe your code too?

Doh! SerialLogHandler logHandler; was within your example code.

I was trying to cheat and tried this and ofcourse it didn’t work. I connected the outPin to the Serial1 RX and then was hoping to send that via the USB port to the PC. I got all sorts of weird text as well, which I wasn’t expecting.

if (inputvalue > highvalue) digitalWrite(outPin, 1);
if (inputvalue < lowvalue) digitalWrite(outPin, 0);  
if (Serial1.available()) Serial.write (Serial1.read());