Ben Zhang / Mbed 2 deprecated CC3000_Nodejs

Dependencies:   HTTPClient WebSocketClient cc3000_hostdriver_mbedsocket mbed

Fork of CC3000_demo by Ben Zhang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  *  \brief CS294-84 demo \author Ben Zhang, Antonio Iannopollo
00003  *
00004  * This sampel code illustrates how to connect the mbed KL25Z platform to internet
00005  * thorugh the CC3000 wifi breakout board (http://www.adafruit.com/product/1469).
00006  * Connections between the KL25Z and the CC3000 are made according to the
00007  * guide at https://learn.adafruit.com/adafruit-cc3000-wifi -- KL25Z and arduino
00008  * UNO are pin to pin compatible --
00009  *
00010  * This application uses the following libraries:
00011  * - cc3000_hostdriver_mbedsocket
00012  *   (http://developer.mbed.org/users/Kojto/code/cc3000_hostdriver_mbedsocket/)
00013  * - HTTPClient (http://developer.mbed.org/users/donatien/code/HTTPClient/)
00014  */
00015 
00016 #include <string>
00017 
00018 #include "cc3000.h"
00019 #include "mbed.h"
00020 #include "HTTPClient.h"
00021 
00022 const string POLLING_URL = "http://universe.eecs.berkeley.edu:8080/value";
00023 const string UPDATE_URL = "http://universe.eecs.berkeley.edu:8080/update?value=";
00024 
00025 // KL25Z wifi connection
00026 // we need to define connection pins for:
00027 // - IRQ      => (pin D3)
00028 // - Enable   => (pin D5)
00029 // - SPI CS   => (pin D10)
00030 // - SPI MOSI => (pin D11)
00031 // - SPI MISO => (pin D12)
00032 // - SPI CLK  => (pin D13)
00033 // plus wifi network SSID, password, security level and smart-configuration flag.
00034 mbed_cc3000::cc3000 wifi(D3, D5, D10, SPI(D11, D12, D13),
00035                          "SSID", "PASSWORD", WPA2, false);
00036 
00037 // create an http instance
00038 HTTPClient http;
00039 
00040 // str is used to hold the response data
00041 char str[512];
00042 char url[80];
00043 
00044 // setup the serial connection, and LEDs
00045 Serial pc(USBTX, USBRX);
00046 DigitalOut led_green(LED_GREEN);
00047 PwmOut led_red(LED_RED);
00048 
00049 Ticker reportSensorTicker;
00050 bool shouldReadSensor;
00051 int sensorData;
00052 
00053 void readSensor() {
00054     // avoid doing much work here (in the interrupt).
00055     // put the actual sensor reading in the main function
00056     shouldReadSensor = true;
00057 }
00058 
00059 int main() {
00060     pc.printf("CC3000 Nodejs Demo Starts\r\n");
00061     // by default, we make the LED red
00062     led_red = 0;
00063     led_green = 1;
00064 
00065     // reset the readsensor bool to false so we don't read the data.
00066     shouldReadSensor = false;
00067     sensorData = 0;
00068     // Every 5 second, we sample the sensor and report the data.
00069     reportSensorTicker.attach(&readSensor, 5.0); 
00070     
00071     wifi.init();
00072 
00073     while(1) {
00074         // continuosly check connection status
00075         if(wifi.is_connected() == false) {
00076             // try to connect
00077             if (wifi.connect() == -1) {
00078                 pc.printf("Failed to connect."
00079                           "Please verify connection details and try again.\r\n");
00080                 continue;
00081             } else {
00082 
00083                 pc.printf("IP address: %s \r\n", wifi.getIPAddress());
00084                 // once connected, turn green LED on and red LED off
00085                 led_red = 1;
00086                 led_green = 0;
00087             }
00088         }
00089 
00090         // update the value when shouldReadSensor is true          
00091         if (shouldReadSensor == true) {
00092             shouldReadSensor = false;
00093             sensorData += 10;
00094             char url[128];
00095             snprintf(url, sizeof(url), "%s%d", UPDATE_URL.c_str(), sensorData);
00096             int ret = http.get(url, str, 128);
00097             if (!ret) {
00098                 pc.printf("sending sensorData %d, response: %s\r\n", sensorData, str);
00099             } else {
00100                 pc.printf("[PUSH] Error - ret = %d - HTTP %d\r\n", ret, http.getHTTPResponseCode());
00101             }
00102         }
00103         
00104         // everytime we do HTTP polling
00105         int ret = http.get(POLLING_URL.c_str(), str, 128);
00106         if (!ret) {
00107             int value = atoi(str);
00108             led_red = 1.0 * value / 255;
00109         } else {
00110             pc.printf("[POLL] Error - ret = %d - HTTP %d\r\n", ret, http.getHTTPResponseCode());
00111         }
00112         wait(0.1);
00113     }
00114 }