eric maundu
Published © GPL3+

Jellyfish Smart Aquaponics - Adding Electronics and Sensors

In this second part for building jellyfish - we add the v2 controller and sensors the smart mini aquaponics garden.

IntermediateFull instructions provided6 hours999
Jellyfish Smart Aquaponics - Adding Electronics and Sensors

Things used in this project

Hardware components

kijani grows v2 smart controller
×1

Software apps and online services

Kijani Grows v2 controller API

Story

Read more

Schematics

v2 controller schematics and block diagrams

v2 controller circuit boards

Code

2560 atmega code

Arduino
/*
}
}
{
"initialize":0,
"baudRate":38400,
"version":"v2.0.0",
"atmegaUptime":"00:00:00:40",
"pins":{
        //"D2":0,
	int0 - flowrate
        "D3":0,
        "D4":0,
        "D5":0,
        "D6":0,
        "D7":0,
        "D8":0,
        "D9":0,
        "D22":0,
        "D23":0,
        "D25":1,
        //"D26":1,
	openwire26:xx,
        //"D27":1,
	dht27:yy.
        "D28":1,
        "D29":1,
        "D30":1,
        "D31":1,
        "D32":1,
        "D33":1,
        "D34":1,
        "D35":0,
        "D36":0,
        "D37":0,
        "D38":0,
        "D39":0,
        "D40":0,
        "D41":0,
        "D42":0,
        "D43":0,
        "D44":0,
        "D45":0,
        "D46":0,
        "D47":0,
        "D48":0,
        "D49":0,
        "A0":509.00,
        "A1":509.00,
        "A2":1023.00,
        "A3":1023.00,
        "A4":1023.00,
        "A5":1023.00,
        "A6":1023.00,
        "A7":1023.00,
        "A8":1023.00,
        "A9":1023.00,
        "A10":713.00,
        "A11":533.00,
        "A12":426.00,
        "A13":359.00,
        "A14":323.00,
        "A15":290.00,
        "UART1":0,
        "UART2":0,
        "UART3":0
	}
}


*/


/* Useful Constants and macros from DateTime.h */
#include <stdio.h>
#include <string.h>
//i2c lib for miniEc
#include <Wire.h> 
//We'll want to save calibration and configration information in EEPROM for miniec
#include <avr/eeprom.h>
#include <MCP3221.h>
#include <RTClib.h>

RTC_DS1307 RTC;

//EEPROM trigger check
#define ecWrite_Check      0x1234
#define phWrite_Check      0x1234
byte ecAddress = 0x4E; // MCP3221 A5 in Dec 77 A0 = 72 A7 = 79)
#define phAddress 0x4D // MCP3221 A5 in Dec 77 A0 = 72 A7 = 79)
#define I2C_TIMEOUT 5000

#define Version  ("\"v2.0.0\"")
#define SECS_PER_MIN  (60UL)
#define SECS_PER_HOUR (3600UL)
#define SECS_PER_DAY  (SECS_PER_HOUR * 24L)

/* Useful Macros for getting elapsed time */
#define numberOfSeconds(_time_) (_time_ % SECS_PER_MIN)  
#define numberOfMinutes(_time_) ((_time_ / SECS_PER_MIN) % SECS_PER_MIN)
#define numberOfHours(_time_) (( _time_% SECS_PER_DAY) / SECS_PER_HOUR)
#define elapsedDays(_time_) ( _time_ / SECS_PER_DAY)  

//Our parameter, for ease of use and eeprom access lets use a struct
struct parameters_phT
{
  unsigned int WriteCheck;
  int pH7Cal, pH4Cal;
  float pHStep;
}
phParams;

//Our parameter, for ease of use and eeprom access lets use a struct
struct parameters_ecT
{
  unsigned int WriteCheck;
  int eCLowCal, eCHighCal;
  float eCStep;
}
ecParams;

