I'm fairly new to programming and am having some real issues. I've spent a few days stuck on this one so I thought id seek some help!
I have an assignment for a uni course that requires me to send a "packet" of data from one arduino to another using cheap radio transmitters and the [virtualwire library][1]
The main data I'm sending is being read from a temperature/pressure sensor. other bits and bobs are being sent too, like an ID for the transmitter so the receiver knows what it is (the idea is we all make a transmitter and they can all talk to 1 receiver and all the transmitters have different ID's)
I have this working in decimal fine using this bit of code:
Serial.println("Attempting to send....");
char buff[3];
char fullmsg[11];
sprintf(buff,"%04d",SysID);
const char *msg0 = buff;
strcpy(fullmsg, msg0);
sprintf(buff,"%04d",NodeID);
const char *msg1 = buff;
strcat(fullmsg, msg1);
sprintf(buff,"%03d",temp);
const char *msg2 = buff;
strcat(fullmsg, msg2);
sprintf(buff,"%03d",Pressure);
const char *msg3 = buff;
strcat(fullmsg, msg3);
sprintf(buff,"%d",Switch);
const char *msg4 = buff;
strcat(fullmsg, msg4);
vw_send((uint8_t *)fullmsg, strlen(fullmsg)); //Sends full message via virtualwire
vw_wait_tx(); //Waits for message to be sent
Serial.print(fullmsg) ;
Serial.println(" ....Sent!");
delay(100);
That code works fine, it sends the data and its received on the other end like so https://i.sstatic.net/RWiSA.png (left is transmitting right is receiving).
The issue is, for all our devices to work together they need to be sending the same kind of "packet" and we have been told that it needs to be sent as binary. I think vw_send only sends char arrays..? (I'm a bit rusty on my terminology) so I needed to convert my integers into binary and then the binary into char arrays to send it...
So I convert to binary using this bit of code I found here:
float temperature; //define temperature as a float
bmp.getTemperature(&temperature); //assign the value to tempriture
int temp = temperature;
int t=temp;
String binarytemp ("");
int maskt = 1;
for(int i = 0; i < 8; i++) // 8 for 8 bit
{
if((maskt&t) >= 1)
binarytemp = "1"+binarytemp;
else
binarytemp = "0"+binarytemp;
maskt<<=1;
}
I do this for each of my variables giving me the String binarytemp along with others like "binarypressure" and "binaryswitch". so I have to tie them together into one string..
String string1 = binarynode;
String string2 = binarysys;
String string3 = binarytemp;
String string4 = binarypressure;
String string5 = binaryswitch;
String FULLMESSAGE = String(string1 + string2 + string3 + string4 + string5);
Then I convert to a Char array and try and send it.
char buf [33];
FULLMESSAGE.toCharArray(buf, 33);
Serial.print("buf:");
Serial.println(buf);
const char *msg = buf;
Serial.print("msg:");
Serial.println(msg);
vw_send((uint8_t *)msg, strlen(msg)); //Sends full message via virtualwire
vw_wait_tx(); //wait till the whole thing is sent
For some reason that I cant figure out It isn't working: https://i.sstatic.net/aofkz.png on the left it seems to be happily sending the 0's and 1's, but as you can see on the right I'm not picking anything up...
Does anyone have any idea why this might be happening? or maybe just a better way of going about the problem, I feel mine is a bit of a mess!
I hope I explained this well enough!
Full transmitter code binary (not working) : http:// pastebin.com/n1xCJidJ
Full transmitter code decimal (working) : http:// pastebin.com/avXv8U5D
Receiver code (working with decimal at least...) : http:// pastebin.com/ELTCfUMz
[1]: https:// www.pjrc.com/teensy/td_libs_VirtualWire.html
(sorry had to break some of the links as i need more reputation to add more than 2 links)
I think you are making too much work for yourself.
The word "binary" doesn't mean a string with "1" and "0" in it. Binary just means using int
, short
, char
(meaning 8 bit integer) types to store numbers - not strings.
That should simplify your code.