SHARE
TWEET

Example code

a guest Jan 20th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * --------------------------------------
  3.  * Arduino with a 12V MagLock - XBee for comms. 0013A200 418D8E11
  4.  * Also has a VL6180x/GY6180 Range sensor.
  5.  * --------------------------------------
  6.  *
  7.  * PINOUT
  8.  *
  9.  * Maglock > MOSFET > Arduino
  10.  * TODO
  11.  *
  12.  * XBEE > Arduino
  13.  * 10 Gnd > Gnd
  14.  * 1 VCC > 3.3V
  15.  * 2 DOUT > 2
  16.  * 3 DIN > 4
  17.  *
  18.  * VL6180x/GY6180 Range
  19.  * VIN > 5V
  20.  * GND > GNd
  21.  * SCL > A5
  22.  * SDA > A4
  23.  * 0 > D5
  24.  * TODO
  25.  */
  26.  
  27. #include <Wire.h>
  28.  
  29. #include <XBee.h>
  30. #include <SoftwareSerial.h>
  31.  
  32. #include <Printers.h>
  33.  
  34. //XBEE & COMMUNICATIONS
  35. SoftwareSerial xbeeSerial(A0, A1); // RX, TX
  36.  
  37. //Works with Series1 and 2
  38. XBeeWithCallbacks xbee;
  39.  
  40. // Build a reuseable message packet to send to the Co-Ordinator
  41. XBeeAddress64 coordinatorAddr = XBeeAddress64(0x00000000, 0x00000000);
  42.  
  43. uint8_t distanceMessagePayload[1] = {0};
  44. ZBTxRequest distanceMessage = ZBTxRequest(coordinatorAddr, distanceMessagePayload,
  45. sizeof(distanceMessagePayload));
  46.  
  47.  
  48. void setup() {
  49.   Serial.begin(9600);   // Initialize serial communications with the PC
  50.  
  51.   Wire.begin();
  52.  
  53.   // XBEE
  54.   xbeeSerial.begin(9600);
  55.   xbee.setSerial(xbeeSerial);
  56.  
  57.   // Make sure that any errors are logged to Serial. The address of
  58.   // Serial is first cast to Print*, since that's what the callback
  59.   // expects, and then to uintptr_t to fit it inside the data parameter.
  60.   xbee.onPacketError(printErrorCb, (uintptr_t)(Print*)&Serial);
  61.   xbee.onTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
  62.   xbee.onZBTxStatusResponse(printErrorCb, (uintptr_t)(Print*)&Serial);
  63.  
  64.   // These are called when an actual packet received
  65.   //xbee.onZBRxResponse(zbReceive, (uintptr_t)(Print*)&Serial);
  66.  
  67.   // Print any unhandled response with proper formatting
  68.   xbee.onOtherResponse(printResponseCb, (uintptr_t)(Print*)&Serial);
  69.  
  70.   // Enable this to print the raw bytes for _all_ responses before they
  71.   // are handled
  72.   xbee.onResponse(printRawResponseCb, (uintptr_t)(Print*)&Serial);
  73.  
  74.   Serial.println("SETUP");
  75. }
  76.  
  77.  
  78. void loop() {
  79.  
  80.   // Continuously let xbee read packets and call callbacks.
  81.   xbee.loop();
  82.  
  83.  
  84.   SendTestPacket('a');
  85.   delay(500);
  86. }
  87.  
  88.  
  89.  
  90. void SendTestPacket( char msg)
  91. {
  92.   distanceMessagePayload[0] = msg;
  93.   distanceMessage.setFrameId(xbee.getNextFrameId());
  94.  
  95.   Serial.println("SENDING...");
  96.  
  97.   // Send the command and wait up to N ms for a response.  xbee loop continues during this time.
  98.   uint8_t status = xbee.sendAndWait(distanceMessage, 3000);
  99.   if (status == 0)
  100.   {
  101.     Serial.println(F("SEND ACKNOWLEDGED"));
  102.   }
  103.   else
  104.   { //Complain, but do not reset timeElapsed - so that a new packet comes in and
  105.     //tried again immedietly.
  106.     Serial.print(F("SEND FAILED: "));
  107.     printHex(status, 2);
  108.     Serial.println();
  109.  
  110.   }
  111. }
  112.  
  113.  
  114. // UTIL FUNCTIONS
  115. void printHex(int num, int precision) {
  116.      char tmp[16];
  117.      char format[128];
  118.  
  119.      sprintf(format, "0x%%.%dX", precision);
  120.  
  121.      sprintf(tmp, format, num);
  122.      Serial.print(tmp);
  123. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top