Hey All,
I have a bit of a project that I am working on with an ESP32 board. The gist of what I'm trying to achieve is to make a ultrasonic wind gauge bluetooth controllable by a PC running specialist software for field event scoring.
Currently I'm using a 30m cable and people don't know how to roll cables, which is killing me!
I have everything working in pieces but the second I try to put it together I run into issues. The wind gauge itself and the communications protocol are non-negotiable. They are worth $5k and need to be made to international specifications. The wind gauge expects commands in the following format:
<0x01><0x13>CWI<0x02><data><0x04>
Where data is the amount of time for the wind reading. This then gets converted to two individual hex characters to be sent. This is the bit that's causing me issues.
I would really like to using Bluetooth be able to send a decimal number and the wind gauge gets ran for that amount of time. The issue I have got is much Google searching has left me still scratching my head on how to convert these two charcters to hex. I know I'm missing something simple but I just can't seem to get there with it.
So far my (non-working) code is
#include "BluetoothSerial.h"
#include <HardwareSerial.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(9600, SERIAL_7E1);
SerialBT.begin("ESP32test"); //Bluetooth device name
}
void startwg() {
delay(200);
byte wgstart[] = {0x01, 0x13, 0x43, 0x57, 0x53, 0x02, 0x30, 0x30, 0x04};
Serial.write(wgstart, sizeof(wgstart));
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
String data = SerialBT.readStringUntil('\\');
byte wginit[] = {0x01, 0x13, 0x43, 0x57, 0x49, 0x02};
Serial.write(wginit, sizeof(wginit));
//, 0x30, 0x35, 0x04
char tens = data.charAt(0);
char ones = data.charAt(1);
Serial.write(strtoul(tens));
Serial.write(strtoul(ones));
Serial.write("0x04");
startwg();
}
delay(20);
}
I know this is rough, but I'm just trying to get the basics working and then I'll pretty it up.
Just as a comparison, I can get the wind gauge to work as a hard coded app, but this isn't overly helpful to me:
#include <HardwareSerial.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(9600, SERIAL_7E1);
}
void loop() {
// put your main code here, to run repeatedly:
byte wginit[] = {0x01, 0x13, 0x43, 0x57, 0x49, 0x02, 0x30, 0x35, 0x04};
byte wgstart[] = {0x01, 0x13, 0x43, 0x57, 0x53, 0x02, 0x30, 0x30, 0x04};
Serial.write(wginit, sizeof(wginit));
delay(200);
Serial.write(wgstart, sizeof(wgstart));
delay(6000);
byte wgout[] = {0x01, 0x13, 0x43, 0x57, 0x4f, 0x02, 0x30, 0x30, 0x04};
Serial.write(wgout, sizeof(wgout));
delay(10000);
}
Is there a way that I can get what I am trying to achieve done, I have tried a fair bit of stuff I have seen suggested. I thought I was onto a winner with Serial.Print, apparently this isn't included in HardwareSerial, and from everything I read I don't want to use SoftwareSerial, however, I'm considering that at this low baud rate it might be ok.
One other thing I have is this needs to be reliable and work every time without fail.
Thanks in advance.