// SLAVE #include #include "Adafruit_FONA.h" #include #include #include #include #include #include "DHT.h" #define FONA_RX 2 #define FONA_TX 10 #define FONA_RST 4 #define ONE_WIRE_BUS_PIN 11 #define DHTPIN 4 //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 // Pin 5 //Sends a call back signal to the master when successful data transmission. DHT dht(DHTPIN, DHTTYPE); OneWire oneWire(ONE_WIRE_BUS_PIN); DallasTemperature sensors(&oneWire); DeviceAddress Probe01 = { 0x28, 0xFF, 0xA3, 0x4D, 0x00, 0x16, 0x03, 0xEC }; // These are the addresses of the temp probes DS18B20 DeviceAddress Probe02 = { 0x28, 0x4E, 0xCA, 0x80, 0x04, 0x00, 0x00, 0xA0 }; DeviceAddress Probe03 = { 0x28, 0x7C, 0x8A, 0x5D, 0x05, 0x00, 0x00, 0xFD }; const long ferrets = 179224; int n = 0; int z = 0; int moisture = 0; unsigned long heatindex = 0; unsigned long humidity = 0; unsigned long tempDHT22 =0; unsigned long windspeed = 0; unsigned long windgust = 0; unsigned long avWindPulseRate = 0; unsigned long maxWindPulseRate = 0; unsigned long rain = 0; unsigned long windDirection = 0; unsigned long windway = 0; unsigned long moistureValue = 0; unsigned long pressure = 0; unsigned long volts = 1200; // multiplied by 100. unsigned long tempint; unsigned long tempout; unsigned long tempsoil; unsigned long previousMillis = 0; const long interval = 10000; // this is a large buffer for replies char replybuffer[255]; String initiator, dataString; const char webAddress[180] = "http://www.goatindustries.co.uk/weather/send.php?"; char url[180]; #include SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX); SoftwareSerial *fonaSerial = &fonaSS; Adafruit_FONA fona = Adafruit_FONA(FONA_RST); Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); uint8_t type; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(115200); pinMode(5, OUTPUT); //Sends a call back signal to the master when successful data transmission. pinMode(6, OUTPUT); // Resets FONA digitalWrite(6,LOW); // delay (1000); // digitalWrite(6,HIGH); // delay (100); // digitalWrite(6,LOW); dht.begin(); if(!bmp.begin()) { /* There was a problem detecting the BMP085 ... check your connections */ Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!"); while(1); } Wire.begin(9); Wire.onReceive(receiveEvent); while (!Serial); // Use this for initiating program by opening of serial monitor. Delete in normal operation. Serial.println(F("FONA basic test")); Serial.println(F("Initializing....(May take 3 seconds)")); sensors.begin(); // Temperature sensors DS18B20. sensors.setResolution(Probe01, 11); /////// Was on pin 10 in version 09 sensors.setResolution(Probe02, 11); sensors.setResolution(Probe03, 11); fonaSerial->begin(4800); if (! fona.begin(*fonaSerial)) { Serial.println(F("Couldn't find FONA")); while (1); } type = fona.type(); Serial.println(F("FONA is OK")); Serial.print(F("Found ")); switch (type) { case FONA800L: Serial.println(F("FONA 800L")); break; case FONA800H: Serial.println(F("FONA 800H")); break; case FONA808_V1: Serial.println(F("FONA 808 (v1)")); break; case FONA808_V2: Serial.println(F("FONA 808 (v2)")); break; case FONA3G_A: Serial.println(F("FONA 3G (American)")); break; case FONA3G_E: Serial.println(F("FONA 3G (European)")); break; default: Serial.println(F("???")); break; } networkStatus(); // Check the network is available. Home is good. // Test FONA by printing module IMEI number. char imei[15] = {0}; // MUST use a 16 character buffer for IMEI! uint8_t imeiLen = fona.getIMEI(imei); if (imeiLen > 0) { Serial.print("Module IMEI: "); Serial.println(imei); } Serial.println(""); Serial.println("Checking that GPRS is turned off to start with ........."); fona.setGPRSNetworkSettings(F("pp.vodafone.co.uk"), F("wap"), F("wap")); // Change these settings! (Network APN, username ,password) delay (1000); // Turn off GPRS: if (!fona.enableGPRS(false)) Serial.println(F("No - Failed to turn off")); Serial.println("If the line above says OK, then GPRS has just been turned off"); delay (1000); Serial.println(""); Serial.println("Waiting for data from i2c ..........."); Serial.println(""); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { if ( z>0) // This loop is initiated if z is greater than 1. The value of z is given by the reciept { // of i2c data. There needs to be a second z evaluation to enable fona to turn on it's GPRS networkStatus(); // and prevent data loss in the time gap. volts = analogRead(0)*4.688; // All decimals are converted to integers as we don't want to send the decimal place character thru HTTP. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true); delay (3000); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); humidity = h; tempDHT22 = t; heatindex = hic; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sensors_event_t event; bmp.getEvent(&event); if (event.pressure) { pressure = (event.pressure) *100; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// sensors.requestTemperatures(); // DS18B20s windspeed = avWindPulseRate *20; windgust = maxWindPulseRate *20; windway = windDirection; moisture = moistureValue * 1.08; printData(); // Prints all the data to serial for debugging. Serial.print(F("FONA> ")); turnOnGPRS(); turnOnGPRS(); // read website URL uint16_t statuscode; int16_t length; char url[180]; flushSerial(); //weatherData(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // This is where the data is compiled into a character .... dataString = initiator + webAddress +"windspeed="+windspeed +"&windgust="+windgust +"&windway="+windway +"&tempout="+tempout +"&tempsoil="+tempsoil +"&rain="+rain +"&humidity="+humidity +"&pressure="+pressure +"&volts="+volts +"&moisture="+moisture +"&tempint="+tempint +"&ferrets="+ferrets ; int n = dataString.length(); Serial.print("Data string to send: ");Serial.println(dataString); Serial.print("Size of string: ");Serial.println(n); // Builds the url character: for (int aa=0;aa<=n;aa++) { url[aa] = dataString[aa]; } Serial.print("Character data to send: ");Serial.println(url); Serial.println(""); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //..... and sent out via HTTP to a PHP file called send.php ready to recieve it. Serial.print("Sending url .... "); Serial.print("Compiled url is: ");Serial.println(url); Serial.println(F("****")); if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) { Serial.println("Failed!"); } while (length > 0) { while (fona.available()) { char c = fona.read(); // Serial.write is too slow, we'll write directly to Serial register! #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */ UDR0 = c; #else digitalWrite(5, HIGH); // Call back to Master to say that tranmission was successful. tone(7,((windway*15)+400),1000); noTone(7); Serial.write(c); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #endif length--; if (! length) break; } } Serial.println(F("\n****")); fona.HTTP_GET_end(); Serial.println("Wait for 10 seconds ..........."); delay(500); digitalWrite(5, LOW); delay (10000); // Turn off GPRS: if (!fona.enableGPRS(false)) Serial.println(F("No - Failed to turn off")); Serial.println("GPRS turned off?"); Serial.println("Waiting for data from i2c ..........."); // flush input flushSerial(); while (fona.available()) { Serial.write(fona.read()); } z=0; } } // End of main loop. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Functions: void flushSerial() { while (Serial.available()) Serial.read(); } char readBlocking() { while (!Serial.available()); return Serial.read(); } uint16_t readnumber() { uint16_t x = 0; char c; while (! isdigit(c = readBlocking())) { //Serial.print(c); } Serial.print(c); x = c - '0'; while (isdigit(c = readBlocking())) { Serial.print(c); x *= 10; x += c - '0'; } return x; } uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) { uint16_t buffidx = 0; boolean timeoutvalid = true; if (timeout == 0) timeoutvalid = false; while (true) { if (buffidx > maxbuff) { //Serial.println(F("SPACE")); break; } while (Serial.available()) { char c = Serial.read(); //Serial.print(c, HEX); Serial.print("#"); Serial.println(c); if (c == '\r') continue; if (c == 0xA) { if (buffidx == 0) // the first 0x0A is ignored continue; timeout = 0; // the second 0x0A is the end of the line timeoutvalid = true; break; } buff[buffidx] = c; buffidx++; } if (timeoutvalid && timeout == 0) { //Serial.println(F("TIMEOUT")); break; } delay(1); } buff[buffidx] = 0; // null term return buffidx; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void receiveEvent(int bytes) { // Make sure the order is the same in master and slave. delay(10); maxWindPulseRate = Wire.read(); delay(10); windDirection = Wire.read(); delay(10); rain = Wire.read(); delay(10); avWindPulseRate = Wire.read(); delay(10); moistureValue = Wire.read(); delay(10); z=1; moisture = moistureValue; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void printSoilTemperature(DeviceAddress deviceAddress) { float temp = sensors.getTempC(deviceAddress); tempsoil = temp*100; if (tempsoil == -127.00) { Serial.print("Error getting temperature "); } else { Serial.print(tempsoil); Serial.print(" degrees C *100"); } } void printOutsideTemperature(DeviceAddress deviceAddress) { float temp = sensors.getTempC(deviceAddress); tempout = temp*100; if (tempout == -127.00) { Serial.print("Error getting temperature "); } else { Serial.print(tempout); Serial.print(" degrees C *100"); } } void printInsideTemperature(DeviceAddress deviceAddress) { float temp = sensors.getTempC(deviceAddress); tempint = temp; if (tempint == -127.00) { Serial.print("Error getting temperature "); } else { Serial.print(tempint); Serial.print(" degrees CC *100"); } } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void networkStatus() { // read the network/cellular status uint8_t n = fona.getNetworkStatus(); Serial.println(""); Serial.print(F("Network status ")); Serial.print(n); Serial.print(F(": ")); if (n == 0) Serial.println(F("Not registered")); if (n == 1) Serial.println(F("Registered (home)")); if (n == 2) Serial.println(F("Not registered (searching)")); if (n == 3) Serial.println(F("Denied")); if (n == 4) Serial.println(F("Unknown")); if (n == 5) Serial.println(F("Registered roaming")); Serial.println("The network status must be 'Registered' 'home' in the line above"); Serial.println(""); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void turnOnGPRS() { Serial.println("Now attempting to turn on GPRS ........."); if (!fona.enableGPRS(true)) Serial.println(F("No - Failed to turn on")); Serial.println("GPRS is on if the line above shows 'OK'"); Serial.println("Wait for 10 seconds to make sure GPRS is on ..........."); delay (10000); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void printData() { Serial.print("Probe01 (outside) temp: "); printOutsideTemperature(Probe01); Serial.println(); Serial.print("Probe02 (soil) temp: "); printSoilTemperature(Probe02); Serial.println(); Serial.print("Probe03 (tempint) temp: "); printInsideTemperature(Probe03); Serial.println(); Serial.print("Main battery voltage: ");Serial.println(volts); Serial.print("i2c maxWindPulseRate: ");Serial.println(windgust); // Not calibrated yet. Serial.print("i2c avWindPulseRate: ");Serial.println(windspeed); Serial.print("i2c windDirection: ");Serial.println(windDirection); Serial.print("i2c rain: ");Serial.println(rain); Serial.print("i2c moistureValue: ");Serial.println(moistureValue); Serial.print("Atmospheric pressure: "); Serial.print(pressure); Serial.println(" hPa *100"); Serial.print("Humidity: "); Serial.print(humidity); Serial.println(" %\t"); Serial.print("DHT22 temp: "); Serial.print(tempDHT22); Serial.println(" *C "); Serial.print("Heat index: "); Serial.print(heatindex); Serial.println(" *C "); Serial.println(""); }