hi,
I have connected distance sensor to serial1 of photon,
as per their documentation I should send hex command to sensor
and I will get hex data from the sensor in reply
I am trying the send the above command to sensor(table1)
and trying to get data (table2)
I wrote following sketch:
// Thishttps://build.particle.io/build/5d8506fc3be99d0021c72496#flash #include statement was automatically added by the Particle IDE.
#include <math.h>
#include "Serial2/Serial2.h"
const int powerUp1 = D0;
byte turn_on[] = { 0xAA, 0x00, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x01 };//0xAA 0x00 0x01 0xBE 0x00 0x01 0x00 0x01 Checksum
byte turn_off[] = { 0xAA, 0x00, 0x01, 0xBE, 0x00, 0x01, 0x00, 0x00 };//0xAA 0x00 0x01 0xBE 0x00 0x01 0x00 0x00 Checksum
byte one_shot_slow[] = { 0xAA, 0x00, 0x00, 0x20, 0x00, 0x01, 0x00, 0x01, 0x22 };
byte one_shot_fast[] = { 0xAA, 0x00, 0x00, 0x20, 0x00, 0x01, 0x00, 0x02, 0x23 };
//checksum = address byte + register bytes + payload count bytes + all payload bytes,
void readDistance(){
digitalWrite(powerUp1, HIGH);
delay(1000);
//OPEN
Serial1.write(0x4F);
delay(1000);
String turn1 = Serial1.readString();
Particle.publish("turn1", turn1, PRIVATE);
while(true){
//1shot slow
Serial1.write(one_shot_slow, sizeof(one_shot_slow));
delay(5000);//4 seconds for 1 shot slow
int bytesAvailable = Serial1.available();
for(int i=0;i<bytesAvailable;i++){
byte response = Serial1.read();
Particle.publish("response", String(response), PRIVATE);
}
delay(1000);
}
//CLOSE
Serial1.write(0x43);
delay(1000);
String turn2 = Serial1.readString();
Particle.publish("turn2", turn2, PRIVATE);
}
void setup() {
pinMode(powerUp1, OUTPUT);
digitalWrite(powerUp1, HIGH);
Serial1.begin(19200, SERIAL_8N1);
}
void loop() {
readDistance();
}
I am sending hex command using Serial1.write (is this correct way of doing it?)
how do I read data into hex format(table2) from serial1?
I am not sure my firmware code is correct as I am not getting expected result
help appreciated