Created
July 30, 2014 15:46
-
-
Save anoochit/3a035223c2b7870286c5 to your computer and use it in GitHub Desktop.
wifi cc3000 rest api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import required libraries | |
#include <Adafruit_CC3000.h> | |
#include <SPI.h> | |
#include <CC3000_MDNS.h> | |
#include <aREST.h> | |
// These are the pins for the CC3000 chip if you are using a breakout board | |
#define ADAFRUIT_CC3000_IRQ 3 | |
#define ADAFRUIT_CC3000_VBAT 5 | |
#define ADAFRUIT_CC3000_CS 10 | |
// Create CC3000 instance | |
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, | |
SPI_CLOCK_DIV2); | |
// Create aREST instance | |
aREST rest = aREST(); | |
// Your WiFi SSID and password | |
#define WLAN_SSID "yourSSID" | |
#define WLAN_PASS "yourPassword" | |
#define WLAN_SECURITY WLAN_SEC_WPA2 | |
// The port to listen for incoming TCP connections | |
#define LISTEN_PORT 80 | |
// Server instance | |
Adafruit_CC3000_Server restServer(LISTEN_PORT); | |
// DNS responder instance | |
MDNSResponder mdns; | |
void setup(void) | |
{ | |
// Start Serial | |
Serial.begin(115200); | |
// Give name and ID to device | |
rest.set_id("001"); | |
rest.set_name("rest_api"); | |
// Set up CC3000 and get connected to the wireless network. | |
if (!cc3000.begin()) | |
{ | |
while(1); | |
} | |
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { | |
while(1); | |
} | |
while (!cc3000.checkDHCP()) | |
{ | |
delay(100); | |
} | |
Serial.println(); | |
// Print CC3000 IP address. Enable if mDNS doesn't work | |
while (! displayConnectionDetails()) { | |
delay(1000); | |
} | |
// Start multicast DNS responder | |
if (!mdns.begin("arduino", cc3000)) { | |
while(1); | |
} | |
// Start server | |
restServer.begin(); | |
Serial.println(F("Listening for connections...")); | |
} | |
void loop() { | |
// Handle any multicast DNS requests | |
mdns.update(); | |
// Handle REST calls | |
Adafruit_CC3000_ClientRef client = restServer.available(); | |
rest.handle(client); | |
} | |
// Print connection details of the CC3000 chip | |
bool displayConnectionDetails(void) | |
{ | |
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv; | |
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv)) | |
{ | |
Serial.println(F("Unable to retrieve the IP Address!\r\n")); | |
return false; | |
} | |
else | |
{ | |
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress); | |
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask); | |
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway); | |
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv); | |
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv); | |
Serial.println(); | |
return true; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment