Advertisement
Guest User

xbee/teensy3.1

a guest
Feb 12th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. /*
  2. * File basic_rx.ino
  3. *
  4. * Synopsis Simple example sketch to demonstrate reception of data
  5. *
  6. * Author Chris Bearman
  7. *
  8. * Version 2.0
  9. */
  10. #include "C:\Users\Owner\Desktop\basic_rx\XbeeWifi.h"
  11.  
  12. // These are the pins that we are using to connect to the Xbee
  13. #define XBEE_RESET 3
  14. #define XBEE_ATN 2
  15. #define XBEE_SELECT 21
  16. #define XBEE_DOUT 0
  17. #define XBEE_DIN 1
  18. #define XBEE_MOSI 7
  19. #define XBEE_MISO 12
  20. #define XBEE_SCK 14
  21.  
  22. // These are the configuration parameters we're going to use
  23. #define CONFIG_PAYLOAD XBEE_NET_IPPROTO_UDP // Expecting TCP connections
  24. #define CONFIG_PORT 0x3039 // To this port
  25. #define CONFIG_ENCMODE XBEE_SEC_ENCTYPE_WPA2 // Network type is WPA2 encrypted
  26. #define CONFIG_SSID "xxxxx" // SSID
  27. #define CONFIG_KEY "xxxxx" // Password
  28.  
  29. // Create an xbee object to handle things for us
  30. XbeeWifi xbee;
  31.  
  32. // Helper function converts binary IP address to textual form
  33. // returning said text as an static buffer reference (so non-reentrant)
  34. char * ipstr(uint8_t *ip)
  35. {
  36. static char ipstrbuf[32];
  37. sprintf(ipstrbuf, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
  38. return ipstrbuf;
  39. }
  40.  
  41. // Receives inbound IP data and outputs to serial
  42. void inbound_ip(unsigned char *data, int len, s_rxinfo *info)
  43. {
  44. Serial.println();
  45. Serial.print(F("Inbound data from "));
  46. Serial.print(ipstr(info->source_addr));
  47. Serial.print(":");
  48. Serial.print(info->source_port, DEC);
  49. Serial.println(":");
  50. data[len] = 0;
  51. Serial.println((char *)data);
  52. Serial.println();
  53. }
  54.  
  55. // Receives inbound modem status updates and outputs to serial
  56. void inbound_status(uint8_t status)
  57. {
  58. Serial.println();
  59. Serial.print(F("Modem status, received code "));
  60. Serial.print(status, DEC);
  61. Serial.print(F(" ("));
  62. switch(status) {
  63. case XBEE_MODEM_STATUS_RESET : Serial.print(F("Reset or power on"));
  64.  
  65. break;
  66. case XBEE_MODEM_STATUS_WATCHDOG_RESET : Serial.print(F("Watchdog reset"));
  67.  
  68. break;
  69. case XBEE_MODEM_STATUS_JOINED : Serial.print(F("Joined")); break;
  70. case XBEE_MODEM_STATUS_NO_LONGER_JOINED : Serial.print(F("No longer joined"));
  71.  
  72. break;
  73. case XBEE_MODEM_STATUS_IP_CONFIG_ERROR : Serial.print(F("IP configuration
  74.  
  75. error")); break;
  76. case XBEE_MODEM_STATUS_S_OR_J_WITHOUT_CON : Serial.print(F("Send or join without
  77.  
  78. connecting first")); break;
  79. case XBEE_MODEM_STATUS_AP_NOT_FOUND : Serial.print(F("AP not found"));
  80.  
  81. break;
  82. case XBEE_MODEM_STATUS_PSK_NOT_CONFIGURED : Serial.print(F("Key not configured"));
  83.  
  84. break;
  85. case XBEE_MODEM_STATUS_SSID_NOT_FOUND : Serial.print(F("SSID not found"));
  86.  
  87. break;
  88. case XBEE_MODEM_STATUS_FAILED_WITH_SECURITY : Serial.print(F("Failed to join with
  89.  
  90. security enabled")); break;
  91. case XBEE_MODEM_STATUS_INVALID_CHANNEL : Serial.print(F("Invalid channel"));
  92.  
  93. break;
  94. case XBEE_MODEM_STATUS_FAILED_TO_JOIN : Serial.print(F("Failed to join AP"));
  95.  
  96. break;
  97. default : Serial.print(F("Unknown Status
  98.  
  99. Code")); break;
  100. }
  101. Serial.println(F(")"));
  102. }
  103.  
  104. // Setup routine
  105. void setup()
  106. {
  107. // Serial at 57600
  108. Serial.begin(9600);
  109. while(!Serial){
  110. ;
  111. }
  112. // Initialize the xbee
  113. bool result = xbee.init(XBEE_MISO, XBEE_MOSI, XBEE_SCK, XBEE_SELECT, XBEE_ATN,
  114.  
  115. XBEE_RESET, XBEE_DOUT, XBEE_DIN);
  116.  
  117. if (result) {
  118. // Initialization okay so far, send setup parameters - if anything fails, result goes
  119.  
  120. false
  121. result &= xbee.at_cmd_byte(XBEE_AT_NET_TYPE, XBEE_NET_TYPE_IBSS_INFRASTRUCTURE);
  122. result &= xbee.at_cmd_byte(XBEE_AT_NET_IPPROTO, CONFIG_PAYLOAD);
  123. result &= xbee.at_cmd_str(XBEE_AT_NET_SSID, CONFIG_SSID);
  124. result &= xbee.at_cmd_byte(XBEE_AT_NET_ADDRMODE, XBEE_NET_ADDRMODE_DHCP);
  125. result &= xbee.at_cmd_short(XBEE_AT_ADDR_SERIAL_COM_SERVICE_PORT, CONFIG_PORT);
  126. result &= xbee.at_cmd_byte(XBEE_AT_SEC_ENCTYPE, CONFIG_ENCMODE);
  127. if (CONFIG_ENCMODE != XBEE_SEC_ENCTYPE_NONE) {
  128. result &= xbee.at_cmd_str(XBEE_AT_SEC_KEY, CONFIG_KEY);
  129. }
  130. }
  131.  
  132. if (!result) {
  133. // Something failed
  134. Serial.println(F("XBee Init Failed"));
  135. while (true) { /* Loop forever - game over */}
  136. } else {
  137. // Register for incoming data
  138. xbee.register_ip_data_callback(inbound_ip);
  139. xbee.register_status_callback(inbound_status);
  140.  
  141. Serial.println("XBee found and configured");
  142. }
  143. }
  144.  
  145. // Main run loop
  146. void loop()
  147. {
  148. // Just keep calling the process method on the xbee object
  149. // When things happen, it will call our callback routines
  150. xbee.process();
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement