Hello everyone!
i want to transmit the DHT22's data through xbee,
i'v been transfer the data to char[] = {2,5,.,7,4,1,.,5} "Temperature=25.7 Humidity=41.5"
how can i get a array that could be {32,35,2e,37,34,31,2e,35}
HEX is just a text representation of a number (any number). Serial.print(2) will e.g. send 0x32 (the ascii value for the character 2).
Please show your code (using code tags) as it's not quit clear what you want to achieve.
Ah, maybe you need this
char value[] = "25.741.5";
/*
/*
Getting Started, Part 1: Router
This example will show you how to setup a router and
send a packet to the network coordinator using the
SimpleZigBee library. You will need to complete
"Getting Started, Part 1: Coordinator" before
completing this example.
###########################################################
created 30 June 2014
updated 16 Sept 2015
by Eric Burger
This example code is in the public domain.
The SimpleZigBee library is released under the GNU GPL v2 License
###########################################################
Setup:
1. Complete "Getting Started, Part 1: Coordinator".
2. This code will be used to interact with a router.
Use the XCTU Software to load the Router API firmware
onto a second XBee S2 radio. Set the PAN ID to whatever you
used in the previous example (Getting Started: Part 1a) or
leave the value as 0 and the router will connect to any
network. Lastly, make sure the API Mode is set to 2 (AP=2).
3. In this example, the Arduino will connect to the XBee
using a Software Serial port. This allows the Hardware
Serial port to remain free, making it easier to reprogram the
Arduino and to debug the code. Begin by connecting the XBee.
Connect DOUT to Pin 10 (RX) and DIN to Pin 11 (TX). Also,
connect the XBee to 3.3V and ground (GND).
4. Upload this sketch (to a second Arduino attached to the
Router) and open the Arduino IDE's Serial Monitor. Read
through the commented code below to understand what is
being displayed in the serial monitor.
5. If you are having trouble getting the router to connect to
the coordinator, try reseting both XBees to the default
settings using XCTU. Also, you can try sending the AT Command
CB4 to leave the network (see setup() below).
6. The next "Getting Started" examples will provide more
detail about how to setup a message and to interpret what
is being received.
*/
#include <SimpleZigBeeRadio.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Create the XBee object ...
SimpleZigBeeRadio xbee = SimpleZigBeeRadio();
// ... and the software serial port. Note: Only one
// SoftwareSerial object can receive data at a time.
SoftwareSerial xbeeSerial(10, 11); // (RX=>DOUT, TX=>DIN)
// Packet to send: In this example, we will update
// the contents of a packet before sending it.
SimpleZigBeePacket zbp = SimpleZigBeePacket();
// Value and payload to be sent
int val = 0;
// Variables to store time
uint8_t hex[8];
unsigned long time = 0;
unsigned long last_sent = 0;
void setup() {
// Start the serial ports ...
Serial.begin( 9600 );
while( !Serial ){;}// Wait for serial port (for Leonardo only).
xbeeSerial.begin( 9600 );
// ... and set the serial port for the XBee radio.
xbee.setSerial( xbeeSerial );
// Receive TX Status packets
xbee.setAcknowledgement(true);
// The frame data in a ZigBee packet refers to the data between
// the length LSB and the checksum. Below is an example of
// a frame where:
// Frame type = 0x10 (ZigBee Transmit Request)
// Frame id = 0x01
// 64-bit destination address = 0x0000000000000000 (Coordinator address)
// 16-bit destination address = 0xfffe (unknown or broadcast)
// Broadcast radius = 0x00
// Options = 0x00
// Payload data = 0xffff
uint8_t exFrame[] = { 0x10,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xfe,0x00,0x00,0xff,0xff };
// Now store the example frame in the packet object.
// The setFrameData() method accepts a starting index
// (where in the frame to begin), an array, and the
// array's length (number of bytes to store).
zbp.setFrameData(0, exFrame, sizeof(exFrame));
// If you are having trouble connecting the Router
// to the Coordinator, you can try sending the
// AT command to leave the current network and then
// wait for the Router to reconnect.
/*
Serial.println( "Send command to leave network (CB4)" );
xbee.prepareATCommand('CB',4);
xbee.send();
delay(10000);
*/
}
void loop() {
// While data is waiting in the XBee serial port ...
while( xbee.available() ){
// ... read the data.
xbee.read();
// If a complete message is available and it
// has not been read yet, read it.
if( xbee.isComplete() ){
// Print the contents of the incoming packet
Serial.print("\nIncoming Message: ");
printPacket( xbee.getIncomingPacketObject() );
}
}
// The Arduino will send a packet to the XBee once every 5
// seconds. To avoid overflowing the serial buffer, you
// should avoid putting long delays, like delay(5000),
// in a sketch.
time = millis();
if( time > (last_sent+5000) ){
last_sent = time; // Update the last_sent variable
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
char hum[8],temp[5],data[8];
String(h).toCharArray(hum,5);
String(t).toCharArray(temp,5);
strcat(hum,temp);
// Serial.print("Humidity: ");
for(int i=0;hum[i]!='\0';i++){
/* hum[i] = (hum[i],HEX);
Serial.print(hum[i]);
Serial.print(",");*/
hex[i] = uint8_t(hum[i]);
Serial.print(hex[i],HEX);
Serial.print(" ");
}
/* for(int i=0;i<sizeof(ex);i++){
Serial.print(ex[i],HEX);
}*/
//Serial.print(String(h));
// Serial.print(" %\t");
// Serial.print("Temperature: ");
//Serial.print(String(t));
// Serial.print(" *C ");
/* Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");*/
// Update the payload (in this case, the last 2 bytes of the frame)
// The rest of the frame (address, etc) does not need to be changed.
// Note: The max value of a byte is 255. Therefore, larger values,
// like ints, must be broken into bytes.
zbp.setFrameData( zbp.getFrameLength()-2, val >> 8 & 0xff );
zbp.setFrameData( zbp.getFrameLength()-1, val & 0xff );
Serial.print("\nSend Message: ");
printPacket( zbp );
// Send the packet. This example does not use the outgoing packet
// object contained in the SimpleZigBeeRadio class.
xbee.send( zbp );
val = (val + 10)%500; // Increase val by 10 (start over at 500)
}
delay(10); // Small delay for stability
// That's it! The router is ready to go.
}
/////////////////////////////////////////////////////////////
// Function for printing the complete contents of a packet //
/////////////////////////////////////////////////////////////
void printPacket(SimpleZigBeePacket & p){
Serial.print( START, HEX );
Serial.print(' ');
Serial.print( p.getLengthMSB(), HEX );
Serial.print(' ');
Serial.print( p.getLengthLSB(), HEX );
Serial.print(' ');
// Frame Type and Frame ID are stored in Frame Data
uint8_t checksum = 0;
for( int i=0; i<p.getFrameLength(); i++){
Serial.print( p.getFrameData(i), HEX );
Serial.print(' ');
checksum += p.getFrameData(i);
}
// Calculate checksum based on summation of frame bytes
checksum = 0xff - checksum;
Serial.print(checksum, HEX );
Serial.println();
}
i want to put the data in to the frame and send it out.
OK; in short you don't have to do anything (except for a minor bugfix); those numbers are already in the format that you want.
Little longer:
Your Convert() function does absolutely nothing except for casting to an uint8_t. You can achieve the same using
hex[i]=(uint8_t)hum[i];
Which is quite useless as well.
Next your hum variable is one character too small. You counted the number of characters (4 plus 4) but c-style strings require a terminating nul character ('\0') and you do not have place for that.
I assume that the data should go in the data variable. I suggest the below approach
// humidity and temperatur eas text, each max 7 characters and nul character
char hum[8], temp[8];
// text to send; max 15 characters and nul character
char data[16];
// convert humidity and temperature to text
dtostrf(hif, 2, 1, hum);
dtostrf(hic, 2, 1, temp);
// 'empty' the data variable
data[0] = '\0';
// add hum and temp to data
strcat(data, hum);
strcat(data, temp);
// print the data as text
Serial.println(data);
// if you want to see a text representation of the binary values inside data
for (int cnt = 0; cnt < strlen(data); cnt++)
{
Serial.print(data[cnt], HEX); Serial.print(" ");
}
Serial.println();
Now my question is which variable is the frame that you're talking about?
Note:
no experience with xbee so don't know how you need to send it and can't help you with that part
sterretje:
OK; in short you don't have to do anything (except for a minor bugfix); those numbers are already in the format that you want.Little longer:
Your Convert() function does absolutely nothing except for casting to an uint8_t. You can achieve the same using
hex[i]=(uint8_t)hum[i];
Which is quite useless as well.
Next your hum variable is one character too small. You counted the number of characters (4 plus 4) but c-style strings require a terminating nul character ('\0') and you do not have place for that.
I assume that the data should go in the data variable. I suggest the below approach
// humidity and temperatur eas text, each max 7 characters and nul character
char hum[8], temp[8];
// text to send; max 15 characters and nul character
char data[16];
// convert humidity and temperature to text
dtostrf(hif, 2, 1, hum);
dtostrf(hic, 2, 1, temp);
// 'empty' the data variable
data[0] = '\0';
// add hum and temp to data
strcat(data, hum);
strcat(data, temp);
// print the data as text
Serial.println(data);
// if you want to see a text representation of the binary values inside data
for (int cnt = 0; cnt < strlen(data); cnt++)
{
Serial.print(data[cnt], HEX); Serial.print(" ");
}
Serial.println();
Now my question is which variable is the frame that you're talking about? Note: no experience with xbee so don't know how you need to send it and can't help you with that part
thanks sterretje ~
the frame is
uint8_t exFrame[] = {0x10,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xfe,0x00,0x00,0xff,0xff };
i want to combine the exFrame and hex array to send the data out , but it seems like some problem when i use strcat.
You can use memcpy to copy the 8 bytes (from data above) into the correct position
// copy data to 3rd (and following) bytes in exFrame
memcpy(&exFrame[2],data,8);
thanks sterretje
when i use loop to print the hex[] array, i got the data with decimals.
should i convert the dec to hex before combine two array ?
Forget the hex array, it's obsolete with the solution I suggested. Even data is not strictly required because you can memcpy both hum and temp into exFrame at the correct positions.
The code will send text so e.g. the character '2' is send as the binary value 0x32 and the decimal dot will be send as the binary value 0x2e.
thanks a lot sterretje!
I finally put the data in frame! But the problem is how to send it out?
The exFrame is declare in setup, but i can't deal with it before the function
"zbp.setFrameData(0, exFrame, sizeof(exFrame));"
The frame in picture is the "ex" that i declare in loop. How can i fix this problem?
#include <SimpleZigBeeRadio.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Create the XBee object ...
SimpleZigBeeRadio xbee = SimpleZigBeeRadio();
// ... and the software serial port. Note: Only one
// SoftwareSerial object can receive data at a time.
SoftwareSerial xbeeSerial(10, 11); // (RX=>DOUT, TX=>DIN)
// Packet to send: In this example, we will update
// the contents of a packet before sending it.
SimpleZigBeePacket zbp = SimpleZigBeePacket();
// Value and payload to be sent
int val = 0;
// Variables to store time
unsigned long time = 0;
unsigned long last_sent = 0;
uint8_t exFrame[] = { 0x10,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xfe,0x00,0x00,0xff,0xff };
void setup() {
// Start the serial ports ...
Serial.begin( 9600 );
while( !Serial ){;}// Wait for serial port (for Leonardo only).
xbeeSerial.begin( 9600 );
// ... and set the serial port for the XBee radio.
xbee.setSerial( xbeeSerial );
// Receive TX Status packets
xbee.setAcknowledgement(true);
// Now store the example frame in the packet object.
// The setFrameData() method accepts a starting index
// (where in the frame to begin), an array, and the
// array's length (number of bytes to store).
zbp.setFrameData(0, exFrame, sizeof(exFrame));
}
void loop() {
// While data is waiting in the XBee serial port ...
while( xbee.available() ){
// ... read the data.
xbee.read();
// If a complete message is available and it
// has not been read yet, read it.
if( xbee.isComplete() ){
// Print the contents of the incoming packet
Serial.print("\nIncoming Message: ");
printPacket( xbee.getIncomingPacketObject() );
}
}
time = millis();
if( time > (last_sent+5000) ){
last_sent = time; // Update the last_sent variable
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
uint8_t ex[22] = { 0x10,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xfe,0x00,0x00 };
uint8_t hum[8],temp[5];
String(h).toCharArray(hum,5);
String(t).toCharArray(temp,5);
strcat(hum,temp);
memcpy(ex+14,hum,9);
// Serial.print("Humidity: ");
for(int i=0;i<22;i++){
Serial.print((ex[i]),HEX);
Serial.print(" ");
}
//Serial.print(String(h));
Serial.print(" %\t");
// Serial.print("Temperature: ");
/* for(int i=0;i<4;i++){
Serial.print(temp[i], HEX);
Serial.print(" ");
}
//Serial.print(String(t));
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");*/
// Update the payload (in this case, the last 2 bytes of the frame)
// The rest of the frame (address, etc) does not need to be changed.
// Note: The max value of a byte is 255. Therefore, larger values,
// like ints, must be broken into bytes.
// zbp.setFrameData( zbp.getFrameLength()-2, val >> 8 & 0xff );
//zbp.setFrameData( zbp.getFrameLength()-1, val & 0xff );
Serial.print("\nSend Message: ");
printPacket( zbp );
// Send the packet. This example does not use the outgoing packet
// object contained in the SimpleZigBeeRadio class.
xbee.send( zbp );
val = (val + 10)%500; // Increase val by 10 (start over at 500)
}
delay(10); // Small delay for stability
// That's it! The router is ready to go.
}
void printPacket(SimpleZigBeePacket & p){
Serial.print( START, HEX );
Serial.print(' ');
Serial.print( p.getLengthMSB(), HEX );
Serial.print(' ');
Serial.print( p.getLengthLSB(), HEX );
Serial.print(' ');
// Frame Type and Frame ID are stored in Frame Data
uint8_t checksum = 0;
for( int i=0; i<p.getFrameLength(); i++){
Serial.print( p.getFrameData(i), HEX );
Serial.print(' ');
checksum += p.getFrameData(i);
}
// Calculate checksum based on summation of frame bytes
checksum = 0xff - checksum;
Serial.print(checksum, HEX );
Serial.println();
}
I have no experience with xbee so have no idea how it is supposed to work. Maybe you should start a new thread about xbee transmit data.
I was mistaken as to where that actual data needed to be inserted; it needed to be inserted in the payload part but you did that correctly (although you might be one off, payload starts at 14 if I counted correctly).
Why do you insist in using String (with capital S)? My example got rid of it and used hum and temp directly to store the text based data.