Monday, June 2, 2014

Grid Frequency Measurements streamed to Xively and Plot.ly

In an effort to measure the local Grid Frequency and ease the analysis of the data, I have been experimenting with some of the cloud-based graphing and plotting services. Pachube was the leader but has now become known as Xively,  another one I stumbled on is Plot.ly which I had used for just basic graphing and discovered they have streaming capabilities. I wanted to capture and plot my grid frequency data so I could look at it anytime via a browser. Both services work well and have benefits. I first used Xively and connected the Arduino via serial (USB) to a laptop running a python script. It would run for hours but then it would crash with a time-out error. It was a klunky setup and tied up a laptop. On both the Xively site and Plot.ly I had seen instructions on using Arduino's using a network interface. There was example code for both the basic Ethernet shield and Wifi shields and boards. A friend had a Seeed Ethernet Shield sold by Radio Shack so I asked to borrow it. The Plot.ly example code worked instantly and was easy to understand so I ran with Plot.ly over the weekend. I was amazed at the update rate. In the example code below it is reading two analog ports and updates every 5 seconds. For the grid monitoring I generated the graph at the top of the post with a 15 second rate. The good news is that these cloud plotting and graphing services are great, the bad news is that the my grid monitoring method is still problematic. I am now planning on trying the method used HERE. This method uses the zero crossings. He has a feed to Xively and he does not show any wild frequency swings like I am getting see his feed --> Desert Home Feed. I also want to measure home power usage via the IR pulse on my SmartMeter.



 #include <Ethernet.h>  
 #include <SPI.h>  
 #include "plotly_streaming_ethernet.h"  
 // Sign up to plotly here: https://plot.ly  
 // View your API key and streamtokens here: https://plot.ly/settings  
 #define nTraces 2  
 // View your tokens here: https://plot.ly/settings  
 // Supply as many tokens as data traces  
 // e.g. if you want to ploty A0 and A1 vs time, supply two tokens  
 char *tokens[nTraces] = {"token_1", "token_2"};  
 // arguments: username, api key, streaming token, filename  
 plotly graph = plotly("username", "apikey", tokens, "test", nTraces);  
 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
 byte my_ip[] = { 199, 168, 1, 177 }; // google will tell you: "public ip address"  
 void startEthernet(){  
   Serial.println("... Initializing ethernet");  
   if(Ethernet.begin(mac) == 0){  
     Serial.println("... Failed to configure Ethernet using DHCP");  
     // no point in carrying on, so do nothing forevermore:  
     // try to congifure using IP address instead of DHCP:  
     Ethernet.begin(mac, my_ip);  
   }  
   Serial.println("... Done initializing ethernet");  
   delay(1000);  
 }  
 void setup() {  
  // Open serial communications and wait for port to open:  
  Serial.begin(9600);  
  while (!Serial) {  
   ; // wait for serial port to connect. Needed for Leonardo only  
  }  
  startEthernet();  
  graph.maxpoints = 5000;  
  graph.fileopt="extend"; // See the "Usage" section in https://github.com/plotly/arduino-api for details  
  bool success;  
  success = graph.init();  
  if(!success){while(true){}}  
  graph.openStream();  
 }  
 unsigned long x;  
 int y;  
 void loop() {  
  graph.plot(millis(), analogRead(A0), tokens[0]);  
  graph.plot(millis(), analogRead(A1), tokens[1]);  
  delay(5000);  
 }  

No comments:

Post a Comment