Anonymous avatar Anonymous committed fd20e8c

- initial commit

Comments (0)

Files changed (2)

power_display/power_display.pde

+
+
+#include <NewSoftSerial.h>
+#include <ST7735.h>
+#include <SPI.h>
+
+#define cs 10   // for MEGAs you probably want this to be pin 53
+#define dc 9
+#define rst 8  // you can also connect this to the Arduino reset
+
+// Color definitions
+#define	BLACK           0x0000
+#define	BLUE            0x001F
+#define	RED             0xF800
+#define	GREEN           0x07E0
+#define CYAN            0x07FF
+#define MAGENTA         0xF81F
+#define YELLOW          0xFFE0  
+#define WHITE           0xFFFF
+
+ST7735 tft = ST7735(cs, dc, rst);    
+
+const uint32_t GRAPH_INTERVAL = 600L * 1000L;
+const float MAX_POWER = 15.0;
+const uint16_t GRAPH_LENGTH = 160;
+const uint16_t GRAPH_HEIGHT = 128;
+
+NewSoftSerial mySerial(2, 3, false);
+
+String line = "";
+
+uint8_t powerBuffer[GRAPH_LENGTH];
+uint16_t pbHead = GRAPH_LENGTH-1;
+uint16_t pbTail = 0;
+
+
+void setup()  
+{
+  Serial.begin(57600);
+  
+  for (uint16_t i = 0; i < GRAPH_LENGTH; i++)
+    powerBuffer[i] = 10;
+   
+
+  // set the data rate for the NewSoftSerial port
+  mySerial.begin(57600);
+  
+  tft.initR(); 
+  tft.writecommand(ST7735_DISPON);
+  tft.fillScreen(BLACK);
+
+}
+
+
+
+
+/*------------------------------------------------------------------------------------------*/
+void loop()                     
+{
+  
+  static char buf[40];
+  static float powerSum = 0.0;
+  static uint16_t ct = 0;
+  static uint32_t lastGraphTime = 0;
+  
+  if (mySerial.available()) {
+    char c = mySerial.read() & 0x7F;
+  
+    if (c == 0x0A) {
+
+      line.toCharArray(buf, 39);
+      tft.fillRect(0,0, tft.width, 8, BLACK);
+      tft.drawString(0, 0, buf, WHITE, 1);  
+      Serial.println(buf);
+      
+      line.substring(0, line.indexOf(" ")).toCharArray(buf, 39);
+      float v = atof(buf);
+      
+      if (v < 30) {
+        powerSum += v;
+        ct++;
+      }
+      
+      line = "";
+      
+    } else {
+      
+      if (c != 0x0D)
+        line += c;  
+   
+    } 
+  }
+  
+  // plot the next item on the graph
+  if (millis() > lastGraphTime + GRAPH_INTERVAL) {
+   
+    float avgUse = powerSum / ct; 
+    powerSum = 0;
+    ct = 0;
+    
+    powerBuffer[pbHead] = min(GRAPH_HEIGHT * avgUse / MAX_POWER, GRAPH_HEIGHT);
+    pbHead = ++pbHead % GRAPH_LENGTH;
+    
+    if (pbHead == pbTail)
+      pbTail = ++pbTail % GRAPH_LENGTH;
+      
+    uint16_t dataPoints = (pbHead - pbTail + GRAPH_LENGTH) % GRAPH_LENGTH;
+    
+    tft.fillScreen(BLACK);
+    
+    Serial.print(pbTail);
+    Serial.print(" ");
+  
+    Serial.print(pbHead);
+    Serial.print(" ");
+  
+    Serial.print(dataPoints);
+    Serial.println(" ");
+    
+    for (uint16_t i = pbTail; i < pbTail+dataPoints-1; i++) {
+      
+      uint16_t y0 = i - pbTail;
+      uint16_t y1 = y0 + 1;
+      
+     
+      uint16_t x0 = powerBuffer[i % GRAPH_LENGTH];
+      uint16_t x1 = powerBuffer[(i+1) % GRAPH_LENGTH];
+      
+      Serial.print(i);
+      Serial.print(" ");      
+      Serial.print(y0);
+      Serial.print(" ");      
+      Serial.print(x0);
+      Serial.print(" ");      
+      Serial.print(y1);
+      Serial.print(" ");      
+      Serial.print(x1);
+      Serial.print(" | "); 
+      
+      Serial.print(i % GRAPH_LENGTH);
+      Serial.print(" ");      
+      Serial.print((i+1) % GRAPH_LENGTH);
+      Serial.print(" ");        
+      
+      tft.drawLine(x0,y0, x1,y1, WHITE);  
+      
+      Serial.println(" ");      
+   
+    }
+    
+    
+    lastGraphTime = millis();
+
+  }
+  
+  
+
+}
+

power_sensor/power_sensor.pde

+
+// send a report every this many milliseconds
+const uint32_t REPORT_INTERVAL = 2000L;
+
+// number of pulses in a kWh
+const uint32_t PULSES_PER_KWH = 10000L;
+
+// total pulses since last report
+volatile uint32_t pulseCt = 0;
+
+// total interval since last report
+volatile uint32_t pulseIntervalSum = 0;
+
+// time of the last pulse in microseconds
+volatile uint32_t lastPulseTime = 0;
+
+// time the last data was reported (milliseconds)
+uint32_t lastReport = 0;
+
+// counts total pulses seen - used to get total power meaured
+uint32_t culmulativePulses = 0;
+
+/*-----------------------------------------------------------------*/
+void setup() {
+  
+   Serial.begin(57600);
+   Serial.print(255);
+   
+   // flash built-in LED on pin 13 to show when data is reported
+   pinMode(13, OUTPUT);
+   
+   // attach the ISR to interrupt 0, pin 2
+   attachInterrupt(0, countLEDPulses, RISING);
+   
+}
+
+
+
+/*-----------------------------------------------------------------*/
+void loop() {
+  
+  uint32_t t = millis();
+
+  // loop until report interval has elapsed
+  if (t > lastReport + REPORT_INTERVAL) {
+       
+    lastReport = t;
+    
+    // turn off interrupts so that the calculatons are atomic
+    cli();
+    
+    float avgPulseInterval = pulseIntervalSum  / (float(pulseCt) * 1000000);
+    culmulativePulses += pulseCt;
+    
+    pulseIntervalSum = 0;
+    pulseCt = 0;
+    
+    // interrupts back on again
+    sei();
+ 
+    // calculate the current power and accumulated use
+    float kiloWatts = 3600 / (avgPulseInterval * PULSES_PER_KWH);
+    float kWh = culmulativePulses / float(PULSES_PER_KWH);
+   
+    // sent data
+    Serial.print(kiloWatts);
+    Serial.print(" ");
+    Serial.println(kWh);
+    
+    // flash the LED
+    digitalWrite(13, HIGH);
+    delay(50);
+    digitalWrite(13, LOW);
+    
+  } 
+   
+}
+
+
+/*-----------------------------------------------------------------*/
+void countLEDPulses() {
+
+  // interrupt service routine
+  
+  uint32_t pulseTime = micros();
+
+  // ignore pulses where the time wraps around
+  if (lastPulseTime < pulseTime) {
+    
+    // accumulate the number of pulses and the time since the previous one
+    pulseIntervalSum += pulseTime - lastPulseTime;
+    pulseCt++;
+    
+  }  
+  
+  lastPulseTime = pulseTime;
+  
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.