LOGIN

Hacking The Mixpanel API with Arduino UNO

Tapping into the Mixpanel API from my 'Duino Uno

JOIN PROJECT
2 Contributions Made
1 Milestones
4 Participating Countries
Electronics
Passion

Tapping into the Mixpanel API from my 'Duino Uno

MENTOR
Anas Dalintakam
Electronics engineer,DIY thinkerer,arduino/raspberry pi/particle enthusiast,IoT researcher
Calicut, Kerala, India
Playing with new DIY components is my first hobby.And blogging simple tutorials on those is my second hobby.I am happy to help community
STORY

Project Overview

I continue to stress that Arduino is a real, yet amazing addiction. This weekend, I had an intensely strong urge to start a new Arduino Project. The thing is, I did not want this to be a meaningless project, even if it is primarily to learn.

My top 2 ideas initially

  1. Sling shot for the office

  2. A wifi + arduino + lcd project that could somehow tap into Mixpanel data

I decided to go with option 2 because it seems like a much more effective learning project, with knowledge we can apply later.

How the Project Will Work

It’s pretty simple to understand.

–> Wifi card taps into mixpanel’s api, pulls the most recent event to happen on Collaborizm Live Server --> Display on LCD.

how it will appear

circuit diagram

https://circuits.io/circuits/2923704-mixpanel-live-activity-to-lcd

Current Task in Progress

There are 2-3 active tasks in Progress. The reason why I say “2-3” is because these 2-3 tasks will work together so they must be tackled at similar times. Please see the Project Outline Card below this section to learn about more details of the tasks in progress.

Teammates & Contributors I’m Looking For

#Developers
Object Oriented Programming – I need an OOP experienced programmer to help me understand how OOP works

#Arduino – experts in WiFi connectivity and making HTTPS Requests **off the ‘Duino’

#LCD – experts in connecting to LCD

#C Programmers

#iOT People will also be extremely helpful for the overall assembly and execution of the Project

Understanding Mixpanel

www.mixpanel.com

Mixpanel is an extremely powerful data analysis tool for tech companies, specifically companies with web based/app based software, like Collaborizm 😃.

All you really need to know for this project are some basic things:

  • Mixpanel is passed EVENTS from whatever web/app you have – so for example on Collaborizm, one event we track, is “REPLY”.

  • You can track any event you want in your Projects

  • EVENTS are attached to EVENT PROPERTIES – like user city, device, country – eg. a REPLY event might tell us if the user was on an Android, in Mumbai, India when that REPLY event was executed.

For this Project that’s really all you need to understand. I will supply more information on mixpanel, and mixpanels API in Supporting Material Section below.

Github

https://github.com/aharshac/Collaborizm_Mixpanel_IoT

Why I’m Passionate About This Project

I am passionate about this Project because I am obsessed with the Internet of Things, Hardware, and Arduino, as well as data analysis.

Current Code File: Just a draft…to organize the different sections

Sections in Arduino File

Section 1: Import libraries
Section 2: Initialize Wifi Module and Connect to network, Send Proper Request to Mixpanel API
Section 3: Retrieve Request, Process Request, Store properly
Section 4: LCD

// Libraries Imported ------------------------------------------------------

~span~class~_1bcRJhiNTH8jh88VODldlE~#include~span~ <WiFi.h>
~span~class~_1bcRJhiNTH8jh88VODldlE~#include~span~ <LiquidCrystal.h>
~span~class~_1bcRJhiNTH8jh88VODldlE~#include~span~ <ArduinoJson.h>

// Define a New Class and Create Object ------------------------------------------------------

class Mixpanel
{
  // Class Member Variables
  // These are initialized at startup
  unsigned long eventName;      
  unsigned long eventCity;    

};


// Initialize and Connect to Wifi Network ------------------------------------------------------

char ssid[] = "WEWORK";     //  your network SSID (name) 
char pass[] = "[email protected]";    // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup() {
  // initialize serial:
  Serial.begin(9600);

  // attempt to connect using WPA2 encryption:
  Serial.println("Attempting to connect to WPA network...");
  status = WiFi.begin(ssid, pass);

  // if you're not connected, stop here:
  if ( status != WL_CONNECTED) { 
    Serial.println("Couldn't get a wifi connection");
    while(true);
  } 
  // if you are connected, print out info about the connection:
  else {
    Serial.println("Connected to the WEWORK network");
  }
}

void loop() {
  // do nothing
}

// Mixpanel API --> (1) Authenticate (2) Get Newest Event in this Critera Range on Mixpanel (3) Update mixpanelEventNow

// Search For Newest Reply Event --> Return Event Name, Country
MP.api.setCredentials(projectSecret)


