void response(char* url) {

    // default page is the error page
    const char* page = error;

    // Get the length of our URL and remove the null termination
    int urlLen = strlen(url) - 1;

    // HOME: check only for the length of the URL, since the home page
    // is represented by a forward slash /
    if(urlLen == 1) {

        page = home;

    }

    // MORE: look for the word
    if(memcmp(url, "/more", 5) == 0) {

        page = more;

    }

    // HOME: look for the word
    if(memcmp(url, "/author", 6) == 0) {

        page = author;

    }

    // Inform the browser that everything is ok
    clientWww.write("HTTP/1.1 200 OK\n\r");

    // Tell the encoding that we are using
    clientWww.write("Content-Type: text/html; charset=ascii\n\r");

    // Separate the header from the body with one empty line
    clientWww.write("\n\r");

    // Send our content
    clientWww.write(page);

    // Close the connection
    clientWww.stop();

}
Exemplo n.º 2
0
void loop () {
    if (!initialised && !connected && (lastCheck + 5000 < millis())) {
        if (WiFi.ready()) {
            connect("www.apple.com", 80);
            if (connected) {
                client.println("GET /library/test/success.html HTTP/1.1");
                client.println("Host: www.apple.com");
                client.println();
                const char *success = "Success";
                unsigned int successLocation = 0;
                while (true) {
                    if (client.available()) {
                        int chr = client.read();
                        if (success[successLocation] == chr) {
                            successLocation ++;
                            if (successLocation == strlen(success)) {
                                initialised = true;
                                Spark.connect();
                                Spark.function("digitalread", tinkerDigitalRead);
                                Spark.function("digitalwrite", tinkerDigitalWrite);
                                Spark.function("analogread", tinkerAnalogRead);
                                Spark.function("analogwrite", tinkerAnalogWrite);
                                client.stop();
                                break;
                            }
                        }
                        else {
                            successLocation = 0;
                        }
                    }
                    else {
                        delay(2);
                    }
                }
            }
        }
        else {
            return;
        }
    }

    if (Serial.available()) {
        if (connected) {
            int bytes = Serial.available();
            for (int i=0; i<bytes; i++) {
                client.write(readSerial());
            }
        }
        else {
            int cmd = readSerial();
            switch (cmd) {
                case 'i':
                    Serial.println("Ready");
                    break;
                case 'c': {
                    char* host;
                    char* stringPort;
                    readWord(); // junk space
                    host = readWord();
                    stringPort = readWord();
                    connect(host, atol(stringPort));
                    free(host);
                    free(stringPort);
                    break;
                }
                case 'w': {
                    char* ssid;
                    char* password;
                    readWord();
                    ssid = readWord();
                    password = readWord();
                    Serial.print("Wifi credentials set to SSID '");
                    Serial.print(ssid);
                    Serial.print("' and password '");
                    Serial.print(password);
                    Serial.println("'");
                    WiFi.setCredentials(ssid, password);
                    break;
                }
                default:
                    Serial.print("Don't know command: ");
                    Serial.write(cmd);
                    Serial.println();
                    break;
            }
        }
    }
    if (connected) {
        int bytes = client.available();
        for (int i=0; i<bytes; i++) {
            Serial.write(client.read());
        }
        
        if (!client.connected()) {
            Serial.println();
            Serial.println("Disconnecting");
            client.stop();
            connected = false;
        }
    }
}
char * http_get(char const * hostname, String path) {

    int i = 0;
    int j = 0;
    int k = 0;
    bool printOnce = false;

    if (client.connect(hostname, 80)) {
        client.print("GET ");
        client.print(path);
        client.println(" HTTP/1.1");
        client.print("HOST: ");
        client.println(hostname);
        client.println("Connection: close");
        client.println();
        client.flush();
    } else {
        if(DEBUG_SERIAL) Serial.println("\r\n\r\nConnection Failed!");
        client.flush();
        client.stop();
        return NULL;
    }

    // wait 5 seconds or less for the host to respond
    uint32_t startTime = millis();
    while(!client.available() && (millis() - startTime) < 5000);

    if(DEBUG_SERIAL) Serial.println("\r\n\r\nREADING HOST DATA......");
    uint32_t lastRead = millis();
    // If the host doesn't close it's connection, we'll timeout in 10 seconds.
    while (client.connected() && (millis() - lastRead) < 10000) {
        while (client.available()) {
            char c = client.read();
            /*
            if(DEBUG_SERIAL) {
              Serial.print(c);
              if(i++ > 100) {
                delay(5);
                i = 0;
              }
            }
            */
            if(c == -1) {
                Serial.print("\r\n\r\nERROR......\r\n\r\n");
                client.flush();
                client.stop();
            }
            if(j++ >= SKIP_CHARS) { // don't buffer the first X bytes to save memory
                if(DEBUG_SERIAL && !printOnce) {
                    Serial.print("\r\n\r\nSAVING......\r\n\r\n");
                    printOnce = true;
                }
                buffer[k++] = c; // save character to buffer
                Serial.print(c);
                delayMicroseconds(100);
                if(k >= BUFFER_SIZE_MAX) { // if we reach the end of our buffer, just bail.
                    Serial.print("\r\n\r\nOUT OF BUFFER SPACE......\r\n\r\n");
                    client.flush();
                    client.stop();
                }
            }
            // as long as we're reading data, reset the lastRead time.
            lastRead = millis();
        } // END while(client.available())
    } // END while(client.connected())

    client.flush();
    client.stop();

    if(DEBUG_SERIAL) {
        Serial.print("\r\nCHARACTERS RECEIVED: ");
        Serial.println(SKIP_CHARS + k);
    }

    return buffer;
}
Exemplo n.º 4
0
void loop()
{
    int debug = digitalRead(debug_tx);
    
    if(!digitalRead(waitpin_C)){
        // Skip mode, to ensure bootloader stays available
        char buffer[32];
        sprintf(buffer,"skip state: %d\r\n",state);
        Serial1.print(buffer);
        digitalWrite(led, HIGH);   // Turn ON the LED       
        delay(100);
        return;
    }else{
        digitalWrite(led, LOW);   // Turn OFF the LED  
    }

    delay(1);
    switch(state){
        case 0:
            // Waiting for next time
            wait-=10;
            if(wait<0){
                wait = 0;
                state = 1;
            }
            break;
        case 1:
            // Connecting
            if(debug) Serial1.println("connecting");
            if (client.connect(server, 80)){
                if(debug) Serial1.println("connected");
                state = 2;
            }else{
                if(debug) Serial1.println("connection failed state 1");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 2:
            // Requesting
            tries = 0;
            store = 0;
            hash = 0;
            command_i = 0;
            for(int i=0; i<COMMAND_SIZE; i++) command[i] = 0;
            if(client.connected()){
                if(debug) client.println("GET // HTTP/1.0\r\n\r\n");
                wait = RESPONSE_INTERVAL;
                state = 3;
            }else{
                if(debug) Serial1.println("connection lost state 2");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 3:
            // Receiving
            if(client.connected()){
                if (client.available() > 0) 
                {
                    // Print response to serial
                    char c = client.read();
                    if(debug) Serial1.print(c);
                    
                    // If last expected char found, quit reading
                    if(c =='>') hash = 1;
                    // If first char of data found, start storing the string
                    if(c =='<') store = 1;
                    if(store){
                        command[command_i++] = c;
                    }
                }else{
                    if(debug) Serial1.println("nd s3 ");  
                    delay(100);
                }
                // Quit reading
                if(hash){
                    Serial1.println();
                    state = 4;
                }
            }else{
                // We lost connection
                if(debug) Serial1.println("connection lost state 3");
                wait = RETRY_INTERVAL;
                state = 0;
            }
            break;
        case 4:
            // Disconnecting
            if(client.connected()){
                client.stop();
                if(debug) Serial1.println("connection closed state 4");
                wait = RENEW_INTERVAL;
                state = 5;
            }else{
                if(debug) Serial1.println("connection closed by server state 4");
                wait = RENEW_INTERVAL;
                state = 5; 
            }
            break;
        case 5:
        {
            int code = 0;
            // Parse data read
            client.stop();
            if(debug) Serial1.println("I've got this:");
            for(int i=0; i<COMMAND_SIZE; i++) Serial1.write(command[i]);
            Serial1.println("");
            
            // Control display
            //int code = (command[1]*1000)+(command[2]*100)+(command[3]*10)+(command[4]*1);
            //float temp = atoi((const char*)&command[6]) + (0.1 * atoi((const char*)&command[8]));
            sscanf((const char*)command,"<%d;%f>",&code,&temperature);
            
            if(debug) Serial1.print("Code: ");Serial1.println(code);
            if(debug) Serial1.print("Temp: ");Serial1.println(temperature);
  
            display(temperature);
        
            
            /*
            char tempString[10]; //Used for sprintf
            Serial1.println();
            Serial1.write('v');
            sprintf(tempString, "%4d", temp); //Convert deciSecond into a string that is right adjusted
            Serial1.print(tempString);
            */
            wait = RENEW_INTERVAL;
            state = 0;
        }
            break;
            
    }
}
Exemplo n.º 5
0
int tcpDisconnect(String param) {
    tcp.flush();
    tcp.stop();
    return 1;
}
Exemplo n.º 6
0
// Called in a loop forever
void loop()
{
	// When you push the button
	if (digitalRead(D0) == HIGH)
	{
		// It might take a couple of loop() iterations to
		// successfully connect to iamresponding.com
		connecting = true;

		// This is how to control the Spark's RGB LED
		RGB.control(true);

		// Red to indicate we're attempting to connect
		RGB.color(255, 0, 0);

		// Push the button once to respond to station.
		// Push it again to cancel.
		if (responding)
		{
			// Do just what the iamresponding.com app does
			data = "{\"keyEntryDesc\":\"CANCELLED\",\"memberIDS\":\"\",\"subscriberID\":\"\",\"userfullname\":\"Jack Bates\"}";
		}
		else
		{
			data = "{\"keyEntryDesc\":\"Station\",\"memberIDS\":\"\",\"subscriberID\":\"\",\"userfullname\":\"Jack Bates\"}";
		}
	}

	// We haven't successfully connected since the button was last
	// pushed, keep trying!
	if (connecting && client.connect(server, 80)) // HTTP is port
	{					      // number 80, FYI
		// We successfully connected, stop trying
		connecting = false;

		// Yellow to indicate we successfully connected and
		// sent the magic words
		RGB.color(255, 255, 0);

		// Again, just ape the iamresponding.com app
		client.println("POST /v3/keyEntries.asmx/AddCallEntry HTTP/1.0");

		client.print("Content-Length: ");
		client.println(strlen(data));

		client.println("Content-Type: application/json");
		client.println("Host: iamresponding.com");
		client.println();

		// Send the magic words
		client.print(data);
	}

	// iamresponding.com sent something back to *us*.
	// (This is our only indication that TCPClient is done
	// transmitting the magic words.  You're welcome to get clever
	// and check if iamresponding.com sent back confirmation or is
	// reporting an error, this just assumes it's confirmation.)
	if (client.available())
	{
		// If we were already responding, then now we aren't
		// (cancelled) and vice versa (if we weren't, then now
		// we are).
		responding = !responding;
		if (responding)
		{
			// Blue to indicate that iamresponding.com
			// knows we're responding to station!
			RGB.color(0, 0, 255);
		}
		else
		{
			// Restore the default RGB LED behavior when
			// we're not responding (cancelled).
			// (By default it shows the Wi-Fi status.)
			RGB.control(false);
		}

		// Tricky: Clean up after transmitting the magic
		// words.  Close the connection to iamresponding.com
		// and discard (flush) anything sent back to us, or
		// else in the next loop() iteration we'll think
		// iamresponding.com sent back something *again*.
		client.stop();
		client.flush();
	}
}
void out(const char *s) {server.write( (const uint8_t*)s, strlen(s) );  client.stop();}