document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /***************************************************
  2.   This is an example for the Adafruit CC3000 Wifi Breakout & Shield
  3.  
  4.   Designed specifically to work with the Adafruit WiFi products:
  5.   ----> https://www.adafruit.com/products/1469
  6.  
  7.   Adafruit invests time and resources providing this open source code,
  8.   please support Adafruit and open-source hardware by purchasing
  9.   products from Adafruit!
  10.  
  11.   Written by Kevin Townsend & Limor Fried for Adafruit Industries.  
  12.   BSD license, all text above must be included in any redistribution
  13.  ****************************************************/
  14.  
  15. /*
  16.  
  17.  
  18. This example does a full test of core connectivity:
  19. * Initialization
  20. * SSID Scan
  21. * AP connection
  22. * DHCP printout
  23. * DNS lookup
  24. * Ping
  25. * Disconnect
  26. It\'s a good idea to run this sketch when first setting up the
  27. module.
  28.  
  29. */
  30.  
  31. #include <Adafruit_CC3000.h>
  32. #include <ccspi.h>
  33. #include <SPI.h>
  34. #include <string.h>
  35. #include "utility/debug.h"
  36.  
  37. // Config the interrupt and control pins on Wido
  38. #define ADAFRUIT_CC3000_IRQ   7
  39. #define ADAFRUIT_CC3000_VBAT  5
  40. #define ADAFRUIT_CC3000_CS  10
  41.  
  42. Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
  43.                                         SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
  44.  
  45. //Please enter the SSID and password of the router you want to connect
  46.  
  47. #define WLAN_SSID       "SSID_name"     // cannot be longer than 32 characters!
  48. #define WLAN_PASS       "password"      //
  49.  
  50. // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
  51. #define WLAN_SECURITY   WLAN_SEC_WPA2
  52.  
  53.  
  54. void setup(void)
  55. {
  56.   Serial.begin(115200);
  57.    while (!Serial) {
  58.     ; // wait for serial port to connect. Needed for Leonardo only
  59.   }
  60.   Serial.println(F("Hello, CC3000!\\n"));
  61.  
  62.  
  63.   displayDriverMode();
  64.   // Measure the free Ram
  65.   Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
  66.  
  67.   /* Initialise the module */
  68.   Serial.println(F("\\nInitialising the CC3000 ..."));
  69.   if (!cc3000.begin())
  70.   {
  71.     Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
  72.     while(1);
  73.   }
  74.  
  75.   /* Optional: Update the Mac Address to a known value */
  76. /*
  77.   uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
  78.    if (!cc3000.setMacAddress(macAddress))
  79.    {
  80.     Serial.println(F("Failed trying to update the MAC address"));
  81.     while(1);
  82.    }
  83. */
  84.  
  85.   uint16_t firmware = checkFirmwareVersion();
  86.   if (firmware < 0x113) {
  87.     Serial.println(F("Wrong firmware version!"));
  88.     for(;;);
  89.   }
  90.  
  91.   displayMACAddress();
  92.  
  93.   /* Optional: Get the SSID list (not available in \'tiny\' mode) */
  94. #ifndef CC3000_TINY_DRIVER
  95.   listSSIDResults();
  96. #endif
  97.  
  98.   /* Delete any old connection data on the module */
  99.   Serial.println(F("\\nDeleting old connection profiles"));
  100.   if (!cc3000.deleteProfiles()) {
  101.     Serial.println(F("Failed!"));
  102.     while(1);
  103.   }
  104.  
  105.   /* Optional: Set a static IP address instead of using DHCP.
  106.     Note that the setStaticIPAddress function will save its state
  107.     in the CC3000\'s internal non-volatile memory and the details
  108.     will be used the next time the CC3000 connects to a network.
  109.     This means you only need to call the function once and the
  110.     CC3000 will remember the connection details.  To switch back
  111.     to using DHCP, call the setDHCP() function (again only needs
  112.     to be called once).
  113.   */
  114.   /*
  115.   uint32_t ipAddress = cc3000.IP2U32(192, 168, 1, 19);
  116.   uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
  117.   uint32_t defaultGateway = cc3000.IP2U32(192, 168, 1, 1);
  118.   uint32_t dns = cc3000.IP2U32(8, 8, 4, 4);
  119.   if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
  120.     Serial.println(F("Failed to set static IP!"));
  121.     while(1);
  122.   }
  123.   */
  124.   /* Optional: Revert back from static IP addres to use DHCP.
  125.     See note for setStaticIPAddress above, this only needs to be
  126.     called once and will be remembered afterwards by the CC3000.
  127.   */
  128.   /*
  129.   if (!cc3000.setDHCP()) {
  130.     Serial.println(F("Failed to set DHCP!"));
  131.     while(1);
  132.   }
  133.   */
  134.  
  135.   /* Attempt to connect to an access point */
  136.   char *ssid = WLAN_SSID;           /* Max 32 chars */
  137.   Serial.print(F("\\nAttempting to connect to ")); Serial.println(ssid);
  138.  
  139.   /* NOTE: Secure connections are not available in \'Tiny\' mode!
  140.     By default connectToAP will retry indefinitely, however you can pass an
  141.     optional maximum number of retries (greater than zero) as the fourth parameter.
  142.   */
  143.   if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  144.     Serial.println(F("Failed!"));
  145.     while(1);
  146.   }
  147.    
  148.   Serial.println(F("Connected!"));
  149.  
  150.   /* Wait for DHCP to complete */
  151.   Serial.println(F("Request DHCP"));
  152.   while (!cc3000.checkDHCP())
  153.   {
  154.     delay(100); // ToDo: Insert a DHCP timeout!
  155.   }  
  156.  
  157.   /* Display the IP address DNS, Gateway, etc. */  
  158.   while (! displayConnectionDetails()) {
  159.     delay(1000);
  160.   }
  161.  
  162. #ifndef CC3000_TINY_DRIVER
  163.   /* Try looking up www.adafruit.com */
  164.   uint32_t ip = 0;
  165.   Serial.print(F("www.adafruit.com -> "));
  166.   while  (ip  ==  0)  {
  167.     if  (!  cc3000.getHostByName("www.adafruit.com", &ip))  {
  168.     Serial.println(F("Couldn\'t resolve!"));
  169.     }
  170.     delay(500);
  171.   }  
  172.   cc3000.printIPdotsRev(ip);
  173.  
  174.   /* Do a quick ping test on adafruit.com */  
  175.   Serial.print(F("\\n\\rPinging ")); cc3000.printIPdotsRev(ip); Serial.print("...");  
  176.   uint8_t replies = cc3000.ping(ip, 5);
  177.   Serial.print(replies); Serial.println(F(" replies"));
  178.   if (replies)
  179.     Serial.println(F("Ping successful!"));
  180. #endif
  181.  
  182.   /* You need to make sure to clean up after yourself or the CC3000 can freak out */
  183.   /* the next time you try to connect ... */
  184.   Serial.println(F("\\n\\nClosing the connection"));
  185.   cc3000.disconnect();
  186. }
  187.  
  188. void loop(void)
  189. {
  190.   delay(1000);
  191. }
  192.  
  193. /**************************************************************************/
  194. /*!
  195.     @brief  Displays the driver mode (tiny of normal), and the buffer
  196.             size if tiny mode is not being used
  197.  
  198.     @note   The buffer size and driver mode are defined in cc3000_common.h
  199. */
  200. /**************************************************************************/
  201. void displayDriverMode(void)
  202. {
  203.   #ifdef CC3000_TINY_DRIVER
  204.     Serial.println(F("CC3000 is configure in \'Tiny\' mode"));
  205.   #else
  206.     Serial.print(F("RX Buffer : "));
  207.     Serial.print(CC3000_RX_BUFFER_SIZE);
  208.     Serial.println(F(" bytes"));
  209.     Serial.print(F("TX Buffer : "));
  210.     Serial.print(CC3000_TX_BUFFER_SIZE);
  211.     Serial.println(F(" bytes"));
  212.   #endif
  213. }
  214.  
  215. /**************************************************************************/
  216. /*!
  217.     @brief  Tries to read the CC3000\'s internal firmware patch ID
  218. */
  219. /**************************************************************************/
  220. uint16_t checkFirmwareVersion(void)
  221. {
  222.   uint8_t major, minor;
  223.   uint16_t version;
  224.  
  225. #ifndef CC3000_TINY_DRIVER  
  226.   if(!cc3000.getFirmwareVersion(&major, &minor))
  227.   {
  228.     Serial.println(F("Unable to retrieve the firmware version!\\r\\n"));
  229.     version = 0;
  230.   }
  231.   else
  232.   {
  233.     Serial.print(F("Firmware V. : "));
  234.     Serial.print(major); Serial.print(F(".")); Serial.println(minor);
  235.     version = major; version <<= 8; version |= minor;
  236.   }
  237. #endif
  238.   return version;
  239. }
  240.  
  241. /**************************************************************************/
  242. /*!
  243.     @brief  Tries to read the 6-byte MAC address of the CC3000 module
  244. */
  245. /**************************************************************************/
  246. void displayMACAddress(void)
  247. {
  248.   uint8_t macAddress[6];
  249.  
  250.   if(!cc3000.getMacAddress(macAddress))
  251.   {
  252.     Serial.println(F("Unable to retrieve MAC Address!\\r\\n"));
  253.   }
  254.   else
  255.   {
  256.     Serial.print(F("MAC Address : "));
  257.     cc3000.printHex((byte*)&macAddress, 6);
  258.   }
  259. }
  260.  
  261.  
  262. /**************************************************************************/
  263. /*!
  264.     @brief  Tries to read the IP address and other connection details
  265. */
  266. /**************************************************************************/
  267. bool displayConnectionDetails(void)
  268. {
  269.   uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
  270.  
  271.   if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
  272.   {
  273.     Serial.println(F("Unable to retrieve the IP Address!\\r\\n"));
  274.     return false;
  275.   }
  276.   else
  277.   {
  278.     Serial.print(F("\\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
  279.     Serial.print(F("\\nNetmask: ")); cc3000.printIPdotsRev(netmask);
  280.     Serial.print(F("\\nGateway: ")); cc3000.printIPdotsRev(gateway);
  281.     Serial.print(F("\\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
  282.     Serial.print(F("\\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
  283.     Serial.println();
  284.     return true;
  285.   }
  286. }
  287.  
  288. /**************************************************************************/
  289. /*!
  290.     @brief  Begins an SSID scan and prints out all the visible networks
  291. */
  292. /**************************************************************************/
  293.  
  294. void listSSIDResults(void)
  295. {
  296.   uint32_t index;
  297.   //uint8_t valid, rssi, sec, index;
  298.   uint8_t valid, rssi, sec;
  299.   char ssidname[33];
  300.  
  301.   //index = cc3000.startSSIDscan();
  302.    cc3000.startSSIDscan(&index); // nuevo
  303.    
  304.   Serial.print(F("Networks found: ")); Serial.println(index);
  305.   Serial.println(F("================================================"));
  306.  
  307.   while (index) {
  308.     index--;
  309.  
  310.     valid = cc3000.getNextSSID(&rssi, &sec, ssidname);
  311.    
  312.     Serial.print(F("SSID Name   : ")); Serial.print(ssidname);
  313.     Serial.println();
  314.     Serial.print(F("RSSI        : "));
  315.     Serial.println(rssi);
  316.     Serial.print(F("Security Mode: "));
  317.     Serial.println(sec);
  318.     Serial.println();
  319.   }
  320.   Serial.println(F("================================================"));
  321.  
  322.   cc3000.stopSSIDscan();
  323. }
');