Go Down

Topic: MKRWAN 1300 not receiving uart (Read 375 times) previous topic - next topic

thebakker

Hello,
I'm using a honeywell HPM sensor, and you have to send it commands via uart
Datasheet

I send the correct codes to the sensor, but I'm not receiving anything.
Connections:
sensor/mkrwan 1300
gnd/gnd
RX/TX(pin 14)
TX/RX (pin 13)

the sensor is connected to an external 5V power source

If I look, I see the arduino sending the values, but not receiving. I even tried running the arduino without connection to the pc (external power source)

Any ideas/ suggestions?

Code: [Select]

bool my_status;
// IMPORTANT!!! When working on an Arduino DUE,
// int is 32 bit (-2,147,483,648 to 2,147,483,647)
// For Arduino Uno int size is 8 bit, that is -32,768 to 32,767
// Use long or float if working with an Uno or simmilar 8-bit board
long PM25;
long PM10;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.begin(9600);
  //while (!Serial);
  //Serial.println("Start!");
  //Serial.begin(9600);
  //Just discard everything the sensor will send at the beginning
  digitalWrite(LED_BUILTIN, HIGH);
  flush_Serial();
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  // Stop autosend
  //Serial.println("Stopping autosend...");
  digitalWrite(LED_BUILTIN, HIGH);
  my_status = stop_autosend();
  digitalWrite(LED_BUILTIN, LOW);
  // Serial print is used just for debugging
  // But one can design a more complex code if desired
 // Serial.print("Stop autosend status is ");
  //Serial.println(my_status, BIN);
  //Serial.println(" ");
  delay(1000);


  // Start fan
  //Serial.println("Starting measurements...");
  digitalWrite(LED_BUILTIN, HIGH);
  my_status = start_measurement();
   digitalWrite(LED_BUILTIN, LOW);
   delay(1000);
  // Serial print is used just for debugging
  // But one can design a more complex code if desired
  //Serial.print("Start measurement status is ");
  //Serial.println(my_status, BIN);
  //Serial.println(" ");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(5000);
  digitalWrite(LED_BUILTIN, LOW);
}


void loop() {
  // Read the particle data every minute
  my_status = read_measurement();
  // Serial print is used just for debugging
  // But one can design a more complex code if desired
  //Serial.print("Read measurement status is ");
  //Serial.println(my_status, BIN);
  //Serial.print("PM2.5 value is ");
  //Serial.println(PM25, DEC);
  //Serial.print("PM10 value is ");
  //Serial.println(PM10, DEC);
  //Serial.println(" ");
  // Wait some time
  delay(10000);
}

bool start_measurement(void)
{
  // First, we send the command
  byte start_measurement[] = { 0x68, 0x01, 0x01, 0x96 };
  Serial.write(start_measurement, sizeof(start_measurement));
  //Then we wait for the response
  while (Serial.available() < 1);
  uint8_t read1 = Serial.read();
  while (Serial.available() < 1);
  uint8_t read2 = Serial.read();
  // Test the response
  if ((read1 == 0xA5) && (read2 == 0xA5)) {
    // ACK
    return 1;
  }
  else if ((read1 == 0x96) && (read2 == 0x96))
  {
    // NACK
    return 0;
  }
  else return 0;
}

bool stop_measurement(void)
{
  // First, we send the command
  byte stop_measurement[] = { 0x68, 0x01, 0x02, 0x95 };
  Serial.write(stop_measurement, sizeof(stop_measurement));
  //Then we wait for the response
  while (Serial.available() < 1);
  uint8_t read1 = Serial.read();
  while (Serial.available() < 1);
  uint8_t read2 = Serial.read();
  // Test the response
  if ((read1 == 0xA5) && (read2 == 0xA5)) {
    // ACK
    return 1;
  }
  else if ((read1 == 0x96) && (read2 == 0x96))
  {
    // NACK
    return 0;
  }
  else return 0;
}

bool read_measurement(void)
{
  // Send the command 0x68 0x01 0x04 0x93
  byte read_particle[] = { 0x68, 0x01, 0x04, 0x93 };
  Serial.write(read_particle, sizeof(read_particle));
  // A measurement can return 0X9696 for NACK
  // Or can return eight bytes if successful
  // We wait for the first two bytes
  while (Serial.available() < 1);
  byte HEAD = Serial.read();
  while (Serial.available() < 1);
  byte LEN = Serial.read();
  // Test the response
  if ((HEAD == 0x96) && (LEN == 0x96)) {
    // NACK
    Serial.println("NACK");
    return 0;
  }
  else if ((HEAD == 0x40) && (LEN == 0x05))
  {
    // The measuremet is valid, read the rest of the data
    // wait for the next byte
    while (Serial.available() < 1);
    byte COMD = Serial.read();
    while (Serial.available() < 1);
    byte DF1 = Serial.read();
    while (Serial.available() < 1);
    byte DF2 = Serial.read();
    while (Serial.available() < 1);
    byte DF3 = Serial.read();
    while (Serial.available() < 1);
    byte DF4 = Serial.read();
    while (Serial.available() < 1);
    byte CS = Serial.read();
    // Now we shall verify the checksum
    if (((0x10000 - HEAD - LEN - COMD - DF1 - DF2 - DF3 - DF4) % 0XFF) != CS) {
      Serial.println("Checksum fail");
      return 0;
    }
    else
    {
      // Checksum OK, we compute PM2.5 and PM10 values
      PM25 = DF1 * 256 + DF2;
      PM10 = DF3 * 256 + DF4;
      return 1;
    }
  }
}

bool stop_autosend(void)
{
  // Stop auto send
  byte stop_autosend[] = { 0x68, 0x01, 0x20, 0x77 };
  Serial.write(stop_autosend, sizeof(stop_autosend));
  //Then we wait for the response
  while (Serial.available() < 1);
  digitalWrite(LED_BUILTIN, LOW);
  uint8_t read1 = Serial.read();
  while (Serial.available() < 1);
  digitalWrite(LED_BUILTIN, HIGH);
  uint8_t read2 = Serial.read();
  // Test the response
  if ((read1 == 0xA5) && (read2 == 0xA5)) {
    // ACK
    return 1;
  }
  else if ((read1 == 0x96) && (read2 == 0x96))
  {
    // NACK
    return 0;
  }
  else return 0;
}

bool start_autosend(void)
{
  // Start auto send
  byte start_autosend[] = { 0x68, 0x01, 0x40, 0x57 };
  Serial.write(start_autosend, sizeof(start_autosend));
  //Then we wait for the response
  while (Serial.available() < 2);
  uint8_t read1 = Serial.read();
  uint8_t read2 = Serial.read();
  // Test the response
  if ((read1 == 0xA5) && (read2 == 0xA5)) {
    // ACK
    return 1;
  }
  else if ((read1 == 0x96) && (read2 == 0x96))
  {
    // NACK
    return 0;
  }
  else return 0;
}

void flush_Serial(void) {
  uint8_t inchar;
  uint8_t count = 0;
  bool is_timeout = 0;
  // We set a 10 second timeout;
  unsigned long timeout = millis() + 10000;
  //Serial.println("Waiting for the first autosend.");
  while (Serial.available() < 1) {
    if (millis() > timeout) {
      //Serial.println("Timeout");
      break;
    }
  }
  // Do we have data in the serial buffer?
  // If so, flush it
  if (Serial.available() > 0) {
    //Serial.println("Flushing buffer...");
    // A data frame is 32 bytes
    while (count < 32) {
      inchar = Serial.read();
      count++;
      delay(10);
      //Serial.print(inchar, HEX);
    }
    //Serial.println("");
  }
}

MITEL

Pins 13 and 14 are mapped to Serial1, not Serial (that is USB).
So you are sending codes to USB and not to the sensor. 

thebakker

Pins 13 and 14 are mapped to Serial1, not Serial (that is USB).
So you are sending codes to USB and not to the sensor. 
Thank you very much, it works now.

Go Up