Hi,
I'm new to programming hence my confusion.
I'm using the HIH-4030 relative humidity sensor with an Arduino Uno an Ethernet shield and feeding the data to Cosm.com. The values that the sensor is returning don't make sense. It's been ranging from 600-800. I found some sample code on a website that Sparkfun linked to where they used a fix temperature value to calculate the relative humidity. Below I've included that code in the Code 1 section.
In the section Code 2 below that's the code I'm currently using to stream data to Cosm. How can I combine the fixed temperature value from Code 1 to the code in Code 2? If someone would be willing to explain I'd greatly appreciate it.
Thanks for the help
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code 1
//From the bildr article http://bildr.org/2012/11/hih4030-arduino/
int HIH4030_Pin = A0; //analog pin 0
void setup(){
Serial.begin(9600);
}
void loop(){
//To properly caculate relative humidity, we need the temperature.
float temperature = 25; //replace with a thermometer reading if you have it
float relativeHumidity = getHumidity(temperature);
Serial.println(relativeHumidity);
delay(100); //just here to slow it down so you can read it
}
float getHumidity(float degreesCelsius){
//caculate relative humidity
float supplyVolt = 5.0;
// read the value from the sensor:
int HIH4030_Value = analogRead(HIH4030_Pin);
float voltage = HIH4030_Value/1023. * supplyVolt; // convert to voltage value
// convert the voltage to a relative humidity
// - the equation is derived from the HIH-4030/31 datasheet
// - it is not calibrated to your individual sensor
// Table 2 of the sheet shows the may deviate from this line
float sensorRH = 161.0 * voltage / supplyVolt - 25.8;
float trueRH = sensorRH / (1.0546 - 0.0026 * degreesCelsius); //temperature adjustment
return trueRH;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code 2
/**
- Cosm Arduino sensor client example.
- This sketch demonstrates connecting an Arduino to Cosm (https://cosm.com),
- using the new Arduino library to send and receive data.
- Requirements
-
- Arduino with Ethernet shield or Arduino Ethernet (board must use the
- Wiznet Ethernet chipset)
-
- Arduino software with version >= 1.0
-
- An account at Cosm (https://cosm.com)
- Optional
-
- An analog sensor connected to pin 2 (note we can still read a value from
- the pin without this)
- Created 8th January, 2013 using code written by Adrian McEwen with
- modifications by Sam Mulube
- Full tutorial available here: https://cosm.com/docs/quickstart/arduino.html
- This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Cosm.h>
#define API_KEY "key goes here" // your Cosm API key
#define FEED_ID ##### // your Cosm feed ID
// MAC address for your Ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = 2;
unsigned long lastConnectionTime = 0; // last time we connected to Cosm
const unsigned long connectionInterval = 15000; // delay between connecting to Cosm in milliseconds
// Initialize the Cosm library
// Define the string for our datastream ID
char sensorId[] = "sensor_reading";
CosmDatastream datastreams[] = {
CosmDatastream(sensorId, strlen(sensorId), DATASTREAM_FLOAT),
};
// Wrap the datastream into a feed
CosmFeed feed(FEED_ID, datastreams, 1 /* number of datastreams */);
EthernetClient client;
CosmClient cosmclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Cosm Sensor Client Example");
Serial.println("==========================");
Serial.println("Initializing network");
while (Ethernet.begin(mac) != 1) {
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
Serial.println("Network initialized");
Serial.println();
}
void loop() {
// main program loop
if (millis() - lastConnectionTime > connectionInterval) {
// read a value from the pin
int sensorValue = analogRead(sensorPin);
// send it to Cosm
sendData(sensorValue);
// read the datastream back from Cosm
getData();
// update connection time so we wait before connecting again
lastConnectionTime = millis();
}
}
// send the supplied value to Cosm, printing some debug information as we go
void sendData(int sensorValue) {
datastreams[0].setFloat(sensorValue);
Serial.print("Read sensor value ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading to Cosm");
int ret = cosmclient.put(feed, API_KEY);
Serial.print("PUT return code: ");
Serial.println(ret);
Serial.println();
}
// get the value of the datastream from Cosm, printing out the value we received
void getData() {
Serial.println("Reading data from Cosm");
int ret = cosmclient.get(feed, API_KEY);
Serial.print("GET return code: ");
Serial.println(ret);
if (ret > 0) {
Serial.print("Datastream is: ");
Serial.println(feed[0]);
Serial.print("Sensor value is: ");
Serial.println(feed[0].getFloat());
}
Serial.println();
}