1. John McCombs
  2. sensor_net

Commits

John McCombs  committed d37e746 Draft

- alpha1 of sensor nodes

  • Participants
  • Branches default

Comments (0)

Files changed (2)

File stn_kitchen/stn_kitchen.ino

View file
  • Ignore whitespace
+
+#include <OneWire.h>
+#include <DallasTemperature.h>
+
+#include <Wire.h>
+#include <BMP085.h>
+
+// station_id
+char stationName[] = "k";
+
+const int pinLED = 7;
+
+// temp sensor data pin plugged into port 2 on the Arduino
+#define ONE_WIRE_BUS 2
+
+// OneWire bus object
+OneWire oneWire(ONE_WIRE_BUS);
+
+// Dallas Temperature object
+DallasTemperature ds18B20(&oneWire);
+
+
+BMP085 dps;
+
+uint8_t reqRecd = false;
+
+
+/* --------------------------------------------------------------------- */
+void setup(void)
+{
+  
+  // the LEDs shows whne the station is read
+  pinMode(pinLED, OUTPUT);
+  digitalWrite(pinLED, LOW);
+  
+  // start serial port
+  Serial.begin(57600);
+
+  // start temp sensor
+  ds18B20.begin();
+  
+  // start pressure sensor
+  dps.begin();
+}
+
+
+
+void loop(void)
+{ 
+
+
+  
+  if (Serial.find(stationName)) {
+    digitalWrite(pinLED, HIGH);
+    sendData();
+    digitalWrite(pinLED, LOW);
+  }
+
+}
+
+
+
+
+
+void sendData() {
+
+  // Send the command to get temperatures
+  ds18B20.requestTemperatures(); 
+
+  Serial.print("{\"stn\": \"");
+  Serial.print(stationName);
+  Serial.print("\",");
+
+  Serial.print(" \"t1\" : ");  
+  Serial.print(ds18B20.getTempCByIndex(0));  
+
+  Serial.print(", \"p1\" : ");  
+  Serial.print(1.0*dps.readPressure()/100); 
+  
+  Serial.println("}");
+  
+}

File stn_outdoor/stn_outdoor.ino

View file
  • Ignore whitespace
+
+#include <OneWire.h>
+#include <DallasTemperature.h>
+
+#include <SHT1x.h>
+
+#define ULONG_MAX 4294967295
+
+// station_id
+char stationName[] = "o";
+
+// LED shows when data is requested/sent
+const int pinDataLED = 8;
+const int pinPowerDayLED = 5;
+const int pinPowerNightLED = 4;
+
+/* --------------------------------------------------------------------------------------- */
+// temp sensor data pin plugged into port 2 on the Arduino
+#define ONE_WIRE_BUS 9
+
+// OneWire bus object
+OneWire oneWire(ONE_WIRE_BUS);
+
+// init Dallas Temperature. 
+DallasTemperature ds18B20(&oneWire);
+
+/* --------------------------------------------------------------------------------------- */
+// specify data and clock connections and instantiate SHT1x object
+#define dataPin  10
+#define clockPin 7
+SHT1x sht1x(dataPin, clockPin);
+
+
+/* --------------------------------------------------------------------------------------- */
+// declarations for lectricity use monitoring
+
+// number of pulses in a kWh - day meter
+const uint32_t PULSES_PER_KWH_DAY = 10000.0;
+
+// number of pulses in a kWh - night meter
+const uint32_t PULSES_PER_KWH_NIGHT = 1600.0;
+
+// total pulses since last report
+volatile uint32_t pulseCt[2] = {};
+uint32_t lastPulseCt[2] = {};
+
+// time the last data was reported (milliseconds)
+uint32_t lastReport;
+
+// indices to the counter arrays
+const int idx_day = 0;
+const int idx_night = 1;
+
+
+
+/* --------------------------------------------------------------------- */
+void setup(void)
+{
+  
+  // the LEDs shows when the station is read
+  pinMode(pinDataLED, OUTPUT);
+  digitalWrite(pinDataLED, LOW);
+  
+  // the LEDs shows when the day power pulses happen
+  pinMode(pinPowerDayLED, OUTPUT);
+  digitalWrite(pinPowerDayLED, LOW);
+  
+  // the LEDs shows when the day power pulses happen
+  pinMode(pinPowerNightLED, OUTPUT);
+  digitalWrite(pinPowerNightLED, LOW);
+
+  
+  // start serial port
+  Serial.begin(57600);
+
+  // start temp sensor
+  ds18B20.begin();
+  
+  // record start time
+  lastReport = millis();
+  
+  // attach the ISR to interrupt 0, pin 2 - DAY
+  attachInterrupt(0, countLEDPulsesDay, FALLING);
+ 
+  // attach the ISR to interrupt 0, pin 3 - NIGHT
+  attachInterrupt(1, countLEDPulsesNight, FALLING);  
+  
+}
+
+
+/* --------------------------------------------------------------------------------------- */
+void loop(void)
+{ 
+
+  // search for data requests
+  if (Serial.find(stationName)) {
+    
+    digitalWrite(pinDataLED, HIGH);
+    sendData();
+    digitalWrite(pinDataLED, LOW);
+
+    lastPulseCt[idx_day] = pulseCt[idx_day];
+    lastPulseCt[idx_night] = pulseCt[idx_night];
+  }
+  
+  if (pulseCt[idx_day] > lastPulseCt[idx_day]+10) {
+    digitalWrite(pinPowerDayLED, HIGH);
+    delay(10);
+    digitalWrite(pinPowerDayLED, LOW);
+    lastPulseCt[idx_day] = pulseCt[idx_day];
+  }
+  
+  if (pulseCt[idx_night] > lastPulseCt[idx_night]+10) {
+    digitalWrite(pinPowerNightLED, HIGH);
+    delay(10);
+    digitalWrite(pinPowerNightLED, LOW);
+    lastPulseCt[idx_night] = pulseCt[idx_night];
+  }
+
+
+
+}
+
+
+
+
+/* --------------------------------------------------------------------------------------- */
+void sendData() {
+
+  // Send the command to get temperatures
+  ds18B20.requestTemperatures(); 
+  float t2 = ds18B20.getTempCByIndex(0);
+  
+  // humidity
+  float humidity = sht1x.readHumidity();
+  
+  // get the power use
+  uint32_t load_day, load_night;
+  calc_power(&load_day, &load_night);  
+
+
+  Serial.print("{\"stn\": \"");
+  Serial.print(stationName);
+  Serial.print("\",");
+
+  Serial.print(" \"t2\" : ");  
+  Serial.print(t2);  
+  Serial.flush();
+
+  Serial.print(", \"h1\" : ");  
+  Serial.print(humidity); 
+  Serial.flush();
+  
+  Serial.print(", \"eld\" : ");  
+  Serial.print(load_day); 
+  Serial.flush();
+
+  Serial.print(", \"eln\" : ");  
+  Serial.print(load_night); 
+  Serial.flush();
+
+  Serial.println("}");
+  
+}
+
+
+
+
+/*-----------------------------------------------------------------*/
+void calc_power(uint32_t *load_day, uint32_t *load_night) {
+  
+  // calculate the average power use since the last report
+  
+  uint32_t elapsed;
+  
+  // get the current time
+  uint32_t t = millis();
+
+  // turn off interrupts so that the calculatons are atomic
+  cli();
+  
+  // calcuate elapsed time, allowing for clock wrap-around
+  if (t > lastReport) 
+    elapsed = t - lastReport;
+  else
+    elapsed = t + (ULONG_MAX - lastReport);
+    
+  uint32_t p_day = pulseCt[idx_day];
+  uint32_t p_night = pulseCt[idx_night];
+  
+  pulseCt[idx_day] = 0;
+  pulseCt[idx_night] = 0;
+  
+  // interrupts back on again
+  sei();
+
+  lastReport = t;
+  
+  // calculate the current power in watts
+  *load_day =  (3600000.0/elapsed) * p_day / (PULSES_PER_KWH_DAY/1000.0);
+  *load_night =  (3600000.0/elapsed) * p_night / (PULSES_PER_KWH_NIGHT/1000.0);
+  
+}
+
+
+
+
+/*-----------------------------------------------------------------*/
+void countLEDPulsesDay() {
+
+  // interrupt service routine for day rate
+
+  pulseCt[idx_day]++;
+
+}
+
+
+/*-----------------------------------------------------------------*/
+void countLEDPulsesNight() {
+
+  // interrupt service routine for night rate 
+  
+  pulseCt[idx_night]++;
+
+}
+
+
+