#include #include #include // defining which I2C addres to use #define SLAVE_ADDRESS 0x05 // setting he number used to request data from pro mini to send back to the pi int number = 0; //setting up pins for sensors const byte rainGauge = 2; const byte windSpeed = 3; // setting up variables for sensors volatile int rainCount = 0; volatile int windCount = 0; volatile int windCountSend = 0; volatile int windCountSendMax = 0; long rainDebouncingTime = 300; //Debouncing Time in Milliseconds volatile unsigned long rainLastMicros; long windDebouncingTime = 100; //Debouncing Time in Milliseconds volatile unsigned long windLastMicros; volatile int windDir = 0; int temp = 0; // pin for DS18B20 OneWire ds(10); // on pin 10 void setup() { // initialize i2c as slave Wire.begin(SLAVE_ADDRESS); // define callbacks for i2c communication Wire.onReceive(receiveData); Wire.onRequest(sendData); // setup pins for sensors and attach interrupts pinMode(rainGauge, INPUT); digitalWrite (rainGauge, HIGH); // internal pull-up resistor attachInterrupt (0, debounceRain, FALLING); // attach interrupt handler pinMode(windSpeed, INPUT); digitalWrite (windSpeed, HIGH); // internal pull-up resistor attachInterrupt (1, debounceWind, FALLING); // attach interrupt handler Serial.begin(9600); } void loop() { //***************************************************************** // Read DS18B20 int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract; byte i; byte present = 0; byte data[12]; byte addr[8]; if ( !ds.search(addr)) { ds.reset_search(); return; } ds.reset(); ds.select(addr); ds.write(0x44,1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. present = ds.reset(); ds.select(addr); ds.write(0xBE); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); } LowByte = data[0]; HighByte = data[1]; TReading = (HighByte << 8) + LowByte; SignBit = TReading & 0x8000; // test most sig bit if (SignBit) // negative { TReading = (TReading ^ 0xffff) + 1; // 2's comp } Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25 temp = Tc_100; //********************************************************************************* // get wind speed windCount = 0; delay(10000); windCountSend = windCount; if (windCountSend > windCountSendMax){ windCountSendMax = windCountSend; } } // callback for received data void receiveData(int byteCount){ while(Wire.available()) { number = Wire.read(); // setup to reset rain count. R-Pi sends this at midnight to reset if (number == 8){ rainCount = 0; } } } // callback for sending data void sendData(){ byte sendData[2]; int sendVal = 0; if (number == 1){ sendVal = rainCount; } // wind speed else if (number == 2){ sendVal = windCountSend; } // wind direction else if (number == 3){ sendVal = analogRead(1); } // wind gust speed else if (number == 4){ sendVal = windCountSendMax; windCountSendMax = 0; } // temperature else if (number == 5){ sendVal = temp; } // humidity else if (number == 6){ sendVal = analogRead(2); } else { sendVal = number; } // sending requested data to the Pi sendData[0] = (byte) sendVal & 0xFF; // Serial.println(sendData0, HEX); sendData[1] = (byte) (sendVal >> 8) & 0xFF; // Serial.println(sendData1, HEX); Wire.write(sendData, 2); //Wire.write(sendData0); //Wire.write(sendData1); } // debouncing of interrupt sensors so only count once per action void debounceRain() { if((long)(micros() - rainLastMicros) >= rainDebouncingTime * 1000) { rainFunc(); rainLastMicros = micros(); } } void debounceWind() { if((long)(micros() - windLastMicros) >= windDebouncingTime * 1000) { windFunc(); windLastMicros = micros(); } } // incrementing the variables used for counting sensors void rainFunc() { rainCount++; Serial.print(rainCount); } void windFunc() { windCount++; Serial.println(windCount); }