Click the below link for our group foginator notebook for the overall process of the project from start to finish.
FOGINATOR GROUP NOTEBOOK
*Pictures along this process of the Foginator from start to finish can be seen on our project web portfolio.
My Foginator Updates/Notebook
My project notebook and work can be found below. It includes notes along the way for my personal part of the Foginator and then includes code for the sensor library which I created after testing the sensors functionality.
Getting the sensors working properly was a good start for the project. Test programs for each sensor were built and used to test the functionality of each sensor. The most challenging part was the next portion of the project which was to take the testing code and make one big sensor library where each sensor can be called with one line and output the data needed from each sensor. The code below is the .h header file, the .cpp and the getArduinoData final script for the sensor library. The code below is then used with the Arduino and sent via i2c communication to the Raspberry Pi, which is used to then call these sensors to update a web page created by Chad.
getArduinoData script
#include <Wire.h>
#include <SensorLibrary.h>
SensorLibrary foginator;
#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
int dataRead = 0;
void setup() {
//pinMode(13, OUTPUT);
Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
//Serial.println(“Ready!”);
}
void loop() {
delay(100);
}
// callback for received data
void receiveData(int byteCount){
while(Wire.available()) {
dataRead = Wire.read();
if (dataRead == 10) {
number = foginator.getWpTempData1();
Serial.print(number);
}
else if (dataRead == 11) {
number = foginator.getWpTempData2();
Serial.print(number);
}
else if (dataRead == 12) {
number = foginator.getCO2Data();
Serial.print(number);
}
else if (dataRead == 13) {
number = 22;
Serial.print(number);
}
else if (dataRead == 14) {
number = foginator.getPHData();
Serial.print(number);
}
else if (dataRead == 15) {
number = foginator.getLuxData();
Serial.print(number);
}
else if (dataRead == 16) {
number = foginator.getHumidityData();
Serial.print(number);
}
}
}
// callback for sending data
void sendData(){
Wire.write(number);
//Wire.endTransmission();
}
CPP File – Getting the Data we needed from the test scripts that were built
#include “Arduino.h”
#include “SensorLibrary.h”
#include “OneWire.h”
#include “DallasTemperature.h”
#include “SparkFunTSL2561.h”
#include “Wire.h”
#include “DHT.h”
SensorLibrary::SensorLibrary()
{
pinMode(A0,INPUT); // CO2 Sensor
}
// CO2 Sensor
int SensorLibrary::getCO2Data()
{
float CO2Curve[3] = {2.602,0.324,(0.020/(2.602-3))};
int percentage;
float volts;
volts = MGRead(A7);
percentage = MGGetPercentage(volts,CO2Curve);
return percentage;
}
float SensorLibrary::MGRead(int mg_pin)
{
int i;
float v=0;
for (i=0;i<5;i++) {
v += analogRead(mg_pin);
delay(50);
}
v = (v/5)*5/1024 ;
return v;
}
int SensorLibrary::MGGetPercentage(float volts, float *pcurve)
{
if ((volts/8.5)>=0.324) {
return -1;
} else {
return pow(10, ((volts/8.5)-pcurve[1])/pcurve[2]+pcurve[0]);
}
}
// * * * * * * * * * * * * * * * * * *
// Waterproof Temperature Sensor
float SensorLibrary::getWpTempData1()
{
OneWire oneWire(A5);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
sensors.requestTemperatures(); // Send the command to get temperatures
delay(50);
return sensors.getTempFByIndex(0);
}
float SensorLibrary::getWpTempData2()
{
OneWire oneWire(A6);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
sensors.requestTemperatures(); // Send the command to get temperatures
delay(50);
return sensors.getTempFByIndex(0);
}
// * * * * * * * * * * * * * * * * * *
// Lux Light Sensor
double SensorLibrary::getLuxData()
{
SFE_TSL2561 light;
unsigned char time = 2;
unsigned int data0, data1;
unsigned int ms = 1000;
boolean gain = 0;
light.begin();
light.setTiming(gain,time,ms);
light.setPowerUp();
//****
if (light.getData(data0,data1))
{
double lux; // Resulting lux value
boolean good; // True if neither sensor is saturated
// Perform lux calculation:
good = light.getLux(0,1000,data0,data1,lux);
return lux;
}
}
// * * * * * * * * * * * * * * * * * *
// Humidity Sensor
float SensorLibrary::getHumidityData()
{
DHT dht(A10, DHT22);
dht.begin();
float h = dht.readHumidity();
return h;
}
// pH Sensor
float SensorLibrary::getPHData()
{
float pHdata; //Store the average value of the sensor feedback
float pHValue,voltage;
pHdata=analogRead(A9);
voltage = pHdata*5.0/1024;
pHValue = 3.5*voltage+0.00;
return pHValue;
}
double SensorLibrary::avgArray(int* arr, int number)
{
int i;
int max,min;
double avg;
long amount=0;
if(number<=0){
Serial.println(“Error number for the array to avraging!/n”);
return 0;
}
if(number<5){ //less than 5, calculated directly statistics
for(i=0;i<number;i++){
amount+=arr[i];
}
avg = amount/number;
return avg;
}else{
if(arr[0]<arr[1]){
min = arr[0];max=arr[1];
}
else{
min=arr[1];max=arr[0];
}
for(i=2;i<number;i++){
if(arr[i]<min){
amount+=min; //arr<min
min=arr[i];
}else {
if(arr[i]>max){
amount+=max; //arr>max
max=arr[i];
}else{
amount+=arr[i]; //min<=arr<=max
}
}//if
}//for
avg = (double)amount/(number-2);
}//if
return avg;
}
// * * * * * * * * * * * * * * * * * *
Header File – Naming and setting the types for each sensor
#ifndef SensorLibrary_h
#define SensorLibrary_h
#include “Arduino.h”
class SensorLibrary
{
public:
SensorLibrary();
//CO2 Sensor functions
int getCO2Data();
float MGRead(int mg_pin);
int MGGetPercentage(float volts, float *pcurve);
float getWpTempData1();
float getWpTempData2();
double getLuxData();
float getHumidityData();
float getPHData();
double avgArray(int* arr, int number);
//
};
#endif
Recent Comments