Go Down

Topic: Sending multiple http requests with a continuous button-press on html page (Read 42 times) previous topic - next topic


Code: [Select]
#include <Adafruit_CC3000.h>
#include <SPI.h>
#include "utility/debug.h"
#include "utility/socket.h"

#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed

#define WLAN_SSID       "-"   // cannot be longer than 32 characters!
#define WLAN_PASS       "-"  // Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2

#define LISTEN_PORT           80      // What TCP port to listen on for connections. 
                                      // The HTTP protocol uses port 80 by default.

Adafruit_CC3000_Server httpServer(LISTEN_PORT);
int i=0;
String HTTP_req;
void setup()
    Serial.println(F("Hello, CC3000!\n"));
  // Initialise the module
  if (!cc3000.begin())
    Serial.println(F("Couldn't begin()! Check your wiring?"));
  Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
  if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
  Serial.println(F("Listening for connections...")); 

void loop()
    Adafruit_CC3000_ClientRef client = httpServer.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                HTTP_req += c;  // save the HTTP request 1 char at a time
                if (c == '\n' && currentLineIsBlank) {
                    client.println(F("HTTP/1.1 200 OK"));
                    client.println(F("Content-Type: text/html"));
                    client.println(F("Connection: keep-alive"));
                    if (HTTP_req.indexOf("Front") > -1)
                        client.print(" Front");                     
                    else {  // HTTP request for web page
                        // send web page - contains JavaScript with AJAX calls
                        client.println(F("<!DOCTYPE html>"));
                        client.println(F("<title>Arduino Web Page</title>"));
                        client.println(F("  var mousedownID = -1; "));     //Global ID of mouse down interval
                        client.println(F("  function Mousedown(id) {  "));
                        client.println(F("    if(mousedownID == -1)  "));   //Prevent multimple loops!
                        client.println(F("    mousedownID = setInterval(whilemousedown(id), 5000 ); } "));
                        client.println(F("  function mouseup() { "));
                        client.println(F("    if(mousedownID!=-1) {  ")); //Only stop if exists
                        client.println(F("      clearInterval(mousedownID); "));
                        client.println(F("      mousedownID=-1;  "));
                        client.println(F("      document.getElementById('State').innerHTML = 'Stopped moving'; }} "));
                        client.println(F("  function whilemousedown(id) {   "));
                        client.println(F("    nocache = '&nocache' + Math.random() * 1000000; ")); 
                        client.println(F("    var request = new XMLHttpRequest(); "));
                        client.println(F("    request.onreadystatechange = function(id) { "));
                        client.println(F("      if (this.readyState == 4) { "));
                        client.println(F("      if(this.status == 200) {  "));
                        client.println(F("      if(this.responseText != null) { "));
                        client.println(F("      document.getElementById('State').innerHTML = 'Moving' + this.response; }}}} "));
                        client.println(F("    request.open('GET',id + nocache, true); "));
                        client.println(F("    request.send(null); } ")); 
                        client.println(F("<h1>Arduino AJAX Switch Status</h1>"));
                        //client.println(F("<p id='switch_txt'>Switch state: Not requested...</p>"));
                        //client.println(F("<button type='button' onmousedown='GetSwitchState()'>Get Switch State</button>"));
                        client.println(F("  <h1 id='State'> State.. </h1> "));
                        client.println(F("  <table style='width:10%'> "));
                        client.println(F("  <tr><th><div style='margin-left:43px'>  <button  id='Front'   onmousedown='Mousedown(this.id)' onmouseup='mouseup()'> Front </button>  </div></th></tr> "));
                        client.println(F("  <tr><td>  <button  id='Left'  onmousedown='Mousedown(this.id)' onmouseup='mouseup()'> Left  </button>  </td> "));
                        client.println(F("  <td>    <button  id='Right' onmousedown='Mousedown(this.id)' onmouseup='mouseup()'> Right </button>  </td></tr> "));
                        client.println(F("  <tr><th><div style='margin-left:43px'>  <button  id='Back'  onmousedown='Mousedown(this.id)' onmouseup='mouseup()'> Back  </button>  </div></th></tr> "));
                        client.println(F("  </table> "));
                    // display received HTTP request on serial port
                    HTTP_req = "";            // finished with request, empty string
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

First of all.. Hello this is my first post here! Explaining is not my strong part so Ill do my best!

So here's my problem. I have this code. What i want it to do is when i press a button Front(for start) to send multiple same http requests until I stop pressing the button.

Right now when i press the button Front it does print Moving Front continuously as it should but this happen with only 1 http request.
I want it to send multiple requests because afterwards I will attach 2 motors to move a tiny robot while buttons are pressed to the direction accordingly.

I thought setInterval would do the trick but no matter how big delay i place it still doesn't send more so I'm guessing there is something else.

Furthermore, since i told you about the motors if you have any side idea on how to do so with not multiple requests it's fine by me!
Bottom line will be to move motors while i press a button down and stop if i stop pressing the button. Whatever method is accepted.

Sideproblem: I tried turning up the onboard led while i press the button down for debug purposes and it didn't open. When i put a program only to do so it works perfectly. If you have a clue about that it would be a great bonus! :D

Thank you for your time!!!!


You don't seem to understand how client/server communications happens, nor to understand how SLOW the process is.

What you really need to do is have the device start moving forward when the forward button is pressed, and NOT stop moving until told otherwise.

Your functions are (properly) invoked when the mouse changes state. Your robot should do the same.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131