Wednesday, June 11, 2014

Weather Station: Day 16 - June 10

The following code is the arduino code that we have been using to read the temperature and the humidity from the DHT sensor:

#include <SPI.h>
#include "DHT.h"


// DHT Sensor Setup
#define DHTPIN 2 // We have connected the DHT to Digital Pin 2
#define DHTTYPE DHT22 // This is the type of DHT Sensor (Change it to
DHT11 if you're using that model)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT object


void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  dht.begin();
}

float h, t;

void loop() {
      h = dht.readHumidity();
      t = dht.readTemperature();
      Serial.print(h);
      Serial.print("\t");
      Serial.println(t);
      delay(10000);

We worked all day on the following code which goes in the Raspberry Pi terminal:

#!/usr/bin/python

import plotly.plotly as py
import time
import datetime
import serial
from plotly.graph_objs import Figure, Data,Scatter

#reads the data from the sensors and splits the line into the correct variables
def getpoint(ser):
        data = ser.readline().split()
        humidity = float(data[0])
        temp_C = float(data[1])

        # convert celsius to fahrenheit
        temperature = ( temp_C * 9.0 / 5.0 ) + 32

        date_stamp = datetime.datetime.now()

        return date_stamp,temperature,humidity

# token info
ser = serial.Serial('/dev/ttyACM0',9600)
py.sign_in('physuser','aldyw0r26q')
my_data1 = Data([Scatter(x=[],y=[], stream=dict(token='tvfuqv0s6g'))])
my_fig1=Figure(data=my_data1)
py.plot(my_fig1, auto_open = False)
s1 = py.Stream('tvfuqv0s6g')

my_data2 = Data([Scatter(x=[],y=[], stream=dict(token='bjo44dghec'))])
my_fig2=Figure(data=my_data2)
py.plot(my_fig2, auto_open = False)
s2 = py.Stream('bjo44dghec')


s1.open()
s2.open()
while True: #while loop for temperature
        pt = getpoint(ser)
        s1.write(dict(x=pt[0], y=pt[1] ))
        s2.write(dict(x=pt[0], y=pt[2]))
        time.sleep(5)

s1.close()
s2.close()


This python code sends two graphs to plotly. One graph is for the temperature and the other graph is for the humidity which are both measured by the DHT sensor in our circuit. Our goal is to get the two graphs to appear on the same graph that way we can use plotly to make subgraphs. 

We finally figured out how to get rid of the window that kept popping up when we ran the program. This was just a simple line of code auto_open=False. Yesterday we kept getting incorrect values for the humidity but we discovered that you have to open the serial monitor on the arduino in order for that value to be correct in python.

We also emailed the Matt Sundquist, the cofounder at plotly,  to ask about how to make the two traces appear on one graph and we are just waiting to hear back from him. 

Once we get this working, we will have to change the code to work for our weather station which includes temperature, humidity, rain, wind speed, wind direction, and gust.





No comments:

Post a Comment