Stap 2: Software voor de Arduino
/* E-Pot v.1.0 ------------------------ Author: Jan Pedryc E-mail: jan.pedryc Date: 23.11.2015 For more information about this project check http://www.janped.com ------------------------ This is the code for the E-Pot project. The main purposes of this code are: 1. Establishing connection - ethernet shield 2. Configuration of the humidity module DHT11 3. Configuration of the photoresistor LOOP: (approximately once every hour) a) Gaining data from the sensors b) Preparing a string (data) for the _POST request c) Sending the data using the gateway to its destination */
#include <SPI.h> #include <Ethernet.h> #include <DHT.h>
// ---------------------------------------------- Web Server CONFIG // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,0,177); IPAddress gateway(192,168,0,1); IPAddress subnet(255,255,255,0);
EthernetClient client;
// ---------------------------------------------- Humidity Module CONFIG #define DHTPIN 2 // What pin we're connected to #define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// ----------------------------------------- Photoresistor CONFIG int lightPin = 0; // Variable stores the value from the photoresistor int PRvalue = 0; // Additional variable storing the value to compare int PRlight = 0; // Stands for the light presence (ON/OFF)
String data; // This string will contain the prepared data // which we will send as a POST request
void setup() { // Open serial communications and wait for port to open: Serial.begin(115200); /* For Arduino Leonardo users - uncomment this part: */ // while (!Serial) { // ; // } Serial.println("Serial connection established"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); } dht.begin(); Serial.println("DHT11 connection established"); data = ""; }
void loop() { delay(1000); // ------------------ Humidity Module ACTION Serial.println("Humidity Module ACTION"); // Wait a few seconds between measurements. int t = 0; // temperature int h = 0; // humidity // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' // (its a very slow sensor) h = dht.readHumidity(); delay(500); // Read temperature as Celsius (the default) t = dht.readTemperature(); delay(500);
// Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // ------------------ Photo Resistor ACTION PRvalue = analogRead(lightPin); // 500 is optional in this case. You should test the // photoresistor in your environment and decide how // sensitive the sensor should be if (PRvalue>500) { PRlight = 1; // 1 means in my case that there is enough light } else { PRlight = 0; // 0 means there is unefficient light } // The Serial.print & Serial.println commands are optional // (I used them for testing purposes) Serial.print("Temp: "); Serial.println(t); Serial.print("Hum: "); Serial.println(h); Serial.print("Light: "); Serial.println(PRlight); // Preparing the data from the sensors to send via the // ethernet shield data = "ahum1=" + String(h) + "&temp1=" + String(t) + "&light=" + String(PRlight); Serial.println(data); // For testing purposes // ------------------ Ethernet Shield ACTION if (client.connect("www.janped.com", 80)) { // Your domain Serial.println("Client connected."); client.println("POST /add.php HTTP/1.1"); client.println("Host: janped.com"); // Your domain client.println("Content-Type: application/x-www-form-urlencoded"); client.println("Connection: close"); client.println("User-Agent: Arduino/1.0"); client.print("Content-Length: "); client.println(data.length()); client.println(); client.println(data); } if (client.connected()) { client.stop(); } // Now wait approximately one hour // 1000ms * 60 = 60 000ms = 1 min // 60 000ms * 60 = 3 600 000ms = 1 h // The delay() function should know it's a long int, // that's why there is a 'L' included at the end delay(3600000L); }
Ik heb geprobeerd om commentaar alles waar eventuele problemen kunnen optreden. Als iets niet duidelijk of de code werkt niet zoals het hoort, neem contact met mij op geen enkele manier.
Kunt u een meer gedetailleerde beschrijving van de code op