"%2x" means parse 2 characters as if they were hexadecimal The "%*1x" means parse a single char and throw it away.
then my hunch a quite off but i got the idea thx
If you are getting byte data, you could read it all into the bufferUSB and then maybe reassemble it like this:
I tried using your code, but I think it gets the set of data (?) for now I'm just using the one that you made, and the value seems wrong especially for the temperature
I turned off the HEX translation so and just showing byte data
I think you made a typo here
// wind_direction = int(data[4:6],16) # wind direction in degrees
temp = bufferUSB[7]<<16 | bufferUSB[8] <<8 | bufferUSB[9];
wind_direction = (temp - 400.0))/10;
so I assume it was for temperature not wind direction (?) I might be wrong.
But I changed it to temperature and get -39 C which is immposible here and I tried to mess with the temperature sensor by heating it up but the value doesn't even changed. I also print the temp value by it self and got 5 which is too small.
and I dont think the tx_type and security need to use that syntax knowing the result from bufferUSB[0]
alone is already correct the result for tx_type in int should be 36
also what does this opperand do, this << and this |, I check on it that this << is for bit shift but I'm not sure how it works on this when combined with this | opperand for the syntax here
temp = bufferUSB[7]<<16 | bufferUSB[8] <<8 | bufferUSB[9];
I tried to directly use the incoming data from the array just like this
tx_type = bufferUSB[0];
security_code = bufferUSB[1];
wind_direction = bufferUSB[2];
int temperature_f = (temperature - 400) / 10;
int wind_speed_f = (wind_speed / 8) * 1.12;
int gust_speed_f = gust_speed * 1.12;
int rain_f = rain * 0.3;
int light_f = light / 10;
Serial.print("TX Type ");
Serial.println(tx_type);
Serial.print("Security Code ");
Serial.println(security_code);
Serial.print("Wind Direction ");
Serial.println(wind_direction);
it works on the first 3 data array, the tx_type until wind_direction but after that knowing it skip a byte and the next 2 byte is taken for for the temp if i wrote
bufferUSB[3]
the result doesn't make sense.
24ED17E2883F000500000B28818750D8
"24" (tx_type) , "ED" (security_code), "17" (wind_direction), "E" (skipped), "288" (temperature),..etc. like you decode before
but in this case bufferUSB[3]
is "E2"
I changed the char to unsigned char to eliminate any FFFF from the dara result in negatives, but I can only got it until wind_direction.
this is the result
Incoming Data (for) : 24
Incoming Data (for) : ED
Incoming Data (for) : 9C
Incoming Data (for) : 62
Incoming Data (for) : BF
Incoming Data (for) : 47
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 9
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : BD
Incoming Data (for) : DB
Incoming Data (for) : 1
Incoming Data (for) : 87
Incoming Data (for) : 4F
Incoming Data (for) : D7
Incoming Data for HEX tx_type : 24
TX Type 36
Security Code 237
Wind Direction 156
Temperature -40
Humidity 0
Wind Speed 0
Gust Speed 0
Accumulative Rainfall 0
UV 0
Light 0
this is the code right now when using your code, I still need to understand what does it meant.
#include <SoftwareSerial.h>
SoftwareSerial usbcomm(3, 2); //RO,DI
int incomingByte = 0;
int tx_type, security_code, wind_direction, temperature, temp, humidity, wind_speed, gust_speed, rain, uv, light;
unsigned char bufferUSB[21 + 1];
void setup()
{
usbcomm.begin(9600);//Uncomment for Arduino Lenardo
Serial.begin(9600);
//while(!usbcomm);//Uncomment for Arduino Lenardo
pinMode(13, OUTPUT);//Led Connected
pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
}
void loop()
{
digitalWrite(8, LOW); //DE/RE=LOW Receive Enabled
for (int i = 0; i < 21; i++)
{
while (!usbcomm.available()); // wait for a character
bufferUSB[i] = usbcomm.read();
Serial.print("Incoming Data (for) : ");
Serial.println(bufferUSB[i],HEX); //just to check the incoming HEX is correct
}
Serial.print("Incoming Data for HEX tx_type : ");
Serial.println(bufferUSB[0], HEX); //Checking the t_type HEX
//sscanf(bufferUSB, "%2x%2x%2x%*1x%3x%2x%2x%2x%4x%4x%6x", &tx_type, &security_code, &wind_direction, &temperature, &humidity, &wind_speed, &gust_speed, &rain, &uv, &light);
tx_type = bufferUSB[0]<<8 | bufferUSB[1];
security_code = bufferUSB[2]<<8| bufferUSB[3];
temp = bufferUSB[7]<<16 | bufferUSB[8] <<8 | bufferUSB[9];
temperature = (temp - 400.0)/10;
// tx_type = bufferUSB[0];
// security_code = bufferUSB[1];
// wind_direction = bufferUSB[2];
// int temperature_f = (temperature - 400) / 10;
int wind_speed_f = (wind_speed / 8) * 1.12;
int gust_speed_f = gust_speed * 1.12;
int rain_f = rain * 0.3;
int light_f = light / 10;
Serial.print("TX Type ");
Serial.println(tx_type);
Serial.print("Security Code ");
Serial.println(security_code);
Serial.print("Wind Direction ");
Serial.println(wind_direction);
Serial.print("Temperature ");
Serial.println(temperature);
// Serial.print("Temperature2 ");
// Serial.println(temp);
Serial.print("Humidity ");
Serial.println(humidity);
Serial.print("Wind Speed ");
Serial.println(wind_speed_f);
Serial.print("Gust Speed ");
Serial.println(gust_speed_f);
Serial.print("Accumulative Rainfall ");
Serial.println(rain_f);
Serial.print("UV ");
Serial.println(uv);
Serial.print("Light ");
Serial.println(light_f);
}
and the result
Incoming Data (for) : 24
Incoming Data (for) : ED
Incoming Data (for) : 9C
Incoming Data (for) : 62
Incoming Data (for) : BE
Incoming Data (for) : 46
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 9
Incoming Data (for) : 0
Incoming Data (for) : 1
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 0
Incoming Data (for) : 8A
Incoming Data (for) : A7
Incoming Data (for) : 1
Incoming Data (for) : 87
Incoming Data (for) : 66
Incoming Data (for) : EE
Incoming Data for HEX tx_type : 24
TX Type 9453
Security Code -25502
Wind Direction 0
Temperature -39
Humidity 0
Wind Speed 0
Gust Speed 0
Accumulative Rainfall 0
UV 0
Light 0