Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial problems #57

Open
msillano opened this issue May 4, 2020 · 1 comment
Open

Serial problems #57

msillano opened this issue May 4, 2020 · 1 comment

Comments

@msillano
Copy link

msillano commented May 4, 2020

Hi
In my project, I need to use the serial input. My source is a sensor, it sends every second 10 bytes (any value) @9600. Tested it using Termite (Win terminal hex) and FTDI. It is OK.
On sonoff basic, using my test code:

config system_output 0
config bitrate 9600
<omissis>
on serial
do
setvar $ser = "(" | $this_serial | ")"
<omissis>

A) with serial RX open, it works as expected: Telnet ok, I can do commands
B) with RX connected to the serial source (TX not used: open)
Telnet not usable: fort every command it give me "Invalid command"

         CMD>show
         Invalid command
         CMD>
  Connecting and disconnecting the source, to show vars, I get:
CMD>show vars

<omissis>

oo: 1

ser: eendifendifenH▒@

<omissis>

i.e, the variable $ser is garbage.

Because data are bin, I think Termite uses a short timeout (polling 100 ms) to define the packet end.

In my application, I can lose some packets, and the CRC is used to certificate received packets, so the use of also CR/LF as line terminator is not a terrible problem.

Where I make errors?
Best regards
m.s.

P.S.
On WEMOS D1 R2 that works:

#include <SoftwareSerial.h>

#if defined(ESP8266) && !defined(D5)
  #define D5 (14)  // RX
  #define D6 (12)  //TX
#endif

SoftwareSerial swSer;  // uses SoftwareSerial to reserve Serial

int avrg[60];  // O3 data from  ZE25-O3
int k = 0;     // avrg index
int c = 10;    // mobile avg size
int getConcO3(){
    long tot = 0;
    for (int j = 0; j<c; j++) tot += avrg[j];
    int res = int(tot/c);
    Serial.print( "ozone = ");
    Serial.println( res );
    return res;
 }
 
void sensorRead() {
  byte dataRX[15];  //byte type array; it can hold 15 data bytes
  byte n = swSer.available();    //check if a character has arrived and saved in FIFO buffer
  if(n > 0) { 
    n = swSer.readBytes(dataRX, 15);
    for  (int i=0;  i< n; i++){
      Serial.print( dataRX[i], HEX);
      Serial.print( ", ");
      }
    //  test some const values (no CRC) to accept data
    if ((n == 9) && (dataRX[0]== 255) && (dataRX[3]== 0) && (dataRX[7]== 16)) {
      avrg[k] =  dataRX[5] + dataRX[4] *256; // store concentration
      k = ++k % c;
      }else
       Serial.print( "bad");
    Serial.println();
    }
  }

void setup() {
    Serial.begin(115200);
    swSer.begin(9600, SWSERIAL_8N1, D5, D6, false, 95, 11);
}

void loop() {
  sensorRead();
  int concO3 = getConcO3();
  delay(50);
}

A solution is therefore WEMOS + MQTTBroker... But I like more the Sonoff form factor (It size is 1/4) so I wanna use it :).
Best regards

p.s.
Close the reception on (NL) char limit the use to ASCII mode.
Adding also a timeout extends it to binary too. Timeout user-defined.
Best regards
m.s.

@9600
Copy link

9600 commented May 4, 2020

Hello, you called?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants