Last active
November 7, 2019 17:46
-
-
Save damienalexandre/6080444 to your computer and use it in GitHub Desktop.
Arduino code making an HTTP request to Google Analytics and returning the status code. Replace `YOURGAID-HERE` by your GA tag.
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
#include <SPI.h> | |
#include <Ethernet.h> | |
// Thx http://bildr.org/2012/11/force-sensitive-resistor-arduino/ | |
// Make use of https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide | |
// MAC Address, needed by the Ethernet Shield | |
byte mac[] = { 0xB0, 0xC0, 0xDE, 0xEF, 0xFE, 0xC7 }; | |
int pin_red = 7; // Red LED | |
int pin_green = 8; // Green LED | |
int pin_FSR = A0; // Force sensor pin | |
int pin_POT = A1; // Potentiometer pin | |
// To skip the DNS | |
IPAddress server(173,194,40,97); // www.google-analytics.com => 173.194.40.97 | |
EthernetClient client; | |
// Global switch (debug mode without API call) | |
boolean netword_enabled = true; | |
/** | |
* Perform an HTTP request to Google Analytics | |
* | |
* @return int The HTTP response code | |
*/ | |
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=YOURGAID-HERE&cid=1337&t=event&ec=irl&ea=visitor&el=IRL Visit&ev=1"; // 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() { | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
pinMode(pin_red, OUTPUT); | |
pinMode(pin_green, OUTPUT); | |
// Turn off the lights | |
digitalWrite(pin_green, LOW); | |
digitalWrite(pin_red, LOW); | |
// start the Ethernet connection | |
if (netword_enabled && Ethernet.begin(mac) == 0) { | |
Serial.println("Failed to configure Ethernet using DHCP"); | |
digitalWrite(pin_red, HIGH); | |
// no point in carrying on, so do nothing forevermore: | |
for(;;) | |
; | |
} | |
// give the Ethernet shield a second to initialize: | |
delay(1000); | |
} | |
void loop() | |
{ | |
int FSRReading = analogRead(pin_FSR); | |
int threshold = analogRead(pin_POT); | |
int responseCode; | |
// If the pressure is higher than the setted threshold, call the API | |
if (FSRReading > threshold) { | |
digitalWrite(pin_green, HIGH); | |
responseCode = sendEventToGA(); | |
if (responseCode >= 200 && responseCode < 400) { | |
digitalWrite(pin_green, HIGH); | |
digitalWrite(pin_red, LOW); | |
} else { | |
digitalWrite(pin_green, LOW); | |
digitalWrite(pin_red, HIGH); | |
Serial.print("Something went wrong, API replied: "); | |
Serial.println(responseCode); | |
} | |
} | |
Serial.print("Pressure: "); | |
Serial.print(FSRReading); | |
Serial.print(" - Potentiometer: "); | |
Serial.println(threshold); | |
delay(500); | |
digitalWrite(pin_green, LOW); | |
digitalWrite(pin_red, LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment