Arduino Weather Station

This is what I do for fun. I am a nerd. And I love geeking out to this stuff. This is super technical. Not meant to be a full on tutorial. Just a recording of one of my projects incase it helps someone else.

I've been playing with my arduino. An arduino is an open source micro processor platform. It is a little computer that you can program. It is expandable by add-on shields. I'm using a Seeedstudio Ethernet shield while I wait for my ESP8622 wifi interface.

IMG_5859.jpg
IMG_5860.jpg

Here's the Code I'm running on the Arduino. It gets temperature & humidity from the DHT22, and TMP36, and then posts it to http://data.sparkfun.com.


/*
This reads the temp from a DHT22 for inside temp and a TMP36 for outside temp, and then posts those to phant.
 
 */

#include <SPI.h>
#include <EthernetV2_0.h>
#include <dht22.h>

/////////////////
// Phant Stuff //
/////////////////
const String publicKey = "JxDAyjDWXMCJWRX53yA4";
const String privateKey = "replace-with your private key";
const byte NUM_FIELDS = 5;
const String fieldNames[NUM_FIELDS] = {"inside_celsius", "inside_fahrenheit", "inside_humidity", "outside_celsius", "outside_fahrenheit"};
String fieldData[NUM_FIELDS];

//DHT22 Sensor Instance
dht22 DHT22;

//TMP36 Pin Variables
int outsideSensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures



/////////////////////
//Ethernet Settings//
/////////////////////
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
    0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
EthernetClient client;
#define W5200_CS  10
#define SDCARD_CS 4
char server[] = "data.sparkfun.com";    // name address for data.sparkFun (using DNS)



//Prototypes
void setupEthernet();
void postData();
void getOutsideTemp();
void getDHT();
void chill();


void setup() {
    //Attach to the DHT22
    DHT22.attach(2);
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    pinMode(SDCARD_CS,OUTPUT);
    
    //Setup Ethernet
    setupEthernet();
}

void loop() {
    

    //Get DHT22 Inside temp and Humidity
    getDHT();
    
    //Get Outside Temp from TMP36
    getOutsideTemp();
    
    //Post To Phant
    postData(); // the postData() function does all the work,
    // check it out below.
    
    chill();
    
}

void postData()
{
    Serial.println("========================data.sparkfun.com/ron_office=======================");
    Serial.print("Connecting to Phant Server. . .");
    // Make a TCP connection to remote host
    if (client.connect(server, 80))
    {
        Serial.println(" Connected!");
        // Post the data! Request should look a little something like:
        // GET /input/publicKey?private_key=privateKey&light=1024&switch=0&name=Jim HTTP/1.1\n
        // Host: data.sparkfun.com\n
        // Connection: close\n
        // \n
        Serial.print("Posting to Phant Server. . .");
        client.print("GET /input/");
        client.print(publicKey);
        client.print("?private_key=");
        client.print(privateKey);
        for (int i=0; i<NUM_FIELDS; i++)
        {
            client.print("&");
            client.print(fieldNames[i]);
            client.print("=");
            client.print(fieldData[i]);
        }
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.println(server);
        client.println("Connection: close");
        client.println();
    }
    else
    {
        Serial.println(F(" Connection failed"));
    }
    
    // Check for a response from the server, and route it
    // out the serial port.
    while (client.connected())
    {
        if ( client.available() )
        {
            char c = client.read();
            //Serial.print(c);
        }
    }
    client.stop();
    Serial.println(" Done!");
}


void setupEthernet(){
    pinMode(SDCARD_CS,OUTPUT);
    digitalWrite(SDCARD_CS,HIGH);//Deselect the SD card
    // this check is only needed on the Leonardo:
    while (!Serial) {
        ; // wait for serial port to connect. Needed for Leonardo only
    }
    
    // start the Ethernet connection:
    if (Ethernet.begin(mac) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        // no point in carrying on, so do nothing forevermore:
        for(;;)
            ;
    }
    // print your local IP address:
    Serial.print("My IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {
        // print the value of each byte of the IP address:
        Serial.print(Ethernet.localIP()[thisByte], DEC);
        Serial.print(".");
    }
    Serial.println();
    
}

void getDHT(){
    
    float hdegree, cdegree, fdegree;
    
    // Gather data:
    hdegree = (float)DHT22.humidity, DEC;
    cdegree = (float)DHT22.temperature, DEC ;
    fdegree = DHT22.fahrenheit(), DEC;
    
    //insert in array
    fieldData[0] = String(cdegree);
    fieldData[1] = String(fdegree);
    fieldData[2] = String(hdegree);
    
    //Print results to Serial
    Serial.println("========================INSIDE=======================");
    Serial.print("Humidity (%): ");
    Serial.println(hdegree);
    
    Serial.print("Temperature (°C): ");
    Serial.println(cdegree);
    
    Serial.print("Temperature (°F): ");
    Serial.println(fdegree);
    
    Serial.print("Temperature (°K): ");
    Serial.println(DHT22.kelvin(), DEC);
    
    Serial.print("Dew Point (°C): ");
    Serial.println(DHT22.dewPoint(), DEC);
    
    Serial.print("Dew PointFast (°C): ");
    Serial.println(cdegree);
    Serial.println();
}

void getOutsideTemp()
{
    //getting the voltage reading from the temperature sensor
    int reading = analogRead(outsideSensorPin);
    
    // converting that reading to voltage, for 3.3v arduino use 3.3
    float voltage = reading * 5.0;
    voltage /= 1024.0;
    
    // print out the voltage
    //Serial.print(voltage); Serial.println(" volts");
    
    // now print out the temperature
    Serial.println("========================OUTSIDE=======================");
    float outsideC = (voltage - 0.5) * 100 ;  //converting from 10 mv per degree wit 500 mV offset
    fieldData[3] = String(outsideC);
    //to degrees ((voltage - 500mV) times 100)
    Serial.print(outsideC); Serial.println(" degrees C");
    
    // now convert to Fahrenheit
    float outsideF = (outsideC * 9.0 / 5.0) + 32.0;
    fieldData[4] = String(outsideF);
    Serial.print(outsideF); Serial.println(" degrees F");
    Serial.println();
    
}

void chill(){
    Serial.print("Waiting to post ");
  //Delays for 15 min  
  for (int i=0; i<10; i++)
    {
    Serial.print(" .");  
      delay(90000); //90 seconds
    }
    Serial.println("Here we go!");
    Serial.println();
    
}

I got the DHT22 no-resistor code from https://www.virtuabotix.com/virtuabotix-dht22-pinout-coding-guide/
And the phant code was several different sources combined with my own stylizing. The Chart at the top is live data. That is using google charts, javascript, and jquery. You can see that code here https://jsfiddle.net/pastorhudson/o5Lmo0L4/

2 Comments

Signup to get updates when I post new content!

* indicates required