Last active
June 14, 2016 19:24
-
-
Save LindaLawton/5f30e63c1e44a4f1d82713ee991b8da4 to your computer and use it in GitHub Desktop.
sending hits to Google Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "HttpClient/HttpClient.h" | |
/** | |
* Declaring the variables. | |
*/ | |
unsigned int nextTime = 0; // Next time to contact the server | |
HttpClient http; | |
// Headers currently need to be set at init, useful for API keys etc. | |
http_header_t headers[] = { | |
// { "Content-Type", "application/json" }, | |
// { "Accept" , "application/json" }, | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
// for light sencor | |
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below). | |
int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above). | |
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor. | |
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop. | |
int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor. | |
String status ; | |
char LightLevelString[40]; | |
char postString[2000]; | |
int lastValue; | |
int loopcounter; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(photoresistor,INPUT); // Our photoresistor pin is input (reading the photoresistor) | |
pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power) | |
// Next, write one pin of the photoresistor to be the maximum possible, so that we can use this for power. | |
digitalWrite(power,HIGH); | |
// We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud. | |
Particle.variable("analogvalue", &analogvalue, INT); | |
lastValue = 0 ; | |
loopcounter = 0; | |
} | |
void loop() { | |
request.hostname = "www.google-analytics.com"; | |
request.port = 80; | |
analogvalue = analogRead(photoresistor); | |
sprintf(LightLevelString,"%u",analogvalue); | |
// Send current light level once a minute | |
if (loopcounter >= 60) { | |
sprintf(postString,"/collect?v=1&tid=xxxx&cid=5555&t=screenView&cd=LivingRoom&an=ParticalTesting&cm1=%u" , analogvalue); | |
Serial.println(); | |
Serial.println("Application>\tStart of Loop."); | |
// Request path and body can be set at runtime or at setup. | |
request.path = postString; | |
// The library also supports sending a body with your request: | |
//request.body = "{\"key\":\"value\"}"; | |
// Get request | |
http.get(request, response, headers); | |
Serial.print("Application>\tResponse status: "); | |
Serial.println(response.status); | |
Serial.print("Application>\tHTTP Response Body: "); | |
Serial.println(response.body); | |
loopcounter = 0; | |
} | |
loopcounter = loopcounter + 1; | |
if(lastValue > analogvalue) { | |
// light level has gone down | |
int diff = lastValue - analogvalue; | |
if(diff > 5) { | |
Particle.publish("LivingRoom-Light-level", "LightDecrease", diff, PRIVATE); | |
sprintf(postString,"/collect?v=1&tid=xxxxx&cid=5555&t=event&ec=LightLevel&ea=Decrease&ev=%u&an=ParticalTesting&cm1=%u" , analogvalue); | |
request.path = postString; | |
http.get(request, response, headers); | |
} | |
} else if (lastValue < analogvalue) { | |
// Level has gone down | |
int diff = analogvalue - lastValue; | |
if(diff > 5) { | |
Particle.publish("LivingRoom-Light-level", "LighIncrease", diff, PRIVATE); | |
sprintf(postString,"/collect?v=1&tid=xxxxx&cid=5555&t=event&ec=LightLevel&ea=Increase&ev=%u&an=ParticalTesting&cm1=%u" , analogvalue); | |
request.path = postString; | |
http.get(request, response, headers); | |
} | |
} | |
lastValue = analogvalue; | |
Particle.publish("LivingRoom-Light-level", LightLevelString, analogvalue, PRIVATE); | |
Particle.publish("LivingRoom-Light-Post", postString, analogvalue, PRIVATE); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment