Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions Clouduino/Clouduino2_2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#include <Wire.h>
#include <OneWire.h>

// #define DHTPIN A0
// #define DHTTYPE DHT22
// DHT dht(DHTPIN, DHTTYPE);

int heatPin = 12; // Heater connected to digital pin 12
int rainPin1 = 10; // Rain sensor 2 to digital pin 11
int rainPin2 = 11; // Rain sensor 1 to digital pin 10

int sleepcount = 1; // sleep time between sensor checks, 0.1 sec per int
int numReadings = 300;
int readings[300]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average

float status1 = 0; // Rain Sensor status
float status2 = 0;
int rainStatus = 0; //Reported rain value
int heatStatus = 0; // Binary heater status

int rainCheck = 0;
float rainCount = 0; // Track positive rain checks
float rainCountTotal = 0; // Tracks number of rain checks




void setup(void) {
Serial.begin(9600);
pinMode(rainPin1, INPUT_PULLUP); //Require a pull-up voltage for a positive measure on rain
pinMode(rainPin2, INPUT_PULLUP);
pinMode(heatPin, OUTPUT);
Serial.println("Pins attached");
}

int heatOn(){
if (digitalRead(heatPin) == HIGH){
heatStatus = 1;
//Serial.println("heat=1");
}
else{
digitalWrite(heatPin, HIGH);
heatStatus = 1;
//Serial.println("heat=1");
}
return 1;
}

int heatOff(){
if (digitalRead(heatPin) == LOW){
//Serial.println("heat=0");
heatStatus = 0;
}
else{
digitalWrite(heatPin, LOW);
heatStatus = 0;
//Serial.println("heat=0");
}
return 0;
}

int checkHeat(){
if (digitalRead(heatPin) == LOW){
heatStatus = 0;
}
else if (digitalRead(heatPin) == HIGH){
heatStatus = 1;
}
else{
heatOff();
heatStatus = 0;
}
return heatStatus;
}
int checkRainSensor1(){
int status = digitalRead(rainPin1); //Read rain sensor pin
if (status == 0){ //If the voltage is zero, there was a positive rain reading
//Serial.println("rain1 = True");
return 1;
}
else{
//Serial.println("rain1 = False");
return 0;
}
}

int checkRainSensor2(){
int status = digitalRead(rainPin2);
if (status == 0){
//Serial.println("rain2 = True");
return 1;
}
else{
//Serial.println("rain2 = False");
return 0;
}
}

int checkRain(){
status1 = checkRainSensor1();
status2 = checkRainSensor2();
if (status1 == 1.0 || status2 == 1.0){
rainCheck = 100;
}
else{
rainCheck = 0;
}
total = total - readings[readIndex]; // subtract the last reading:
readings[readIndex] = rainCheck; // read from the sensor:
total = total + readings[readIndex]; // add the reading to the total:
readIndex = readIndex + 1; // advance to the next position in the array:

if (readIndex >= numReadings) { // if we're at the end of the array...
readIndex = 0; // ...wrap around to the beginning:
}

average = float(total) / float(numReadings); // calculate the average:
//Serial.print("rain=");
//Serial.print(average);
//delay(1); // delay in between reads for stability
return float(average);
}


void test(void){
Serial.println("test routine");
}

void loop(void) {
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'y':
test();
break;
case 'h':
heatOn();
break;
case 'l':
heatOff();
break;
case 's':
Serial.print("heat=");
Serial.print(checkHeat());
Serial.print(",rain=");
Serial.println(checkRain());
break;
}
}
checkRain();
//Serial.println(checkRain());
delay(100*sleepcount);
}
175 changes: 175 additions & 0 deletions Clouduino/clouduino_interface2_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#! /usr/bin/python

"""
<<<<<<< HEAD
clouduino_interface.py
=======
clouduino_interface2_1.py
>>>>>>> c5ed227b3ad99bb7342e4de9cc1c888fb4362c99

This program is designed to read in data from an arduino.
Specifically this is program interfaces to the arduino on the
cloud camera. Functionality includes reading the temperature,
humidity, and light levels.

TODO:
finish interface

Usage:

Options:


"""

__author__ = ["Joseph Huehnerhoff", "Matt Armstrong", "Andrew Wilkins"]
__license__ = "GPL"
__version__ = "0.1"
__maintainer__ = "Matt Armstrong"
__email__ = ""
__status__ = "Developement"

import serial
import datetime
import time
import os

class ClouduinoInterface():
def __init__(self):
self.ser = None
#self.serPort = '/dev/tty.usbmodem1421'
self.serPort = '/dev/ttyACM0'
self.savefile = os.getcwd()+'/logs/log.txt'

self.heatStatus = 0
self.heatLast = datetime.datetime.strptime("01012001-00:00:00", "%m%d%Y-%H:%M:%S")
self.heatThreshold = 30.0 #Minimum pi core temp to turn on heaters
self.heatDuration = 10 #Duration (in mins) to run heaters

self.rainStatus = 0
self.rainThreshold = 40 #percent rain detects in last 30 seconds
self.rainLast = datetime.datetime.strptime("01012001-00:00:00", "%m%d%Y-%H:%M:%S")
self.rain10m = False

self.delay = 4.0

def readSer(self):
"""
Check the serial port for data
and write any data with a timestamp
to the savefile
"""
data = self.ser.readline().rstrip("\n").rstrip("\r")
f = open(self.savefile, 'a')
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H:%M:%S")
returnData = str(data)+",timestamp="+timestamp
f.write(returnData)
f.close()
return returnData

def openPort(self):
""" open the serial port for communication with the arduino"""
self.ser=serial.Serial(self.serPort, 9600)
return

def closePort(self):
""" close the serial port connection to the arduino"""
self.ser.close()
return

def checkStatus(self):
self.ser.write(b's')
time.sleep(0.1)
data = ''
data = self.readSer()
sortedDat = {'1':1}
#print data
if data.startswith('heat=') == True:
sortedDat = self.sortOutput(data)
if len(sortedDat) == 3:
self.rainStatus = int(sortedDat['rain'])
self.heatStatus = int(sortedDat['heat'])
sortedDat['rain10m'] = str(self.rainCheck())
#sortedDat['coretemp'] = int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3
#sortedDat = checkHeat(sortedDat)
print sortedDat
return sortedDat

def checkHeat(self, statusDict):
coreTemp = statusDict['coretemp']
if coreTemp >= self.heatThreshold:
self.heatLast = datetime.datetime.now() #will keep heaters running until 10min after heat rises above threshold
heatCheck = datetime.datetime.now() - self.heatLast
heatCheckmins = timeCheck.total_seconds() / 60
#print timeCheckmins
if timeCheckmins <= self.heatDuration:
self.heatOn()
statusDict['heat'] = 1
print "heaters on"
else:
self.heatOff()
statusDict['heat'] = 0
print "heaters off"
return statusDict

def heatOn(self):
self.ser.write(b'h')
self.heatStatus = 1
return

def heatOff(self):
self.ser.write(b'l')
self.heatStatus = 0
return

def sortOutput(self, serDat = None):
sortedDat = {}
#print serDat
#rawDat = serDat.strip('\r\n')
#print rawDat
sortedDat = dict(x.split('=') for x in serDat.split(','))
return sortedDat


def rainCheck(self):
print self.rainStatus
if self.rainStatus >= self.rainThreshold:
self.rainLast = datetime.datetime.now()
#print "Rain Detected"
timeCheck = datetime.datetime.now() - self.rainLast
timeCheckmins = timeCheck.total_seconds() / 60
print timeCheckmins
if timeCheckmins <= 10:
self.rain10m = True
else:
self.rain10m = False
#"rain10m = "+str(self.rain10m)
return self.rain10m


def serOut(self, status, filename):
directory = '/var/www/html/'+filename+'.txt'
f_out = open(directory,'w')
#print str(directory)
for key in status:
f_out.write(str(key)+"="+str(status[key])+'\n')
f_out.close()

if __name__ == "__main__":
c = ClouduinoInterface()
run = True
c.openPort()
time.sleep(2)
print "Port open"
while run == True:
#print "in loop"
statusDict = c.checkStatus()
#print len(statusDict)
if len(statusDict) == 4:
c.serOut(statusDict, 'testlog')
time.sleep(c.delay)

else:
time.sleep(0.1)

#c.closePort()
57 changes: 57 additions & 0 deletions Clouduino/rainDisplay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/python

import sys
#sys.path.insert(0, '/home/matt/CloudCamera/Clouduino')
import datetime
import time
import os
import cgi
#from clouduino_interface2_2 import ClouduinoInterface

#ci = ClouduinoInterface()

statusDict = {}
with open("testlog.txt") as f:
for line in f:
(key, val) = line.split('=')
statusDict[str(key)] = str(val)

#rainStatus = ci.rain10m
#heatStatus = ci.heatStatus
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<meta http-equiv="cahce-control" content="no-cache">'
print '<meta http-equiv="refresh" content="30"'
print '<title>Cloudunio Sensor Status</title>'
print '</head>'
print ' '
print '<body>'
print '<body onload="updateImage()" bgcolor="black">'
print '<h2>'
print '<center><font color="blue">Apache Point Observatory Optical Cloud Camera</font></center>'
print '</h2>'
print ' '
print '<table border="0">'
print '<tr>'
print '<td><a href="latest.png"><img src="latest.png" width="400" name="latest"></a></td>'
print '<td><a href="latest.mp4" width="400" name="latestgif"><video width="400" height="400" controls>'
print ' <source src="latest.mp4" type="video/mp4"></a></td>'
print '<td><font color="white">rain = %s <br>' % str(statusDict['rain'])
if str(statusDict['rain10m']).rstrip('\r').rstrip('\n') == 'True':
print '<font color = "red">rain [in last 10 min] = %s</font><br>' % str(statusDict['rain10m'])
else:
print '<font color = "green">rain [in last 10 min] = %s</font><br>' % str(statusDict['rain10m'])
print 'heat = %s</td><br></font>' % str(statusDict['heat'])
print '</tr>'
print '</table>'
print ' '
print '<br><Br>'
print '<center>'
print '<font color="white">'
print 'Image will auto-refresh<br><br>'
print '</center>'
print '</font>'
print '</body>'
print '</html>'
#time.sleep(5)
1 change: 1 addition & 0 deletions Clouduino/templog.txt

Large diffs are not rendered by default.