float eC, temperatute;
const int I2CadcVRef = 4948;
const int oscV = 185; //voltage of oscillator output after voltage divider in millivolts i.e 120mV (measured AC RMS) ideal output is about 180-230mV range   
const float kCell = 1.0; //set our Kcell constant basically our microsiemesn conversion 10-6 for 1 10-7 for 10 and 10-5 for .1
const float Rgain = 3000.0; //this is the measured value of the R9 resistor in ohms

float pH;
const float vRef = 4.096; //Our vRef into the ADC wont be exact
                    //Since you can run VCC lower than Vref its
                    //best to measure and adjust here
const float opampGain = 5.25; //what is our Op-Amps gain (stage 1)

MCP3221 i2cADC(ecAddress, I2CadcVRef);

//coriz co2
#include <cozir.h>
// <SoftwareSerial.h> is needed as the cozir lib uses it.
// even when only using HWserial
#include <SoftwareSerial.h>
// COZIR object
COZIR czr(&Serial1);



//uarts
const int RX1 = 19;
const int TX1 = 18;
const int RX2 = 17;
const int TX2 = 16;
const int RX3 = 15;
const int TX3 = 14;
//dio's
/*controls*/
const int RESET9331 = 24; //reset ar9331 module
/*specials*/
const int HALLSENSOR = 21;    //The pin location of the sensor
const int FLOWSWITCH1 = 30; //flow switch sensor

const int D13 = 13;
/*outputs*/
const int D4 = 4;
const int D10 = 10;
const int D11 = 11;
const int D12 = 12;
/*inputs*/
const int D5 = 5;
const int D2 = 2;
const int D3 = 3;
const int D6 = 6;
const int D7 = 7;
const int D8 = 8;
const int D9 = 9;
const int D49 = 49;
const int D48 = 48;
const int D47 = 47;
const int D46 = 46;
const int D45 = 45;
const int D44 = 44;
const int D43 = 43;
const int D42 = 42;
const int D41 = 41;
const int D40 = 40;
const int D39 = 39;
const int D38 = 38;
const int D37 = 37;
const int D36 = 36;
const int D35 = 35;
const int D34 = 34;
const int D33 = 33;
const int D32 = 32;
const int D31 = 31;
const int D30 = 30; //flow switch
const int D29 = 29;
const int D28 = 28;
//const int D27 = 27;
//const int D26 = 26;
const int D25 = 25;
const int D23 = 23;
const int D22 = 22;


boolean waterIsFlowing = 0; //is water flowing
//flow rate sensor variables
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;                               
int flowRate;


//analogue
/*
const int A15;
const int A14;
const int A13;
const int A12;
const int A11;
const int A10;
const int A9;
const int A8;
const int A7;
const int A6;
const int A5;
const int A4;
const int A3;
const int A2;
const int A1;
const int A0;
*/
//libraries


/*1-wire temp sensors*/
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 26
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
int numberOfDevices; //number of one wire temperature devices found
DeviceAddress tempDeviceAddress, nutrientTemp, roomTemp; // We'll use this variable to store a found device address

/*humidity sensor constants*/
#include "DHT.h"
#define DHTPIN 27     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 27  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

// the follow variables is a long because the time, measured in miliseconds, will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)
////int measureFlowRateInterval = 10; //measure flow rate once every number of seconds.
int measureFlowRateInterval = 1; //measure flow rate once every number of seconds.
long previousMillis = 0;        // will store last timer update

//FLOWSWITCH1 38
//HALLSENSOR 21
//dio pinouts
const int RELAY1 = 10;
const int RELAY2 = 11;
const int RELAY3 = 12;
const int RELAY4 = 4;

int relay1Status;
int relay2Status;
int relay3Status;
int relay4Status;

//program setup
int initialize;
//long baudRate = 115200;
long baudRate = 38400;


//This just simply applies defaults to the params incase the need to be reset or
//they have never been set before (!magicnum)
void reset_ecParams(void)
{
  //Restore to default set of parameters!
  ecParams.WriteCheck = ecWrite_Check;
  ecParams.eCLowCal = 200; //assume ideal probe and amp conditions 1/2 of 4096
  ecParams.eCHighCal = 1380; //using ideal probe slope we end up this many 12bit units away on the 4 scale
  eeprom_write_block(&ecParams, (void *)0, sizeof(ecParams)); //write these settings back to eeprom
}

//This just simply applys defaults to the params incase the need to be reset or
//they have never been set before (!magicnum)
void reset_phParams(void)
{
  //Restore to default set of parameters!
  phParams.WriteCheck = phWrite_Check;
  phParams.pH7Cal = 2048; //assume ideal probe and amp conditions 1/2 of 4096
  phParams.pH4Cal = 1286; //using ideal probe slope we end up this many 12bit units away on the 4 scale
  phParams.pHStep = 59.16;//ideal probe slope
  eeprom_write_block(&phParams, (void *)0, sizeof(phParams)); //write these settings back to eeprom
}

//used to visual indicate program wait state when booting. wait is for preventing console kernel overrides
//int flashCounter;
int flashLed(int flashCounter) {
	int ledcount = 0;
	int flashDelay = 500; // milliseconds
	//int flashCounter = 10;
	while (ledcount < flashCounter ){ //flashCounter times
		digitalWrite(D13, HIGH);   // turn the LED on (HIGH is the voltage level)
		delay(flashDelay);         // wait for a second
		digitalWrite(D13, LOW);    // turn the LED off by making the voltage LOW
		delay(flashDelay);         // wait for a second
		ledcount ++;
	}
}

/*pio hardware initialization*/
void setup() {                
	//delay(30000); //let 9331 boot to prevent hanging up
	pinMode(D4, OUTPUT);
	pinMode(D10, OUTPUT);
	pinMode(D11, OUTPUT);
	pinMode(D12, OUTPUT);
	pinMode(D13, OUTPUT);
	pinMode(RESET9331, OUTPUT); //reset wireless module module
	pinMode(D4, INPUT);
	pinMode(D5, INPUT);
	//pinMode(D2, INPUT);
	pinMode(D3, INPUT);
	pinMode(D6, INPUT_PULLUP);
	pinMode(D7, INPUT);
	pinMode(D8, INPUT_PULLUP);
	pinMode(D9, INPUT);
	pinMode(D49, INPUT);
	pinMode(D48, INPUT);
	pinMode(D47, INPUT_PULLUP);
	pinMode(D46, INPUT);
	pinMode(D45, INPUT_PULLUP);
	pinMode(D44, INPUT);
	pinMode(D43, INPUT_PULLUP);
	pinMode(D42, INPUT);
	pinMode(D41, INPUT_PULLUP);
	pinMode(D39, INPUT_PULLUP);
	pinMode(D38, INPUT_PULLUP);
	pinMode(D40, INPUT_PULLUP);
	pinMode(D37, INPUT_PULLUP);
	pinMode(D36, INPUT_PULLUP);
	pinMode(D35, INPUT_PULLUP);
	pinMode(D34, INPUT_PULLUP);
	pinMode(D33, INPUT_PULLUP);
	pinMode(D32, INPUT_PULLUP);
	pinMode(D31, INPUT);
	pinMode(D30, INPUT);
	pinMode(D29, INPUT);
	pinMode(D28, INPUT);
//	pinMode(D27, INPUT);
//	pinMode(D26, INPUT);
	pinMode(D25, INPUT_PULLUP);
	pinMode(D23, INPUT_PULLUP);
	pinMode(D22, INPUT);

	//int flashCounterTimes = 10;
	flashLed(10);	
	//initialize serial ports
	Serial.begin(baudRate); //linux

	// SETUP COZIR
	Serial1.begin(9600); //coriz co2
  	czr.init();
  	Serial.print("Cozir HardwareSerial: ");
  	Serial.println(COZIR_LIB_VERSION);

	//atlas specific sensors
	Serial2.begin(baudRate); //ph
	Serial3.begin(baudRate); //tds
	RTC.begin();
	Serial2.print("\r"); //sensors require return to start
	Serial3.print("\r");

	// Start up one wire temp library
	sensors.begin();
//	sensors.reset_search();
	numberOfDevices = sensors.getDeviceCount();
	  // report parasite power requirements
  	Serial.print("Parasite power is: "); 
  	if (sensors.isParasitePowerMode()) Serial.println("ON");
  	else Serial.println("OFF");

	if (!sensors.getAddress(nutrientTemp, 0)) Serial.println("Unable to find address for nutrient temperature"); 
  	if (!sensors.getAddress(roomTemp, 1)) Serial.println("Unable to find address for room temperature"); 

  Wire.begin(); //connects I2C
  //Lets read our Info from the eeprom and setup our params,
  //if we loose power or reset we'll still remember our settings!
  eeprom_read_block(&ecParams, (void *)0, sizeof(ecParams));
  //if its a first time setup or our magic number in eeprom is wrong reset to default
  if (ecParams.WriteCheck != ecWrite_Check){
    reset_ecParams();
  }

  eeprom_read_block(&ecParams, (void *)0, sizeof(ecParams));

	Serial.println(initialize = 1); 

  //if we loose power or reset we'll still remember our settings!
  eeprom_read_block(&phParams, (void *)0, sizeof(phParams));
  //if its a first time setup or our magic number in eeprom is wrong reset to default
  if (phParams.WriteCheck != phWrite_Check){
    reset_phParams();
  }
}

void getD2(){
    Serial.print("\t\"D2\":");
    int d2 = (digitalRead(D2)); 
    Serial.print(d2); 
    Serial.println(",");
}
void getD3(){
    Serial.print("\t\"D3\":");
    int d3 = (digitalRead(D3)); 
    Serial.print(d3); 
    Serial.println(",");
}
void getD4(){
    Serial.print("\t\"D4\":");
    int d4 = (digitalRead(D4)); 
    Serial.print(d4); 
    Serial.println(",");
}
void getD5(){
    Serial.print("\t\"D5\":");
    int d5 = (digitalRead(D5)); 
    Serial.print(d5); 
    Serial.println(",");
}
void getD6(){
    Serial.print("\t\"D6\":");
    int d6 = (digitalRead(D6)); 
    Serial.print(d6); 
    Serial.println(",");
}
void getD7(){
    Serial.print("\t\"D7\":");
    int d7 = (digitalRead(D7)); 
    Serial.print(d7); 
    Serial.println(",");
}
void getD8(){
    Serial.print("\t\"D8\":");
    int d8 = (digitalRead(D8)); 
    Serial.print(d8); 
    Serial.println(",");
}
void getD9(){
    Serial.print("\t\"D9\":");
    int d9 = (digitalRead(D9)); 
    Serial.print(d9); 
    Serial.println(",");
}
void getD22(){
    Serial.print("\t\"D22\":");
    int d22 = (digitalRead(D22)); 
    Serial.print(d22); 
    Serial.println(",");
}
void getD23(){
    Serial.print("\t\"D23\":");
    int d23 = (digitalRead(D23)); 
    Serial.print(d23); 
    Serial.println(",");
}
void getD25(){
    Serial.print("\t\"D25\":");
    int d25 = (digitalRead(D25)); 
    Serial.print(d25); 
    Serial.println(",");
}
/*
void getD26(){
    Serial.print("\t\"D26\":");
    int d26 = (digitalRead(D26)); 
    Serial.print(d26); 
    Serial.println(",");
}
void getD27(){
    Serial.print("\t\"D27\":");
    int d27 = (digitalRead(D27)); 
    Serial.print(d27); 
    Serial.println(",");
}
*/

void getD28(){
    Serial.print("\t\"D28\":");
    int d28 = (digitalRead(D28)); 
    Serial.print(d28); 
    Serial.println(",");
}
void getD29(){
    Serial.print("\t\"D29\":");
    int d29 = (digitalRead(D29)); 
    Serial.print(d29); 
    Serial.println(",");
}
void getD30(){
    Serial.print("\t\"D30\":");
    int d30 = (digitalRead(D30)); 
    Serial.print(d30); 
    Serial.println(",");
}
void getD31(){
    Serial.print("\t\"D31\":");
    int d31 = (digitalRead(D31)); 
    Serial.print(d31); 
    Serial.println(",");
}
void getD32(){
    Serial.print("\t\"D32\":");
    int d32 = (digitalRead(D32)); 
    Serial.print(d32); 
    Serial.println(",");
}
void getD33(){
    Serial.print("\t\"D33\":");
    int d33 = (digitalRead(D33)); 
    Serial.print(d33); 
    Serial.println(",");
}
void getD34(){
    Serial.print("\t\"D34\":");
    int d34 = (digitalRead(D34)); 
    Serial.print(d34); 
    Serial.println(",");
}
void getD35(){
    Serial.print("\t\"D35\":");
    int d35 = (digitalRead(D35)); 
    Serial.print(d35); 
    Serial.println(",");
}
void getD36(){
    Serial.print("\t\"D36\":");
    int d36 = (digitalRead(D36)); 
    Serial.print(d36); 
    Serial.println(",");
}
void getD37(){
    Serial.print("\t\"D37\":");
    int d37 = (digitalRead(D37));
    Serial.print(d37); 
    Serial.println(",");
}
void getD38(){
    Serial.print("\t\"D38\":");
    int d38 = (digitalRead(D38)); 
    Serial.print(d38); 
    Serial.println(",");
}
void getD39(){
    Serial.print("\t\"D39\":");
    int d39 = (digitalRead(D39)); 
    Serial.print(d39); 
    Serial.println(",");
}
void getD40(){
    Serial.print("\t\"D40\":");
    int d40 = (digitalRead(D40)); 
    Serial.print(d40); 
    Serial.println(",");
}
void getD41(){
    Serial.print("\t\"D41\":");
    int d41 = (digitalRead(D41)); 
    Serial.print(d41); 
    Serial.println(",");
}
void getD42(){
    Serial.print("\t\"D42\":");
    int d42 = (digitalRead(D42)); 
    Serial.print(d42); 
    Serial.println(",");
}
void getD43(){
    Serial.print("\t\"D43\":");
    int d43 = (digitalRead(D43)); 
    Serial.print(d43); 
    Serial.println(",");
}
void getD44(){
    Serial.print("\t\"D44\":");
    int d44 = (digitalRead(D44)); 
    Serial.print(d44); 
    Serial.println(",");
}
void getD45(){
    Serial.print("\t\"D45\":");
    int d45 = (digitalRead(D45)); 
    Serial.print(d45); 
    Serial.println(",");
}
void getD46(){
    Serial.print("\t\"D46\":");
    int d46 = (digitalRead(D46)); 
    Serial.print(d46); 
    Serial.println(",");
}
void getD47(){
    Serial.print("\t\"D47\":");
    int d47 = (digitalRead(D47)); 
    Serial.print(d47); 
    Serial.println(",");
}
void getD48(){
    Serial.print("\t\"D48\":");
    int d48 = (digitalRead(D48)); 
    Serial.print(d48); 
    Serial.println(",");
}
void getD49(){
    Serial.print("\t\"D49\":");
    int d49 = (digitalRead(D49)); 
    Serial.print(d49); 
    Serial.println(",");
}

//analogue
void getA0(){
	Serial.print("\t\"A0\":");
	float a0 = analogRead(A0); //gb level
	Serial.print(a0); 
	Serial.println(",");
}
void getA1(){
	Serial.print("\t\"A1\":");
	float a1 = analogRead(A1); //gb level
	Serial.print(a1); 
	Serial.println(",");
}
void getA2(){
	Serial.print("\t\"A2\":");
	float a2 = analogRead(A2); //gb level
	Serial.print(a2); 
	Serial.println(",");
}
void getA3(){
	Serial.print("\t\"A3\":");
	float a3  = analogRead(A3); //gb level
	Serial.print(a3); 
	Serial.println(",");
}
void getA4(){
	Serial.print("\t\"A4\":");
	float a4  = analogRead(A4); //gb level
	Serial.print(a4); 
	Serial.println(",");
}
void getA5(){
	Serial.print("\t\"A5\":");
	float a5  = analogRead(A5); //gb level
	Serial.print(a5); 
	Serial.println(",");
}
void getA6(){
	Serial.print("\t\"A6\":");
	float a6  = analogRead(A6); //gb level
	Serial.print(a6); 
	Serial.println(",");
}
void getA7(){
	Serial.print("\t\"A7\":");
	float a7  = analogRead(A7); //gb level
	Serial.print(a7); 
	Serial.println(",");
}
void getA8(){
	Serial.print("\t\"A8\":");
	float a8  = analogRead(A8); //gb level
	Serial.print(a8); 
	Serial.println(",");
}
void getA9(){
	Serial.print("\t\"A9\":");
	float a9  = analogRead(A9); //gb level
	Serial.print(a9); 
	Serial.println(",");
}
void getA10(){
	Serial.print("\t\"A10\":");
	float a10  = analogRead(A10); //gb level
	Serial.print(a10); 
	Serial.println(",");
}
void getA11(){
	Serial.print("\t\"A11\":");
	float a11  = analogRead(A11); //gb level
	Serial.print(a11); 
	Serial.println(",");
}
void getA12(){
	Serial.print("\t\"A12\":");
	float a12  = analogRead(A12); //gb level
	Serial.print(a12); 
	Serial.println(",");
}
void getA13(){
	Serial.print("\t\"A13\":");
	float a13  = analogRead(A13); //gb level
	Serial.print(a13); 
	Serial.println(",");
}
void getA14(){
	Serial.print("\t\"A14\":");
	float a14  = analogRead(A14); //gb level
	Serial.print(a14); 
	Serial.println(",");
}
void getA15(){
	Serial.print("\t\"A15\":");
	float a15  = analogRead(A15); //gb level
	Serial.print(a15); 
	Serial.println(",");
}
void getUART1(){
	Serial.print("\t\"UART1\":");
	char uart1[20];
	Serial.print(0); 
	Serial.println(",");
}
void getUART2(){
	Serial.print("\t\"UART2\":");
	char uart2[20];
	Serial.print(0); 
	Serial.println(",");
}
void getUART3(){
	Serial.print("\t\"UART3\":");
	char uart3[20];
	Serial.print(0); 
	Serial.println(",");
}

 
    
int turnRelay1on(){
 //turn relay1 on
 digitalWrite(RELAY1, HIGH);
 relay1Status = 1;
}
int turnRelay1off(){
 //turn relay1 off
 digitalWrite(RELAY1, LOW);
 relay1Status = 0;
}
int turnRelay2on(){
 //turn relay2 on
 digitalWrite(RELAY2, HIGH);
 relay2Status = 1;
}
int turnRelay2off(){
 //turn relay2 off
 digitalWrite(RELAY2, LOW);
 relay2Status = 0;
}
int turnRelay3on(){
 //turn relay3 on
 digitalWrite(RELAY3, HIGH);
 relay3Status = 1;
}
int turnRelay3off(){
 //turn relay4 off
 digitalWrite(RELAY3, LOW);
 relay3Status = 0;
}
int turnRelay4on(){
 //turn relay4 on
 digitalWrite(RELAY4, HIGH);
 relay4Status = 1;
}
int turnRelay4off(){
 //turn relay4 off
 digitalWrite(RELAY4, LOW);
 relay4Status = 0;
}

void mydelay(unsigned long ms)
{
     //does not use delay due to interupts
     if (ms > 20) {
       unsigned long start = millis();

       while (millis() - start <= ms) ;
     } else delayMicroseconds((unsigned int)ms * 1000);
} 

