Advertisement
Guest User

Untitled

a guest
May 4th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <XBee.h>
  2. #include <math.h>
  3. // create the XBee object
  4. XBee xbee = XBee();
  5.  
  6. int sensor = A5;
  7. uint8_t payload[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  8. // union to convert float to byte string
  9. union u_tag {
  10. uint8_t b[4];
  11. float fval;
  12. } u;
  13.  
  14.  
  15. // SH + SL Address of receiving XBee
  16. XBeeAddress64 addr64 = XBeeAddress64(0x0013a200, 0x40DC7C90);
  17. ZBTxRequest zbTx = ZBTxRequest(addr64, payload, sizeof(payload));
  18. ZBTxStatusResponse txStatus = ZBTxStatusResponse();
  19.  
  20. int statusLed = 13;
  21. int errorLed = 12;
  22.  
  23. void flashLed(int pin, int times, int wait) {
  24.  
  25. for (int i = 0; i < times; i++) {
  26. digitalWrite(pin, HIGH);
  27. delay(wait);
  28. digitalWrite(pin, LOW);
  29.  
  30. if (i + 1 < times) {
  31. delay(wait);
  32. }
  33. }
  34. }
  35.  
  36. double Thermistor(int RawADC)
  37. {
  38. double Temp;
  39. Temp = log(10000.0 * ((1024.0 / RawADC - 1)));
  40. Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
  41. Temp = Temp - 273.15; // Convert Kelvin to Celcius
  42. //Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  43. return Temp;
  44. }
  45.  
  46. void setup() {
  47. pinMode(statusLed, OUTPUT);
  48. pinMode(errorLed, OUTPUT);
  49. Serial.begin(9600);
  50. }
  51.  
  52. void loop() {
  53. float rawADC = analogRead(sensor);
  54. float t = Thermistor (rawADC);
  55.  
  56. // check if returns are valid, if they are NaN (not a number) then something went wrong!
  57. if (!isnan(t)) {
  58.  
  59. // convert temperature into a byte array and copy it into the payload array
  60. u.fval = t;
  61. for (int i=0;i<4;i++){
  62. payload[i]=u.b[i];
  63. }
  64. u.fval = 100.33;
  65. for (int i=0;i<4;i++){
  66. payload[i+4]=u.b[i];
  67. }
  68.  
  69. xbee.send(zbTx);
  70. flashLed(statusLed, 1, 100); // flash TX indicator
  71.  
  72.  
  73. // after sending a tx request, we expect a status response, wait up to half second for the status response
  74. if (xbee.readPacket(500)) {
  75. // got a response!
  76. // should be a znet tx status
  77. if (xbee.getResponse().getApiId() == ZB_TX_STATUS_RESPONSE) {
  78. xbee.getResponse().getZBTxStatusResponse(txStatus);
  79.  
  80. // get the delivery status, the fifth byte
  81. if (txStatus.getDeliveryStatus() == SUCCESS) {
  82. // success. time to celebrate
  83. flashLed(statusLed, 5, 50);
  84. } else {
  85. // the remote XBee did not receive our packet. is it powered on?
  86. flashLed(errorLed, 3, 500);
  87. }
  88. }
  89. } else if (xbee.getResponse().isError()) {
  90. //nss.print("Error reading packet. Error code: ");
  91. //nss.println(xbee.getResponse().getErrorCode());
  92. } else {
  93. // local XBee did not provide a timely TX Status Response -- should not happen
  94. flashLed(errorLed, 1, 50);
  95. }
  96. }
  97. delay(2000);
  98. }
  99.  
  100. from xbee import ZigBee
  101. import serial
  102. import struct
  103. import datetime
  104.  
  105. PORT = '/dev/ttyUSB0'
  106. BAUD_RATE = 9600
  107.  
  108. def hex(bindata):
  109. return ''.join('%02x' % ord(byte) for byte in bindata)
  110.  
  111. # Open serial port
  112. ser = serial.Serial(PORT, BAUD_RATE)
  113.  
  114. # Create API object
  115. xbee = ZigBee(ser,escaped=True)
  116.  
  117. # Continuously read and print packets
  118. while True:
  119. try:
  120. response = xbee.wait_read_frame()
  121. sa = hex(response['source_addr_long'])
  122. rf = hex(response['rf_data'])
  123. obj = createObject(response)
  124. obj.createPacket()
  125. print ("Temperature: %.2f" % obj.packet['temperature'],
  126. "Humidity: %.2f" % obj.packet['humidity'],
  127. "Source Address: 0x%s" % obj.packet['sourceAddressShort'],
  128. "Timestamp: %s" % obj.packet['timestamp'].isoformat())
  129. except KeyboardInterrupt:
  130. break
  131.  
  132. ser.close()
  133.  
  134. class createObject:
  135. def __init__(self, response):
  136. self.sourceAddrLong = hex(response['source_addr_long'])
  137. self.rfData = hex(response['rf_data'])
  138. self.sourceAddrShort = hex(response['source_addr_long'][4:])
  139. self.options = response.pop('options')
  140. self.frameType = response['id']
  141. self.temperature = struct.unpack('f',response['rf_data'][0:4])[0]
  142. self.humidity = struct.unpack('f',response['rf_data'][4:])[0]
  143. self.dataLength = len(response['rf_data'])
  144. self.packet={}
  145. self.dateNow = datetime.datetime.utcnow()
  146. self.packetJson=0
  147.  
  148. def createPacket(self):
  149. self.packet.update({
  150. 'timestamp' : self.dateNow,
  151. 'temperature' : self.temperature,
  152. 'humidity' : self.humidity,
  153. 'dataLength' : self.dataLength,
  154. 'sourceAddressLong' : self.sourceAddrLong,
  155. 'sourceAddressShort' : self.sourceAddrShort,
  156. 'options' : self.options,
  157. 'frameType' : self.frameType
  158. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement