Quantcast
Channel: ThingSpeak IoT Community - Forum: Arduino
Viewing all articles
Browse latest Browse all 172

HydroponicsMAC on Multiple 1 Wire Bus on Digital Pin ....

$
0
0

Here's the code I'm using which is doing something similar.

#include "ThingSpeak.h"
unsigned long myChannelNumber = 123456789;
const char * myWriteAPIKey = "ABCDEFGHIJK";

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //DE:AD:BE:EF:FE:ED, Internal static IP is 192.168.0.52
EthernetClient client;

#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(3);
DallasTemperature OneWire_Sensors(&oneWire);

#include "DHT.h"
#define PLANT_DHTPIN 2
#define DHTTYPE DHT11
DHT Plant_dht(PLANT_DHTPIN, DHTTYPE);

#define RAIN_SENSOR_DIGITAL 4

float fltEnviromentTemp = 0, fltPlantTemp = 0, fltReservoirTemp = 0;
int i, intPlantHumidity = 0, intSensorAlarm = 0, intRainAlarm = 0;
boolean blnRainAlarmNotified = false;

void setup() {
Serial.begin(9600);

pinMode(RAIN_SENSOR_DIGITAL, INPUT);

Serial.println(F("v." __DATE__ ", " __TIME__ "
"));

Serial.print(F("Starting network"));
Ethernet.begin(mac);

Serial.print(F(", ThingSpeak"));
ThingSpeak.begin(client);

Serial.println(F(" and sensors."));
OneWire_Sensors.begin();
Plant_dht.begin();
}

void loop() {
Serial.println(F("
Reading sensors"));

// Rain sensor
if (!(digitalRead(RAIN_SENSOR_DIGITAL))) {
Serial.println(F("WARNING: Rain detected, ALARM activated"));
intRainAlarm = 1;
blnRainAlarmNotified = true;
} else {
intRainAlarm = 0;
blnRainAlarmNotified = false;
}

//Onewire sensors
OneWire_Sensors.requestTemperatures();
fltEnviromentTemp = OneWire_Sensors.getTempCByIndex(0);
fltPlantTemp = OneWire_Sensors.getTempCByIndex(1);
fltReservoirTemp = OneWire_Sensors.getTempCByIndex(2);

//DHT11 sensor
intPlantHumidity = Plant_dht.readHumidity();

Serial.print(F("Environment = "));
Serial.print(fltEnviromentTemp);
Serial.println(F("*C"));
Serial.print(F("Plant = "));
Serial.print(fltPlantTemp);
Serial.println(F("*C"));
Serial.print(F("PlantHumidity="));
Serial.print(intPlantHumidity);
Serial.println(F("%"));
Serial.print(F("Reservoir = "));
Serial.print(fltReservoirTemp);
Serial.println(F("*C"));
Serial.print(F("RainAlarm = "));
Serial.println(intRainAlarm);

if ((fltEnviromentTemp == -127.00) || (fltPlantTemp == -127.00) || (fltReservoirTemp == -127.00) || isnan(intPlantHumidity) || (intPlantHumidity == 0))
{
Serial.println(F("WARNING: Sensor problem, ALARM activated"));
intSensorAlarm = 1;
} else {
Serial.println(F("Sensors are OK"));
intSensorAlarm = 0;
}

ThingSpeak.setField(1, fltEnviromentTemp);
ThingSpeak.setField(2, fltPlantTemp);
ThingSpeak.setField(3, intPlantHumidity);
ThingSpeak.setField(4, fltReservoirTemp);
ThingSpeak.setField(5, intRainAlarm);
ThingSpeak.setField(6, intSensorAlarm);

Serial.println(F("Updating ThingSpeak"));

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // ThingSpeak only accept updates every 15 seconds

Serial.println(F("Finished updating"));

Serial.println(F("Sleeping for 15 minutes"));

for (i = 0; i < 45; i++) { //15 minutes = 45 * 20,000 = 900,000
delay(20000); //Wait 20 seconds
Serial.println(F("Checking for rain"));

if (!(digitalRead(RAIN_SENSOR_DIGITAL)))
{
Serial.println(F("WARNING: Rain detected, ALARM activated"));

if (blnRainAlarmNotified == false)
{
Serial.println(F("Updating ThingSpeak"));
ThingSpeak.writeField(myChannelNumber, 5, 1, myWriteAPIKey);
Serial.println(F("Finished updating"));

blnRainAlarmNotified = true;

} else {
Serial.println(F("Already updated ThingSpeak"));
}
}
}
}


Viewing all articles
Browse latest Browse all 172

Trending Articles