Created
February 23, 2017 02:13
-
-
Save PaulTR/f1ce96ba2054b63b6c2c2f7599fe7d82 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
Written by Paul Trebilcox-Ruiz | |
Based on code by PuzzleDuck. | |
Original source and explaination: https://www.instructables.com/id/-Arduinolitics-Google-Analytics-on-Arduino/ | |
*/ | |
#include <ESP8266WiFi.h> | |
//#include "floatToString.h" | |
# define LED D4 // Use built-in LED which connected to D4 pin or GPIO 2 | |
const char* ssid = ""; | |
const char* password = ""; | |
String trackingID = ""; | |
int deviceID = 42; | |
String payloadCategory = "Arduino"; | |
String payloadAction = "Sensor"; | |
String payloadLabel = "Random"; | |
int trackingVersion = 1; | |
String payloadType ="event";// Must be one of 'pageview', 'appview', 'event', 'transaction', 'item', 'social', 'exception', 'timing'. | |
int payloadValue = 0; | |
char buffer[25]; | |
int sensorValue; | |
WiFiClient client; | |
char *host = "www.google-analytics.com"; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
pinMode(LED, OUTPUT); // Initialize the LED as an output | |
randomSeed(analogRead(0)); | |
deviceID = random(1000); //remove this line for static device ID. | |
WiFi.begin(ssid, password); | |
while( WiFi.status() != WL_CONNECTED ) { | |
delay(500); | |
} | |
if (!client.connect(host, 80)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
} | |
void loop() { | |
digitalWrite(LED, HIGH); // Turn the LED off because the LED is active low | |
delay(1000); // Wait a second | |
digitalWrite(LED, LOW); // Turn the LED on because the LED is active low | |
delay(1000); // Wait a second | |
int val = analogRead(A0); | |
sensorValue = map(val, 0, 1023, 1, 100); | |
String payloadData = "v="; | |
payloadData += trackingVersion; | |
payloadData += "&tid="; | |
payloadData += trackingID; | |
payloadData += "&cid="; | |
payloadData += deviceID; | |
payloadData += "&t="; | |
payloadData += payloadType; | |
payloadData += "&ec="; | |
payloadData += payloadCategory; | |
payloadData += "&ea="; | |
payloadData += payloadAction; | |
payloadData += "-"; | |
payloadData += deviceID; | |
payloadData += "&el="; | |
payloadData += payloadLabel; | |
payloadData += "&ev="; | |
// itoa(externalHumid, buffer,10); //needed for floats | |
payloadData += sensorValue; | |
if (client.connected()) { | |
client.println("POST /collect HTTP/1.1"); | |
client.println("Host: www.google-analytics.com"); | |
client.print("Content-Length: "); | |
client.println(payloadData.length()); | |
client.println(); | |
client.print(payloadData); | |
client.println(); | |
Serial.print("connected: "); | |
Serial.print(client.connected()); | |
Serial.print(" Avaliable: "); | |
Serial.print(client.available()); | |
Serial.print("\n"); | |
} else { | |
Serial.println("failed.. just skipping a beat for now"); | |
} | |
unsigned long timeout = millis(); | |
while (client.available() == 0) { | |
if (millis() - timeout > 5000) { | |
Serial.println(">>> Client Timeout !"); | |
Serial.println(">>> Client Timeout !"); | |
Serial.println(">>> Client Timeout !"); | |
Serial.println(">>> Client Timeout !"); | |
client.stop(); | |
return; | |
} | |
} | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment