#include "OneWire.h" #include "DallasTemperature.h" #include #define AWAKE 4 // WIFLY AWAKE #define PANEL A8 // PANEL LEVEL #define BAT A7 // BAT LEVEL #define IO0 5 // MICS5525_HEATHER #define IO1 13 // MICS2710_HEATHER #define IO2 9 // MICS2710_HIGH_IMPEDANCE #define IO3 10 // MICS2710_HIGH_IMPEDANCE #define FACTORY 7 // WIFLY - Factory RESET/AP RN131 #define CONTROL 12 // WIFLY - CONTROL #define S0 A4 //MICS_5525 #define S1 A5 //MICS_2710 #define S2 A2 //SENS_5525 #define S3 A3 //SENS_2710 #define S4 A0 //MICRO #define S5 A1 //LDR uint8_t dataPin = IO1; uint8_t latchPin = IO3; uint8_t clockPin = IO2; uint8_t val_shift; //Valor del shift register uint8_t val_shift1; //Valor del shift register #define bh1730 0x29 // Direction of the light sensor #define Temperature 0x40 // Direction of the sht21 #define MCP1 0x2E // Direction of the mcp1 Potenciometers #define PH 0x00 // Direction of ph resistor #define MOTOR0 0x01 // Direction of motor 1 resistor #define MOTOR1 0x07 // Direction of motor 2 resistor #define TWI_FREQ 400000L //Frecuencia bus I2C // DS18S20 Temperature chip i/o OneWire ds(IO0); // on pin 10 OneWire ds1(S5); // on pin 10 // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&ds); DallasTemperature sensors1(&ds1); void led_rgb(boolean red, boolean green, boolean blue){ shiftWrite(11, !blue); shiftWrite(10, !green); shiftWrite( 9, !red); } float average(int anaPin) { int lecturas = 100; long total = 0; float average = 0; for(int i=0; iabout 216mv if the voltage(compensate)<150,that is <1ms/cm,out of the range // else if(CoefficientVolatge>3300)Serial.println("Out of the range!"); //>20ms/cm,out of the range else { ECcurrent = kmEC*CoefficientVolatge + kbEC; // if(CoefficientVolatge<=448)ECcurrent=(6.84*CoefficientVolatge-64.32)*1.51; //1ms/cm500) return 0x00; DATA = Wire.read()<<8; while (!Wire.available()); DATA = (DATA|Wire.read()); DATA &= ~0x0003; return DATA; } void getSHT21() { lastTemperature = readSHT21(0xE3); // RAW DATA for calibration in platform lastHumidity = readSHT21(0xE5); // RAW DATA for calibration in platform Serial.print(" Temperature SHT21: "); Serial.print((-46.85 + 175.72 / 65536.0 * (float)lastTemperature)); Serial.print(" C, Humidity SHT21: "); Serial.print(-6.0 + 125.0 / 65536.0 * (float)lastHumidity); Serial.print(" % "); } uint16_t getLight() { uint8_t TIME0 = 0xDA; uint8_t GAIN0 = 0x00; uint8_t DATA [8] = {0x03, TIME0, 0x00 ,0x00, 0x00, 0xFF, 0xFF ,GAIN0} ; uint16_t DATA0 = 0; uint16_t DATA1 = 0; Wire.beginTransmission(bh1730); Wire.write(0x80|0x00); for(int i= 0; i<8; i++) Wire.write(DATA[i]); Wire.endTransmission(); delay(100); Wire.beginTransmission(bh1730); Wire.write(0x94); Wire.endTransmission(); Wire.requestFrom(bh1730, 4); DATA0 = Wire.read(); DATA0=DATA0|(Wire.read()<<8); DATA1 = Wire.read(); DATA1=DATA1|(Wire.read()<<8); uint8_t Gain = 0x00; if (GAIN0 == 0x00) Gain = 1; else if (GAIN0 == 0x01) Gain = 2; else if (GAIN0 == 0x02) Gain = 64; else if (GAIN0 == 0x03) Gain = 128; float ITIME = (256- TIME0)*2.7; float Lx = 0; float cons = (Gain * 100) / ITIME; float comp = (float)DATA1/DATA0; if (comp<0.26) Lx = ( 1.290*DATA0 - 2.733*DATA1 ) / cons; else if (comp < 0.55) Lx = ( 0.795*DATA0 - 0.859*DATA1 ) / cons; else if (comp < 1.09) Lx = ( 0.510*DATA0 - 0.345*DATA1 ) / cons; else if (comp < 2.13) Lx = ( 0.276*DATA0 - 0.130*DATA1 ) / cons; else Lx=0; Serial.print(" BH1730: "); Serial.print(Lx); Serial.print(" Lx"); return Lx; } #define RES 256 // Digital pot. resolution #define P1 5 //Digital potentiometer resistance 100Kohm #define VCC 3300 //Voltage VCC float kr= ((float)P1*1000)/RES; // Resistance conversion Constant for the digital pot. void writeMCP(byte deviceaddress, byte address, int data ) { if (data>RES) data=RES; Wire.beginTransmission(deviceaddress); address=(address<<4)|bitRead(data, 8) ; Wire.write(address); Wire.write(lowByte(data)); Wire.endTransmission(); delay(4); } int readMCP(int deviceaddress, uint16_t address ) { byte rdata = 0xFF; int data = 0x0000; Wire.beginTransmission(deviceaddress); address=(address<<4)|B00001100; Wire.write(address); Wire.endTransmission(); Wire.requestFrom(deviceaddress,2); unsigned long time = millis(); while (!Wire.available()) if ((millis() - time)>500) return 0x00; rdata = Wire.read(); data=rdata<<8; while (!Wire.available()); rdata = Wire.read(); data=data|rdata; return data; } void writeRL(byte device, long resistor) { int data=0x00; data = (int)(resistor/kr); writeMCP(MCP1, device, data); } float readRL(byte device) { return (kr*readMCP(MCP1, device)); // Returns Resistance (Ohms) } void writeGAIN(float gain) { long resistor=0x00; resistor = (long)((((1+P1)*(gain-1)-2.2)/gain)*1000); writeRL(PH, resistor); } float readGAIN() { float resistor = readRL(PH)/1000.; return (2.2 + resistor)/(1 + P1 - resistor) + 1; // Returns Resistance (Ohms) } void motor_orientation(int motor, int orientation){ if (motor==MOTOR1) { if (orientation == 0 ) { shiftWrite(1, 0); shiftWrite(2, 0); } else if (orientation == 1 ) { shiftWrite(1, 0); shiftWrite(2, 1); } else if (orientation == -1 ) { shiftWrite(1, 1); shiftWrite(2, 0); } } else { if (orientation == 0 ) { shiftWrite(3, 0); shiftWrite(4, 0); } else if (orientation == 1 ) { shiftWrite(3, 0); shiftWrite(4, 1); } else if (orientation == -1 ) { shiftWrite(3, 1); shiftWrite(4, 0); } } } void writeVoltage(byte device, float voltage) { int data=0x00; if (voltage==0) motor_orientation(device, 0); else if (voltage>0) motor_orientation(device, 1); else if (voltage<0) motor_orientation(device, -1); data = (int)(abs(voltage)*P1*1000/VCC); // Serial.println(data); writeRL(device, data); } float readVoltage(byte device) { return (VCC*readRL(device))/(P1*1000); // Returns Resistance (Ohms) } byte opto[4] = {12, 13, 14, 15}; void relay(byte device, boolean state) { shiftWrite(opto[device], state); } void setup(){ pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); Wire.begin(); TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; Serial.begin(115200); resetshift(); led_rgb(0, 0, 4095); // Start up the library DS18B20 sensors.begin(); sensors1.begin(); for (int i=0; i<4; i++) relay(i, LOW); } void loop(){ Serial.print("EC: "); Serial.print(EcSensor()); Serial.print(" ms/cm"); sensors.requestTemperatures(); // Send the command to get temperatures Serial.print(" Temperature DS18B20: "); Serial.print(sensors.getTempCByIndex(0)); Serial.print(" ^C"); getSHT21(); getLight(); writeGAIN(3.5); Serial.print(" GAIN PH: "); Serial.print(readGAIN()); Serial.print(" PH: "); Serial.print(PHSensor()); Serial.print(" HUMIDITY: "); Serial.print(average(S0)); sensors1.requestTemperatures(); // Send the command to get temperatures Serial.print(" Temperature1 DS18B20: "); Serial.print(sensors1.getTempCByIndex(0)); Serial.print(" ^C"); writeVoltage(MOTOR0, 0); Serial.print(" Voltage MOTOR1: "); Serial.print(readVoltage(MOTOR0)); writeVoltage(MOTOR1, 0); Serial.print(" Voltage MOTOR2: "); Serial.println(readVoltage(MOTOR1)); // for(int i=7; i<16; i++) // { // Serial.println(i); // shiftWrite(i, LOW); // delay(1000); // shiftWrite(i, HIGH); // delay(1000); // } }