void getUptime (unsigned long time) {
  static unsigned char days, hours, minutes, seconds;
  static char format[] = "dd:hh:mm:ss";

  days = (unsigned char) elapsedDays(time);
  hours = (unsigned char) numberOfHours(time);
  minutes = (unsigned char) numberOfMinutes(time);
  seconds = (unsigned char) numberOfSeconds(time);

  format[0] = days / 10 + '0';
  format[1] = days % 10 + '0';
  format[3] = hours / 10 + '0';
  format[4] = hours % 10 + '0';
  format[6] = minutes / 10 + '0';
  format[7] = minutes % 10 + '0';
  format[9] = seconds / 10 + '0';
  format[10] = seconds % 10 + '0';

  Serial.print (format);
}

float whatIsTemperatureSensors() {
	// temps for fish tank and ambient air, Grab a count of devices on the wire
	// call sensors.requestTemperatures() to issue a global temperature, request to all devices on the bus
	sensors.requestTemperatures(); // Send the command to get temperatures
	float temp; //left to support the earlier release visualization code - this is now set to the last sensor value if multiple sensors are used

		Serial.print("\t\"temperature_sensor_count\":");
		Serial.print(numberOfDevices);
		Serial.println(","); 

	for(int i=0;i<numberOfDevices; i++)
	{
		// Search the wire for address
		if(sensors.getAddress(tempDeviceAddress, i))
		{
		float temperature = sensors.getTempC(roomTemp);
			// Output the device ID
			Serial.print("\t\"temperature_"); //"temperature_"
			Serial.print(i,DEC); //
			Serial.print("\":"); //"temperature_0":
			float tempF = sensors.getTempF(tempDeviceAddress);
			temp = tempF;
			Serial.print(tempF);//"temperature_0":xx
			Serial.println(","); //"temperature_0":xx,
		} 
		//else ghost device! Check your power requirements and cabling
	}
	return temp;
}

float whatIsTemperature() {
	// temps for fish tank and ambient air, use addresses found in setup 
	// call sensors.requestTemperatures() to issue a global temperature, request to all devices on the bus
	// "temperature_sensor":19.91,
	// "nutrientTemp_sensor":22.20,
	
	sensors.requestTemperatures(); // Send the command to get temperatures
	float nutTemperature = 0;
	if (sensors.getAddress(nutrientTemp, 0)){ // Serial.println("Unable to find address for nutrient temperature"); 
		//float nutTemperature = sensors.getTempC(nutrientTemp);
		nutTemperature = sensors.getTempC(nutrientTemp);
		Serial.print("\t\"nutrientTemp\":");
		Serial.print(nutTemperature);
		Serial.println(","); 
	}
  	if (sensors.getAddress(roomTemp, 1)) { //Serial.println("Unable to find address for room temperature"); 
		float temperature = sensors.getTempC(roomTemp);
		Serial.print("\t\"temperature\":");
		Serial.print(temperature);
		Serial.println(","); 
	}
	return nutTemperature;
}

float whatIsHumidity(){
	//humidity - temp reading from DHt22  
	// Reading temperature or humidity takes about 250 milliseconds!
	// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
	float humidity = 0;
	float temperature = 0;
	humidity = dht.readHumidity();
/*
switch (humidity)
  {
    case "DHTLIB_OK":  
    break;
    case "DHTLIB_ERROR_CHECKSUM": 
    Serial.print("Checksum error,\t"); 
    break;
    case "DHTLIB_ERROR_TIMEOUT": 
    Serial.print("Time out error,\t"); 
    break;
    default: 
    Serial.print("Unknown error,\t"); 
    break;
  }
*/
	temperature = dht.readTemperature();
	// check if returns are valid, if they are NaN (not a number) then something went wrong!
	if (isnan(temperature) || isnan(humidity)) {
		//Serial.println("Failed to read from DHT");
		Serial.println("\t\"humidity\": 0,");
		Serial.println("\t\"humidity_temperature\": 0,");
	} else {

		Serial.print("\t\"humidity\":");
		Serial.print(humidity);
		Serial.println(","); 
		Serial.print("\t\"humidity_temperature\":");
		Serial.print(temperature);
		Serial.println(","); 
	}
}

