// This code reads temp and hum from DHT11, the board got local dhts spread out to the entire place + ethernet shield that gets data from the arduino's at the greenhouses
//Written by Shachaf Zilberman property of Groundwork Bio Ag Ltd
// Last update: 11/01/2017
//librarys:
#include "DHT.h"
#include "MegunoLink.h"
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// end of librarys
//meguno time plot declarations
const byte numberOfTimePlots = 8;
TimePlot MyPlotArray[] = {("WHTemp"),("WHHum"),("PDTemp"),("PDHum"),("GH1Temp"),("GH1Hum"),("MLTemp"),("MLHum")};
const char *MyPlotTitle[20] = {"WHTemp","WHHum","PDTemp","PDHum","GH1Temp","GH1Hum","MLTemp","MLHum"};
//update
//end of meguno time plot declarations
// DHT 11 settings
#define numberOfDhts 10 // how many dhts
#define DHT1PIN 22 // what pin we're connected to
#define DHT2PIN 23
#define DHT3PIN 24
#define DHT4PIN 25
#define DHT5PIN 26
#define DHT6PIN 27
#define DHT7PIN 28
#define DHT8PIN 29
#define DHT9PIN 30
#define DHT10PIN 31
//update
#define DHTTYPE DHT11 // DHT type decleration
#define MaxNumberOfDhtAtGreenhouse 30// the maximum number of dhts in 1 greenhouse
// setting the dht pin and dht type
DHT dht1(DHT1PIN, DHTTYPE);
DHT dht2(DHT2PIN, DHTTYPE);
DHT dht3(DHT3PIN, DHTTYPE);
DHT dht4(DHT4PIN, DHTTYPE);
DHT dht5(DHT5PIN, DHTTYPE);
DHT dht6(DHT6PIN, DHTTYPE);
DHT dht7(DHT7PIN, DHTTYPE);
DHT dht8(DHT8PIN, DHTTYPE);
DHT dht9(DHT9PIN, DHTTYPE);
DHT dht10(DHT10PIN, DHTTYPE);
DHT dhtArray[numberOfDhts]= {dht1,dht2,dht3,dht4,dht5,dht6,dht7,dht8,dht9,dht10};//update
// create arrays to store the temps and hums that we read
float tempsArray[numberOfDhts];
float humsArray[numberOfDhts];
const byte numberOfGroups = 4;
const int dhtGroup[] = {0,0,0,0,1,1,1,1,2,3}; //update
byte dhtGroupSize[] = {4,4,1,1};
unsigned long previousMillis = 0;// we dont want to read the dht too many times so we delays it with this
const long interval = 1000;
byte mac[] = { // the ethernet shield mac adrs
0xDE, 0xAC, 0xBE, 0xEF, 0xFE, 0xA1
};
// ip of this station
IPAddress ip(192, 168, 10, 112);
// port of this station
unsigned int localPort = 8765;
// how many greenhouses connected with dht
#define numberOfGreenHouses 3
// the ip of this greenhouses
IPAddress server1(192, 168, 10, 122); // all gh
IPAddress server2(192, 168, 10, 124);// gh 2
IPAddress server3(192, 168, 10, 125);// not in use
//array of the servers ip from above
IPAddress greenHouseIp[numberOfGreenHouses] = { server1,server2,server3 };
//the ports for each server in array
unsigned int greenHousePort[numberOfGreenHouses] = {2222,4444,5555};
// needed to read the messages from the servers
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
// declare the UDP communication method
EthernetUDP Udp;
int tempArrayOnQRoom[MaxNumberOfDhtAtGreenhouse];
int humArrayOnQRoom[MaxNumberOfDhtAtGreenhouse];
boolean gotResponed = false;
void setup() {
Serial.begin(19200);
Serial.println("Dht master Start");
for(int i=0;i<numberOfDhts;i++){
dhtArray[i].begin();
}
Ethernet.begin(mac, ip);
Udp.begin(localPort);
for(int i=0;i<numberOfTimePlots;i++){
MyPlotArray[i].SetTitle(MyPlotTitle[i]);
MyPlotArray[i].SetXlabel("Time");
MyPlotArray[i].SetYlabel("Value");
MyPlotArray[i].SetSeriesProperties(MyPlotTitle[i], Plot::Magenta, Plot::Solid, 2, Plot::Square);
delay(50);
}
}
void loop() {
// Wait a few seconds between measurements.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
for(int i=0;i<numberOfGreenHouses;i++){
delay(1000);
Udp.beginPacket(greenHouseIp[i],greenHousePort[i]);
Udp.write("Temp");
Udp.endPacket();
int packetSize = Udp.parsePacket();
int counter = 0;
while (!packetSize) {
packetSize = Udp.parsePacket();
counter++;
Serial.print(F("."));
delay(50);
if(counter >= 40){
Serial.println();
Serial.print("cant get responed from:");
Serial.println(greenHouseIp[i]);
gotResponed = false;
break;
}
}
if(counter < 49){
gotResponed = true;
}
if(gotResponed){
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
delay(100);
// read the packet into packetBufffer
Udp.read(packetBuffer, packetSize);
delay(50);
Serial.println("Contents:");
Serial.println(packetBuffer);
stringOne.substring(14, 18)
int numberOfDhtOnSlave = StrExtractVals(packetBuffer, "N", 2, UDP_TX_PACKET_MAX_SIZE);
if(numberOfDhtOnSlave == 999){
Serial.println("Un valid");
}else{
Serial.print("number of dht on QR: ");
Serial.println(numberOfDhtOnSlave);
for(int i=0;i<numberOfDhtOnSlave;i++){
tempArrayOnQRoom[i]= StrExtractVals(packetBuffer, "T", 2, UDP_TX_PACKET_MAX_SIZE);
Serial.print("Sensor ");
Serial.print(i);
Serial.print(" Temp: ");
Serial.print(tempArrayOnQRoom[i]);
humArrayOnQRoom[i]=StrExtractVals(packetBuffer, "H", 2, UDP_TX_PACKET_MAX_SIZE);
Serial.print(" Hum: ");
Serial.println(humArrayOnQRoom[i]);
}
}
StrClear(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Ethernet.maintain();
}
}
}
for(int i=0;i<numberOfDhts;i++){
humsArray[i] = dhtArray[i].readHumidity();
Serial.print("DHT: ");
Serial.print(i);
Serial.print(" Hum now: ");
Serial.print(dhtArray[i].readHumidity());
// Read temperature as Celsius
tempsArray[i] = dhtArray[i].readTemperature();
Serial.print(" Temp now: ");
Serial.println(dhtArray[i].readTemperature());
if(tempsArray[i] <=100 && tempsArray[i]>= 0){
TableData(dhtName[i],"T-"+String(tempsArray[i])+" H-"+String(humsArray[i]),String(descriptionArray[i]));
}else{
TableData(dhtName[i],"OFFLINE",String(descriptionArray[i]));
}
delay(50);
}
int tempSum[numberOfGroups];
int humSum[numberOfGroups];
for(int i=0;i<numberOfGroups;i++){
tempSum[i] = 0;
humSum[i] = 0;
}
for(int i=0;i<numberOfDhts;i++){
if(tempsArray[i] <=100 || tempsArray[i] >=0){
tempSum[dhtGroup[i]] += tempsArray[i];
humSum[dhtGroup[i]] += humsArray[i];
}
}
for(byte i=0;i<numberOfGroups;i++){
tempSum[i] = tempSum[i]/dhtGroupSize[i];
humSum[i] = humSum[i]/dhtGroupSize[i];
}
MyPlotArray[0].SendData(MyPlotTitle[0], tempSum[0]);
MyPlotArray[1].SendData(MyPlotTitle[1], humSum[0]);
MyPlotArray[2].SendData(MyPlotTitle[2], tempSum[1]);
MyPlotArray[3].SendData(MyPlotTitle[3], humSum[1]);
MyPlotArray[4].SendData(MyPlotTitle[4], tempSum[2]);
MyPlotArray[5].SendData(MyPlotTitle[5], humSum[2]);
MyPlotArray[6].SendData(MyPlotTitle[6], tempSum[3]);
MyPlotArray[7].SendData(MyPlotTitle[7], humSum[3]);
delay(2000);
}
void TableData(String Name, String State, String Description)
{
Serial.print("{TABLE");
Serial.print("|SET|");
Serial.print(Name);
Serial.print("|");
Serial.print(State);
Serial.print("|");
Serial.print(Description);
Serial.println("}");
}
int StrExtractVals(char *str, char *sfind, int numberOfCharsPerVal, int len)
{
//this function gets char array and turn it into val
char found = 0;
int index = 0;
String valueString = "";
if (strlen(sfind) > len) {
return 999;
}//end of>> if (strlen(sfind) > len)
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
for (byte i = 1; i <= numberOfCharsPerVal; i++) {
valueString += str[index + i];
}
return valueString.toInt();
}
}
else {
found = 0;
}
index++;
}
return 999;
}
void StrClear(char *str, char length)
{
//this function clears the str
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}