2

I've done this laser trip wire: http://www.instructables.com/id/Another-Arduino-Laser-Tripwire/

Got it working, and now I'd like to send info to analytics - to measure how many times I enter the kitchen...

I tried to take bits of code and add it to my code from: http://bigsnarf.wordpress.com/2013/12/24/arduino-sensor-python-and-google-analytics/ and one more which I can't post due to permissions....

but neither will work and adding this code breaks my basic functionality (I know as I measure it via the buzzer, it stops responding once I add this new code...

Is there anyone able to help?


Here is my basic working code for the laser trip wire that makes buzzer noise if someone walks through it:

/*
Jonathan Robson Feb 09.
Laser tripwire activates a buzzer. adapted from
http://www.instructables.com/id/Arduino_Laser_Tripwire/
*/

int buzzPin = 11; // buzzer connected to digital pin 11

void setup() {
  pinMode(buzzPin, OUTPUT); // sets the digital pin as output
}

void loop(){
  if(analogRead(0) < 850){ // this number depends on calibration of the photocell
    digitalWrite(buzzPin, HIGH); // turns buzzer on
    delay(1000); // waits for 1 second
    digitalWrite(buzzPin, LOW); // turns buzzer off
  } else{
    digitalWrite(buzzPin, LOW);
  }
}

Here is the code with analytics additions I’ve added:

/*
Jonathan Robson Feb 09.
Laser tripwire activates a buzzer. adapted from
http://www.instructables.com/id/Arduino_Laser_Tripwire/
*/
#include <SPI.h>
#include <Ethernet.h>

byte mac[]     = {  0x0C, 0xD2, 0x92, 0xAC, 0x75, 0x0B };

// To skip the DNS
IPAddress server(173,194,40,97); // www.google-analytics.com => 173.194.40.97
EthernetClient client;

int buzzPin = 11; // buzzer connected to digital pin 11


// Global switch (debug mode without API call)
boolean netword_enabled = true;

int sendEventToGA() {
  int responseCode      = 0;

  if (!netword_enabled) {
    return responseCode;  
  }

  // open the connection
  if (client.connect(server, 80)) {
    int responseSize      = 0;
    int responseSizeMax   = 1024;
    char response[1024];
    String eventData = "v=1&tid=UA-51525058-1&cid=123&t=event&ec=home%20movement&ea=door%20open&el=bedroom"; // Refer to the Google documentation

    Serial.println("Connection opened!");

    // Write the HTTP request to the Client    
    client.println("POST /collect HTTP/1.1");
    client.println("Host: www.google-analytics.com");
    client.print("Content-Length: ");
    client.println(eventData.length());
    client.println("Content-Type:application/x-www-form-urlencoded");
    client.println();
    client.println(eventData);

    Serial.println("Request sent.");

    // Wait a little bit
    delay(1000);

    // Read the response, looking for the Response Code.
    while (client.available() && client.connected() && responseCode < 100 && responseSize < responseSizeMax) {   

      response[responseSize] = client.read();
      responseSize++;
      response[responseSize] = '\0'; // Set manualy the end of string char

      // Here is the magic!
      sscanf (response, "HTTP/1.%*d %d", &responseCode);

      Serial.print("HTTP code ");
      Serial.print(responseCode);
      Serial.print(" founded in ");
      Serial.println(response);
    }
  }

  Serial.println("End of communications.");

  client.stop();
  return responseCode;
}


void setup() {
  pinMode(buzzPin, OUTPUT); // sets the digital pin as output
  Serial.begin(9600);

  // start the Ethernet connection
  if (netword_enabled && Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");

    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }

  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop(){
  int responseCode;
  if(analogRead(0) < 850){ // this number depends on calibration of the photocell
    digitalWrite(buzzPin, HIGH); // turns buzzer on
    responseCode = sendEventToGA();
    delay(1000); // waits for 1 second
    digitalWrite(buzzPin, LOW); // turns buzzer off
  } else{
    digitalWrite(buzzPin, LOW);
  }
}
7
  • 2
    Welcome to Arduino SE! Can you please add your code? If you "can't post it because of permissions," you probably can't even use it. Also, it's generally okay if you cite your source (although it's your call). Either way, it's impossible to tell you how to fix your code without seeing it. If you can narrow it down to a small section and post that, it'd be fine to do so (along with any dependencies/extra functions it calls). Until then, we can't really help you. The only advice I can give you is this: does the code sending it to the serial port block the checking function? Is there a delay()? Jun 21, 2014 at 15:47
  • I'm trying but my code is over 2k characters and there's a limit here to 600 characters per response.....
    – KrystianS
    Jun 22, 2014 at 8:48
  • 1
    I also started reading an arduino book and it mentions Ethernet shield - so I ordered that... Here is a link to my code hosted on Google docs (I couldn't figure out how to use github) docs.google.com/document/d/…
    – KrystianS
    Jun 22, 2014 at 9:12
  • Do you get no buzzer? Or does the buzzer start, but not stop?
    – Gerben
    Jun 22, 2014 at 14:16
  • 2
    I'm voting to close this question as off-topic because it has been abandoned for 3 years without information to make it answerable ever being provided. Feb 25, 2018 at 18:32

1 Answer 1

1

As with any piece of code that doesn't work, it is very easy to troubleshoot using the Serial.println() statement.

The big clue here is that the buzzer turns on but never turns off. Go ahead and add unique Serial.println() statements between each statement in the loop() function.

My guess is that the digitalWrite(buzzPin, HIGH) executes but it never exits the sendEventToGA() function.

If this is the case, then go ahead and add unique Serial.println() statements in between each line of the sendEventToGA() code.

Again, my guess is that the code goes into the while() statement and never comes out. This while statement is waiting for a response from the server. If you never get a response it won't exit.

Perhaps add a timeout or determine why Google Analytics isn't returning a response.

Help on the topic can be found at the Arduino Libraries reference pages:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.