Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <SD.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "DHT.h"
- // size of buffer used to capture HTTP requests
- #define REQ_BUF_SZ 20
- #define BUFSIZ 100
- // MAC address from Ethernet shield sticker under board
- byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192, 168, 0, 200); // IP address, may need to change depending on network
- EthernetServer server(80); // create a server at port 80
- char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
- char req_index = 0; // index into HTTP_req buffer
- //Temperatures variables
- float waterInTemp;
- float waterOutTemp;
- float airInTemp;
- float airOutTemp;
- float airInHumidity;
- float airOutHumidity;
- long lastTime=0;
- #define TEMP_DELAY 5000 //Delay between two temperatures in ms
- #define WATER_IN_BUS 2
- #define WATER_OUT_BUS 3
- #define AIR_IN_BUS 5
- #define AIR_OUT_BUS 6
- #define DHTTYPE DHT22
- // Setup oneWire instance to communicate with devices
- OneWire waterInWire(WATER_IN_BUS);
- OneWire waterOutWire(WATER_OUT_BUS);
- // Pass oneWire reference to Dallas Temperature.
- DallasTemperature sensorWaterIn(&waterInWire);
- DallasTemperature sensorWaterOut(&waterOutWire);
- DHT sensorAirIn(AIR_IN_BUS, DHTTYPE);
- DHT sensorAirOut(AIR_OUT_BUS, DHTTYPE);
- void setup()
- {
- // disable Ethernet chip
- pinMode(10, OUTPUT);
- digitalWrite(10, HIGH);
- Serial.begin(9600); // for debugging
- // initialize SD card
- Serial.println("Initializing SD card...");
- if (!SD.begin(4)) {
- Serial.println("ERROR - SD card initialization failed!");
- return; // init failed
- }
- Serial.println("SUCCESS - SD card initialized.");
- //Initialize Ethernet
- Ethernet.begin(mac, ip); // initialize Ethernet device
- server.begin(); // start to listen for clients
- //Initialize Sensors
- sensorWaterIn.begin();
- sensorWaterOut.begin();
- sensorAirIn.begin();
- sensorAirOut.begin();
- }
- void loop()
- {
- if(TempTimer())
- {
- GetTemperatures();
- WriteTemperatures();
- }
- Internet();
- }
- //Get temperatures
- void GetTemperatures()
- {
- sensorWaterIn.requestTemperatures();
- waterInTemp = (sensorWaterIn.getTempCByIndex(0));
- sensorWaterOut.requestTemperatures();
- waterOutTemp = (sensorWaterOut.getTempCByIndex(0));
- airInHumidity = sensorAirIn.readHumidity();
- airInTemp = sensorAirIn.readTemperature();
- airOutHumidity = sensorAirOut.readHumidity();
- airOutTemp = sensorAirOut.readTemperature();
- }
- //Write temperatures
- void WriteTemperatures()
- {
- File f = SD.open("logh.txt", FILE_WRITE);
- f.print(millis());
- f.print(",");
- f.print(waterInTemp);
- f.print(",");
- f.print(waterOutTemp);
- f.print(",");
- f.print(airInTemp);
- f.print(",");
- f.print(airOutTemp);
- f.print(",");
- f.print(airInHumidity);
- f.print(",");
- f.println(airOutHumidity);
- f.close();
- }
- //Ethernet
- void Internet()
- {
- char clientline[BUFSIZ];
- int index = 0;
- EthernetClient client = server.available(); // try to get client
- if (client) { // got client?
- boolean currentLineIsBlank = true;
- index = 0;
- while (client.connected()) {
- if (client.available()) { // client data available to read
- char c = client.read();
- // If it isn't a new line, add the character to the buffer
- if (c != '\n' && c != '\r') {
- clientline[index] = c;
- index++;
- // are we too big for the buffer? start tossing out data
- if (index >= BUFSIZ)
- index = BUFSIZ -1;
- // continue to read more data!
- continue;
- }
- // got a \n or \r new line, which means the string is done
- clientline[index] = 0;
- // Print it out for debugging
- Serial.print("clientline : ");
- Serial.println(clientline);
- ///////////////////////
- // If we want to go to ROOT
- ///////////////////////
- if (strstr(clientline, "GET / ") != 0) {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<h2>Please go <a href='index.htm'>here</a></h2>");
- }
- ///////////////////////
- // If we want a specific file
- ///////////////////////
- else if (strstr(clientline, "GET /") != 0) {
- char *filename;
- filename = clientline + 5; // look after the "GET /" (5 chars)
- // a little trick, look for the " HTTP/1.1" string and
- // turn the first character of the substring into a 0 to clear it out.
- (strstr(clientline, " HTTP"))[0] = 0;
- File file = SD.open(filename);
- if (!file) {
- Serial.println("Error : file not found !");
- client.println("HTTP/1.1 404 Not Found");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<h2>File Not Found 1!</h2>");
- file.close();
- break;
- }
- else {
- Serial.println("Opened!");
- client.println("HTTP/1.1 200 OK");
- //Set appropriate mime type based on file extension
- String fname = String(filename);
- int dotat = fname.lastIndexOf('.');
- String extension = String("");
- if(dotat > -1){
- extension = String(fname.substring(dotat+1));
- }
- //client.println("Access-Control-Allow-Origin: *");
- if(extension == "js"){
- client.println("Content-Type: application/x-javascript");
- }
- /*else if (extension == "txt"){
- client.println("Content-Type: text/plain");
- }*/
- else {
- client.println("Content-Type: text/html");
- }
- client.println();
- if (file) {
- while(file.available()) {
- client.write(file.read());
- }
- file.close();
- }
- break;
- }
- }
- ///////////////////////
- // Everything else is a 404
- ///////////////////////
- else {
- // everything else is a 404
- client.println("HTTP/1.1 404 Not Found");
- client.println("Content-Type: text/html");
- client.println();
- client.println("<h2>File Not Found 2!</h2>");
- }
- break;
- } // end "if (client.available())"
- } // end while (client.connected())
- delay(1); // give the web browser time to receive the data
- client.stop(); // close the connection
- } // end "if (client)"
- }
- boolean TempTimer()
- {
- if (millis()-lastTime >= TEMP_DELAY)
- {
- lastTime=millis();
- return true;
- }
- return false;
- }
- // sets every element of str to 0 (clears array)
- void StrClear(char *str, char length)
- {
- for (int i = 0; i < length; i++) {
- str[i] = 0;
- }
- }
- // searches for the string sfind in the string str; returns 1 if string found; return 0 if not found
- char StrContains(char *str, char *sfind)
- {
- char found = 0;
- char index = 0;
- char len;
- len = strlen(str);
- if (strlen(sfind) > len) {
- return 0;
- }
- while (index < len) {
- if (str[index] == sfind[found]) {
- found++;
- if (strlen(sfind) == found) {
- return 1;
- }
- }
- else {
- found = 0;
- }
- index++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement