informatica:arduino:wifi
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
informatica:arduino:wifi [2016/12/07 18:50] – created jose | informatica:arduino:wifi [2016/12/12 20:15] (current) – jose | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== CC3000 Wifi Shield ====== | ====== CC3000 Wifi Shield ====== | ||
http:// | http:// | ||
+ | |||
+ | Problema que deja de funcionar: | ||
+ | |||
+ | https:// | ||
+ | |||
Nos bajamos la biblioteca (que no librería) | Nos bajamos la biblioteca (que no librería) | ||
Line 6: | Line 11: | ||
{{ : | {{ : | ||
+ | |||
+ | ====== Servidor web con IP Estática ====== | ||
+ | La ip fija se asigna con la función setStaticIPAddress que hay que poner antes que conectar a la red wifi. He puesto un delay de 1000 después de asignar la ip estática porque si no fallaba. | ||
+ | |||
+ | < | ||
+ | / | ||
+ | Adafruit CC3000 Breakout/ | ||
+ | | ||
+ | This is a simple implementation of a bare bones | ||
+ | HTTP server that can respond to very simple requests. | ||
+ | Note that this server is not meant to handle high | ||
+ | load, concurrent connections, | ||
+ | with 2K of memory can only handle so much complexity! | ||
+ | This server example is best for very simple status messages | ||
+ | or REST APIs. | ||
+ | |||
+ | See the CC3000 tutorial on Adafruit' | ||
+ | for more information on setting up and using the | ||
+ | CC3000: | ||
+ | http:// | ||
+ | | ||
+ | Requirements: | ||
+ | | ||
+ | This sketch requires the Adafruit CC3000 library. | ||
+ | download the library from: | ||
+ | https:// | ||
+ | | ||
+ | For information on installing libraries in the Arduino IDE | ||
+ | see this page: | ||
+ | http:// | ||
+ | | ||
+ | Usage: | ||
+ | | ||
+ | Update the SSID and, if necessary, the CC3000 hardware pin | ||
+ | information below, then run the sketch and check the | ||
+ | output of the serial port. After connecting to the | ||
+ | wireless network successfully the sketch will output | ||
+ | the IP address of the server and start listening for | ||
+ | connections. | ||
+ | to the server IP from a web browser. | ||
+ | server is listening on IP 192.168.1.130 you would access | ||
+ | http:// | ||
+ | | ||
+ | Created by Tony DiCola and adapted from HTTP server code created by Eric Friedrich. | ||
+ | | ||
+ | This code was adapted from Adafruit CC3000 library example | ||
+ | code which has the following license: | ||
+ | | ||
+ | Designed specifically to work with the Adafruit WiFi products: | ||
+ | ----> https:// | ||
+ | |||
+ | Adafruit invests time and resources providing this open source code, | ||
+ | please support Adafruit and open-source hardware by purchasing | ||
+ | products from Adafruit! | ||
+ | |||
+ | Written by Limor Fried & Kevin Townsend for Adafruit Industries. | ||
+ | BSD license, all text above must be included in any redistribution | ||
+ | | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | #include " | ||
+ | |||
+ | // These are the interrupt and control pins | ||
+ | #define ADAFRUIT_CC3000_IRQ | ||
+ | // These can be any two pins | ||
+ | #define ADAFRUIT_CC3000_VBAT | ||
+ | #define ADAFRUIT_CC3000_CS | ||
+ | // Use hardware SPI for the remaining pins | ||
+ | // On an UNO, SCK = 13, MISO = 12, and MOSI = 11 | ||
+ | |||
+ | Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, | ||
+ | | ||
+ | |||
+ | #define WLAN_SSID | ||
+ | #define WLAN_PASS | ||
+ | // Security can be WLAN_SEC_UNSEC, | ||
+ | #define WLAN_SECURITY | ||
+ | |||
+ | #define LISTEN_PORT | ||
+ | // The HTTP protocol uses port 80 by default. | ||
+ | |||
+ | #define MAX_ACTION | ||
+ | |||
+ | #define MAX_PATH | ||
+ | // There isn't much memory available so keep this short! | ||
+ | |||
+ | #define BUFFER_SIZE | ||
+ | // Since only the first line is parsed this | ||
+ | // needs to be as large as the maximum action | ||
+ | // and path plus a little for whitespace and | ||
+ | // HTTP version. | ||
+ | |||
+ | #define TIMEOUT_MS | ||
+ | // an incoming request to finish. | ||
+ | // too high or your server could be slow to respond. | ||
+ | |||
+ | Adafruit_CC3000_Server httpServer(LISTEN_PORT); | ||
+ | uint8_t buffer[BUFFER_SIZE+1]; | ||
+ | int bufindex = 0; | ||
+ | char action[MAX_ACTION+1]; | ||
+ | char path[MAX_PATH+1]; | ||
+ | |||
+ | void setup(void) | ||
+ | { | ||
+ | Serial.begin(115200); | ||
+ | Serial.println(F(" | ||
+ | |||
+ | Serial.print(" | ||
+ | | ||
+ | // Initialise the module | ||
+ | Serial.println(F(" | ||
+ | if (!cc3000.begin()) | ||
+ | { | ||
+ | Serial.println(F(" | ||
+ | while(1); | ||
+ | } | ||
+ | | ||
+ | uint32_t ipAddress = cc3000.IP2U32(192, | ||
+ | uint32_t netMask = cc3000.IP2U32(255, | ||
+ | uint32_t defaultGateway = cc3000.IP2U32(192, | ||
+ | uint32_t dns = cc3000.IP2U32(8, | ||
+ | if (!cc3000.setStaticIPAddress(ipAddress, | ||
+ | Serial.println(F(" | ||
+ | while(1); | ||
+ | } | ||
+ | | ||
+ | delay(1000); | ||
+ | |||
+ | | ||
+ | Serial.print(F(" | ||
+ | if (!cc3000.connectToAP(WLAN_SSID, | ||
+ | Serial.println(F(" | ||
+ | while(1); | ||
+ | } | ||
+ | |||
+ | Serial.println(F(" | ||
+ | | ||
+ | // ****************************************************** | ||
+ | // You can safely remove this to save some flash memory! | ||
+ | // ****************************************************** | ||
+ | Serial.println(F(" | ||
+ | Serial.println(F(" | ||
+ | Serial.println(F(" | ||
+ | Serial.println(F(" | ||
+ | Serial.println(F(" | ||
+ | | ||
+ | // Start listening for connections | ||
+ | httpServer.begin(); | ||
+ | | ||
+ | Serial.println(F(" | ||
+ | } | ||
+ | |||
+ | void loop(void) | ||
+ | { | ||
+ | // Try to get a client which is connected. | ||
+ | Adafruit_CC3000_ClientRef client = httpServer.available(); | ||
+ | if (client) { | ||
+ | Serial.println(F(" | ||
+ | // Process this request until it completes or times out. | ||
+ | // Note that this is explicitly limited to handling one request at a time! | ||
+ | |||
+ | // Clear the incoming data buffer and point to the beginning of it. | ||
+ | bufindex = 0; | ||
+ | memset(& | ||
+ | | ||
+ | // Clear action and path strings. | ||
+ | memset(& | ||
+ | memset(& | ||
+ | |||
+ | // Set a timeout for reading all the incoming data. | ||
+ | unsigned long endtime = millis() + TIMEOUT_MS; | ||
+ | | ||
+ | // Read all the incoming data until it can be parsed or the timeout expires. | ||
+ | bool parsed = false; | ||
+ | while (!parsed && (millis() < endtime) && (bufindex < BUFFER_SIZE)) { | ||
+ | if (client.available()) { | ||
+ | buffer[bufindex++] = client.read(); | ||
+ | } | ||
+ | parsed = parseRequest(buffer, | ||
+ | } | ||
+ | |||
+ | // Handle the request if it was parsed. | ||
+ | if (parsed) { | ||
+ | Serial.println(F(" | ||
+ | Serial.print(F(" | ||
+ | Serial.print(F(" | ||
+ | // Check the action to see if it was a GET request. | ||
+ | if (strcmp(action, | ||
+ | // Respond with the path that was accessed. | ||
+ | // First send the success response code. | ||
+ | client.fastrprintln(F(" | ||
+ | // Then send a few headers to identify the type of data returned and that | ||
+ | // the connection will not be held open. | ||
+ | client.fastrprintln(F(" | ||
+ | client.fastrprintln(F(" | ||
+ | client.fastrprintln(F(" | ||
+ | // Send an empty line to signal start of body. | ||
+ | client.fastrprintln(F("" | ||
+ | // Now send the response data. | ||
+ | client.fastrprintln(F(" | ||
+ | client.fastrprint(F(" | ||
+ | } | ||
+ | else { | ||
+ | // Unsupported action, respond with an HTTP 405 method not allowed error. | ||
+ | client.fastrprintln(F(" | ||
+ | client.fastrprintln(F("" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Wait a short period to make sure the response had time to send before | ||
+ | // the connection is closed (the CC3000 sends data asyncronously). | ||
+ | delay(100); | ||
+ | |||
+ | // Close the connection when done. | ||
+ | Serial.println(F(" | ||
+ | client.close(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Return true if the buffer contains an HTTP request. | ||
+ | // path and action strings if the request was parsed. | ||
+ | // parse any HTTP headers because there really isn't enough memory to process | ||
+ | // them all. | ||
+ | // HTTP request looks like: | ||
+ | // [method] [path] [version] \r\n | ||
+ | // Header_key_1: | ||
+ | // ... | ||
+ | // Header_key_n: | ||
+ | // \r\n | ||
+ | bool parseRequest(uint8_t* buf, int bufSize, char* action, char* path) { | ||
+ | // Check if the request ends with \r\n to signal end of first line. | ||
+ | if (bufSize < 2) | ||
+ | return false; | ||
+ | if (buf[bufSize-2] == ' | ||
+ | parseFirstLine((char*)buf, | ||
+ | return true; | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | // Parse the action and path from the first line of an HTTP request. | ||
+ | void parseFirstLine(char* line, char* action, char* path) { | ||
+ | // Parse first word up to whitespace as action. | ||
+ | char* lineaction = strtok(line, | ||
+ | if (lineaction != NULL) | ||
+ | strncpy(action, | ||
+ | // Parse second word up to whitespace as path. | ||
+ | char* linepath = strtok(NULL, | ||
+ | if (linepath != NULL) | ||
+ | strncpy(path, | ||
+ | } | ||
+ | |||
+ | // Tries to read the IP address and other connection details | ||
+ | bool displayConnectionDetails(void) | ||
+ | { | ||
+ | uint32_t ipAddress, netMask, defaultGateway, | ||
+ | | ||
+ | Serial.print(F(" | ||
+ | Serial.print(F(" | ||
+ | Serial.print(F(" | ||
+ | Serial.print(F(" | ||
+ | Serial.println(); | ||
+ | return true; | ||
+ | | ||
+ | } | ||
+ | </ | ||
informatica/arduino/wifi.1481136654.txt.gz · Last modified: 2016/12/07 18:50 by jose