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);
}
}
1 Answer 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:
delay()
?