Realtime data using python

I managed to build a small python script as proof of concept.

I located the correct address for my sensor data through the Dash on the flukso website. If you are monitoring on minute, the flukso gets its data from the lokal network. In my web browser I retreived the correct link via pagesources(safari) for my flukso:

http://10.0.1.11:8080/sensor/345487e1fa2e73c5d6a166e4190da13a?callback=j...

This link works in a standard web browser, and outputs the last 60 stored values in the flukso.

jQuery17106542406435369124_1359410073538([[1359489910,265],[1359489911,267],[1359489912,267],[1359489913,268],[1359489914,267],[135 ...... 9489965,276],[1359489966,276],[1359489967,276],[1359489968,277],[1359489969,278]]);

I only need the last one. so i wrote the attached script to filter it out. There will be better ways to do it, please feel free to improve it.

  1. #!
  2. import urllib2
  3. import time
  4.  
  5. begin_teller = 0
  6. eind_teller = 0
  7. three_digit = 0
  8. var = 1
  9.  
  10. while var == 1: # infinite loop
  11.  time.sleep(2) # wait two seconds, don't need the data quicker.
  12.  for line in urllib2.urlopen('http://10.0.1.11:8080/sensor/365487e4fa2e73c5d6a166e4190da13a?callback=jQuery17106542406445369124_1359410073538&version=1.0&interval=minute&resolution=second&unit=watt&_=1359410093774'): # read the data from the flukso
  13.     begin_teller = len(line)-8 #start and end position for the wanted data
  14.     eind_teller = len(line)-4
  15.     three_digit = len(line)-7
  16.     if line[begin_teller:three_digit] == ",": # if the character in this position is a "," then we read 3 digits
  17.       print line[three_digit:eind_teller]
  18.     else: # else read four digits
  19.       print line[begin_teller:eind_teller]
  20.  
  21. print "good Night" # end of loop

Hope is is useful, next i will try to get the data using my arduino.

bramcloet's picture

This is how I did it on my arduino.
probably there are better ways also.

  1. // Flukso settings
  2. //////////////////
  3. byte flukso[] = { 192,168,0,233 }; //ip Address of the server you will connect to
  4. //The location to go to on the server
  5. //make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
  6. const String location = "/sensor/a52ac12e2d3a780f1c7ca3cd66206cee?version=1.0&interval=minute&unit=watt HTTP/1.0";
  7. EthernetClient client_flukso;
  8. char inString[32]; // string for incoming serial data
  9. int stringPos = 0; // string index counter
  10. String pageValue="";
  11. String power="";
  12. String tijd="";
  13. time_t a_tijd=0;
  14. int a_power=0;
  15. const int timezone = 1; //wintertijd 1; zomertijd=2
  16.  
  17.  
  18. // Read Power
  19. /////////////
  20.  
  21. String connectAndRead(){
  22.   //connect to the server
  23.  
  24.   Serial.println("connecting to flukso...");
  25.  
  26.   //port 80 is typical of a www page
  27.   if (client_flukso.connect(flukso, 8080)) {
  28.    // Serial.println("connected to flukso");
  29.     client_flukso.print("GET ");
  30.     client_flukso.println(location);
  31.     client_flukso.println();
  32.  
  33.     //Connected - Read the page
  34.     return readPage(); //go and read the output
  35.  
  36.   }else{
  37.     return "connection to flukso failed";
  38.   }
  39.  
  40. }
  41.  
  42. String readPage(){
  43.   //read the page, and capture & return everything between '<' and '>'
  44. int commaCount = 0;  // counter for comma's in flukso matrix
  45.   stringPos = 0;
  46.   memset( &inString, 0, 32 ); //clear inString memory
  47.  
  48.   while(true){
  49.  
  50.     if (client_flukso.available()) {
  51.       char c = client_flukso.read();
  52. //Serial.print(c);
  53.       if (c == '[' ) { //'[' is our character to count
  54.       commaCount = commaCount + 1;
  55.        }else if(commaCount == 61){//Ready to start reading the part
  56.  
  57.         if(c != ']'){ //']' is our ending character
  58.           inString[stringPos] = c;
  59.           stringPos ++;
  60.         }else{
  61.           //got what we need here! We can disconnect now
  62.           //Serial.println(inString);
  63.           client_flukso.stop();
  64.           client_flukso.flush();
  65.           Serial.println("disconnecting from flukso.");
  66.           return inString;
  67.  
  68.         }
  69.  
  70.       }
  71.     }
  72.  
  73.   }
  74.  
  75. }
  76.  
  77. int readPower(){
  78.   pageValue = connectAndRead(); //connect to the flukso and read the output
  79.   power = pageValue.substring(11);
  80.   a_power = 0;
  81.   for(int i=0;i<power.length();i++){
  82.     a_power = (10*a_power)+(power[i]-'0');
  83.   }
  84.   return a_power;
  85.   //tijd = pageValue.substring(0,10);
  86.   //a_tijd = 0;
  87.   //for(int i=0;i<tijd.length();i++){
  88.   //  a_tijd = (10*a_tijd)+(tijd[i]-'0');
  89.   //}
  90.   //a_tijd = a_tijd+timezone*60*60;
  91.   //return a_tijd;
  92. }

michi's picture

Thanks for that, I'll have a tinker!

Michi.

icarus75's picture

The FLM advertises its enabled sensors to the local network via the Bonjour mDNS protocol. While this info cannot (yet) be used in a browser context, you should be able to pick this up using a scripting language.

michi's picture

Thanks for the link! I've never used Bonjour before. I assume someone has written a Python module to resolve addresses via Bonjour? Anyway, I'm sure I'll find out soon enough :)

Cheers,

Michi.

icarus75's picture

First hit on Google: pybonjour provides a pure-Python interface to Apple Bonjour and compatible DNS-SD libraries (such as Avahi).

The FLM uses the Avahi implementation of the Bonjour protocol.

michi's picture

I just tried this. Works like a charm :-)

Also found a useful Bonjour browser via the Wikipedia page: http://www.tildesoft.com

That one is for OS X, but there are similar programs for Windows and Linux around too.

Michi.

icarus75's picture

I've experimented with DNSSD as well. Would be great to auto-detect the available sensors in the LAN and be able to access them in a browser context. That would make the real-time charting more robust. Pity that none of the browsers natively supports a JS API. You can upvote the feature requests on FF and chrome though.