// you are always sending your secret key with every request

//// sandbox


/*
  Repeating Wifi Web Client

 This sketch connects to a a web server and makes a request
 using an Arduino Wifi shield.

 Circuit:
 * WiFi shield attached to pins SPI pins and pin 7

 created 23 April 2012
 modified 31 May 2012
 by Tom Igoe
 modified 13 Jan 2014
 by Federico Vanzati

 http://www.arduino.cc/en/Tutorial/WifiWebClientRepeating
 This code is in the public domain.
 */

~span~class~_1bcRJhiNTH8jh88VODldlE~#include~span~ <SPI.h>
~span~class~_1bcRJhiNTH8jh88VODldlE~#include~span~ <WiFi.h>

char ssid[] = "yourNetwork";      //  your network SSID (name)
char pass[] = "secretPassword";   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

// Initialize the Wifi client library
WiFiClient client;

// server address:
char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
  // you're connected now, so print out the status:
  printWifiStatus();
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: ArduinoWiFi/1.1");
    client.println("Connection: close");
    client.println();

    /// 

    curl https://mixpanel.com/api/2.0/jql \
    -u SECRETGOESHERE: \
    --data-urlencode [email protected]_query.js
//

curl https://mixpanel.com/api/2.0/jql \
-u SECRETGOESHERE: \
--data-urlencode params='{"from_date":"2016-01-01", "to_date": "2016-01-07"}' \
--data-urlencode script='function main(){ return Events(params).groupBy(["name"], mixpanel.reducer.count()) }'



function main() {
  return Events({
    from_date: '2016-10-08',
    to_date:   '2016-12-22',
    event_selectors: [{event: 'reply'},{event: 'discussion'},{event: 'chat_msg'},{event: 'approved_join_request'},{event: 'denied_join_request'},{event: 'favorite'},{event: 'join_request'},{event: 'login'},{event: 'login_native'},{event: 'upvote'},]
  })
}

// Create Object of Class Mixpanel


create object 

method eventName = key from item in json array where time = greatest
method eventCity = key from item in json array where time = greatest

/// need to use json library for Arduino to somehow evaluate our goal which is set fucking arrayitem with biggest time stamps key values of Event Name/City to an object the lcd can print from



////

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}



////




// Set up LCD ------------------------------------------------------


LiquidCrystal lcd(2, 3, 8, 9, 10, 11); 

void setup()

{

lcd.begin(16, 2);

}

void loop()

{

lcd.setCursor(0, 0);

lcd.print("eventName");

lcd.setCursor(0, 1);

lcd.print("eventCity");

delay(750);

}





/// ***----- Resources -----***

// https://ictronic.wordpress.com/2016/07/02/interfacing-16x2-lcd-with-arduino-without-potentiometer/

// http://www.instructables.com/id/Connecting-an-LCD-to-the-Arduino/?ALLSTEPS

// https://github.com/bblanchon/ArduinoJson/wiki/Decoding-JSON

// 



Supporting Material & Info

LCD Connection

https://ictronic.wordpress.com/2016/07/02/interfacing-16x2-lcd-with-arduino-without-potentiometer/

http://www.instructables.com/id/Connecting-an-LCD-to-the-Arduino/?ALLSTEPS

Decoding a JSON Response on Arduino

https://github.com/bblanchon/ArduinoJson/wiki/Decoding-JSON

WiFi Connection & HTTPS Requests From Arduino

https://www.arduino.cc/en/Reference/WiFiClientConnect

www.mixpanel.com
https://mixpanel.com/help/

PROJECT OUTLINE & PROGRESS

Team Micro-Checklist

Completed List:

  • Circuit Diagram Completed: WiFi Module Added

  • Circuit Diagram Completed: Arduino UNO Board Added

  • Circuit Diagram Completed: LCD Connected

  • Initial attempt at Arduino Code completed

  • Circuit Diagram Completed: Power (9V) connected

In Progress List:

  • Must successfully connect to Mixpanel API and pull data FROM Arduino

  • Must successfully STORE the JSON response from Mixpanel API to Arduino

  • Must extract the most recent event and add it to an Object which will be used in the LCD function to continuously loop through this Object to display most recent event (and associated properties)

  • Github Documentation

  • Improving hardware

To Be Completed:

  • Debugged Script

  • Assemble Circuit

  • Film it in action

  • Add new ideas to pull from Mixpanel


Discuss Hacking The Mixpanel API with Arduino UNO


Related Projects

Hacking The Mixpanel API
Share
11
DISCOVER
CHAT
ALERTS
-2
DISCUSSIONS
-1
FEED