         
     }
     
-    //Do transform the raw readings to squared current values
+    //transform the raw readings into averages and then calculate RMS current
     cnt = 0;
     RMScurrent = 0;
-    
-    
     while(cnt<2500){
-        
+
         //create a averaged point of 10 samples
+        RMSsamp = 0;
         cnt2 = 0;
-        avgsamp = 0;
-        while (cnt2<10){
-            avgsamp = avgsamp + (double) analogvals[cnt];
+        while (cnt2<n_avg && cnt < 2500){
+            //convert the raw readings to a voltage
+            voltage = (double)analogvals[cnt]*3.3/4095 - Vmid;
+            
+            //implement a 2nd order butterworth to filter the raw readings
+            filtered[0] = a[0]*voltage + a[1]*prev[0] + a[2]*prev[1] - b[0]*filtered[0] - b[1]*filtered[1];
+            prev[1] = prev[0];
+            prev[0] = voltage;
+            filtered[1] = filtered[0];
+            
+            //convert the filtered value and square it, add it to the sum of 10 RMS values
+            RMSsamp = RMSsamp + pow(filtered[0]*20,2);
             cnt = cnt + 1;
             cnt2 = cnt2 + 1;
         }
         
-        RMScurrent = RMScurrent + pow((3.3*avgsamp/40950-Vmid)*20,2);//convert to a current, square it, and sum
+        //average 10 RMS values
+        RMSsamp = RMSsamp/n_avg;
+        
+        //sum the squared and averaged samples
+        RMScurrent = RMScurrent + RMSsamp;
+        
     }
     
-    RMScurrent = sqrt(RMScurrent/250);//root mean of 2500/10 = 250 averaged samples
+    //find the total RMS current value of the entire data set
+    RMScurrent = sqrt(RMScurrent/(2500/n_avg));//root mean
     
-    sprintf(publishString, "%lf", RMScurrent);
-    Particle.publish("RMScurrent",publishString);
+    //publish the RMS wattage value with a linear calibration
+    sprintf(publishString, "%lf", 120*(1.44*RMScurrent-0.25));
+    Particle.publish("RMS wattage",publishString);
     delay(1000);
  
 }

#include "math.h"
int analogvals[2500];
int cnt,cnt2;
double Vmid = 1.61;
double avgsamp, RMScurrent;
char publishString[40];

void setup() {

    
    //very quickly sample the signal
    cnt = 0;
    while(cnt<2500){
        
        analogvals[cnt] = analogRead(A0); ///store current read to the array location cnt