float whatIsOldHumidity(){
	//humidity - temp reading from DHt22  
	// Reading temperature or humidity takes about 250 milliseconds!
	// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
	float humidity = 0;
	float temperature = 0;
	humidity = dht.readHumidity();
	temperature = dht.readTemperature();
	// check if returns are valid, if they are NaN (not a number) then something went wrong!
	if (isnan(temperature) || isnan(humidity)) {
		//Serial.println("Failed to read from DHT");
		Serial.print("\t\"humidity_sensor\": { \"humidity\":");
		Serial.print(0);
		Serial.print(",\"temperature\":");
		Serial.print(0);
		Serial.println("},"); 
	} else {

		Serial.print("\t\"humidity_sensor\": { \"humidity\":");
		Serial.print(humidity);
		Serial.print(", \"temperature\":");
		Serial.print(temperature);
		Serial.println("},"); 
	}
}

void rpm ()  {   
	//This is the function that the flow rate sensor interupt calls 
	//interrupt routine for hall effect sensor 1 counter
	NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 

int measureFlowRate () {
	//measure flow rate
	//  Serial.println("hall(flow) sensor 1 - measuring flow rate");
	attachInterrupt(0, rpm, RISING); //and the interrupt is attached
	NbTopsFan = 0;	//Set NbTops to 0 ready for calculations
	delay (1000);	//Wait 1 second
	detachInterrupt(0);
	Calc = (NbTopsFan * 60 /   7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour   
	float flowRateGalPerMin = Calc * 0.00440286754; //1 liters per hour = 0.00440286754 US gallons per minute
	return Calc;
}

float whatIsFlowRate(){
	waterIsFlowing = digitalRead(FLOWSWITCH1); //is water flowing
	float flowRateLPerMin = measureFlowRate(); // measure flow rate in liters per min
	float flowRateGalPerMin = flowRateLPerMin * 0.00440286754; //flow rate in gal per min
	Serial.print("\t\"flow_rate_sensor\":");
	Serial.print(flowRateGalPerMin);
	Serial.println(","); 
	return flowRateGalPerMin; 
}

//------------mini ec
//This is really the heart of the calibration process
void calceCSlope ()
{
	//RefVoltage * our deltaRawpH / 12bit steps *mV in V / OP-Amp gain /pH step difference 7-4
	ecParams.eCStep = ((((I2CadcVRef*(float)(ecParams.eCLowCal - ecParams.eCHighCal)) / 4095) * 1000));
}
//Lets read our raw reading while in our lower eC cal solution
void calibrateeCLow(int calnum)
{
	ecParams.eCLowCal = calnum;
	calceCSlope();
	//write these settings back to eeprom
	eeprom_write_block(&ecParams, (void *)0, sizeof(ecParams)); 
}

//Lets read our raw reading while in our higher eC cal solution
void calibrateeCHigh(int calnum)
{
	ecParams.eCHighCal = calnum;
	calceCSlope();
	//write these settings back to eeprom
	eeprom_write_block(&ecParams, (void *)0, sizeof(ecParams));
}

void calcEc(int raw)
{
	float temp, tempmv, tempgain, Rprobe;
	tempmv = (float)i2cADC.calcMillivolts(raw);
	tempgain = (tempmv / (float)oscV) - 1.0; // what is our overall gain again so we can cal our probe leg portion
	Rprobe = (Rgain / tempgain); // this is our actually Resistivity
	temp = ((1000000) * kCell) / Rprobe; // this is where we convert to uS inversing so removed neg exponant
	eC = temp / 1000.0; //convert to EC from uS
}

void whatIsMiniEc(){
	//miniEc
	int adcRaw = i2cADC.calcRollingAVG();
	Serial.print("\t\"miniEc\":");
	Serial.print(adcRaw);
...

This file has been truncated, please download it to see its full contents.

Credits

eric maundu

eric maundu

2 projects • 1 follower
r&d engineer at kijani grows

Comments