Save data to an SD card with the Ethernet Shield
FunduinoPublished on
Messwerte auf SD-Karte speichern und im Browser anzeigen
You need this
W5100 Ethernet Shield - RJ45, 10 Mbit/s, for Arduino
18,90 € *Item no. F23106788About the product — W5100 Ethernet Shield - RJ45, 10 Mbit/s, for Arduino
DHT11 temperature sensor - humidity, 3.3-5.5 V, without breakout board
2,32 € *Item no. F23106840About the product — DHT11 temperature sensor - humidity, 3.3-5.5 V, without breakout board
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm
1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
All 4 parts together 34,08 € *Ready to ship immediately · Delivery time: 1-3 business days
* All prices include statutory VAT, plus shipping costs.
Required components
Prepare the SD card
Project 1: Store a sequence of numbers
Save sequences of numbers to the SD card
#include <SD.h> //Add the SD library
int a=0; // Variable for a counting operation
int b=0; // Variable for a counting operation
const int chipSelect = 4; //Chip pin for the SD card (4 on UNO, 53 on MEGA)
void setup() {
pinMode (13, OUTPUT);
if (startSDCard() == true) //The "startSDCard" function block is used to check the SD card. If the SD card can be read, the onboard LED on pin 13 should blink twice.
{
digitalWrite(13, HIGH); //on
delay(500);
digitalWrite(13, LOW); //off
delay(500);
digitalWrite(13, HIGH); //on
delay(500);
digitalWrite(13, LOW); //off
delay(500);
}
}
void loop()
{
File dataFile = SD.open("zaehlen.csv", FILE_WRITE); //Create a file on the SD card named "zaehlen"
a=a+1; //The value a+1 is now stored in the variable "a". This increases the value of "a" by 1 on each iteration.
b=b+2; //The value b+2 is now stored in the variable "b". This increases the value of "b" by 2 on each iteration.
dataFile.print(a); //The value of "a" is saved to the SD card
dataFile.print(";"); //A semicolon is saved in the CSV file, allowing the values to be displayed separately in a table later.
dataFile.println(b); //The value of "b" is saved to the SD card
dataFile.close(); //The file is temporarily closed.
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500); //Here the loop ends and then starts again from the beginning. The values of "a" and "b" are saved to the table on the SD card every second.
}
boolean startSDCard() //This function block is needed to check whether the SD card is ready.
{
boolean result = false;
pinMode(4, OUTPUT); //4 on UNO, change to 53 on MEGA
if (!SD.begin(chipSelect)) //Check whether the SD card can be read
{
result = false;
}
else //If so, create a file as in the loop
{
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile)
{
dataFile.close();
result = true;
}
}
return result;
}Evaluate the result
Project 2: Connecting the DHT11
Required libraries
Save measured values
Save DHT11 readings to the SD card
#include "SD.h" //SD Library hinzufügen
#include "DHT.h" //DHT Library hinzufügen
#define DHTPIN 2 //Pin an dem der DHT angeschlossen ist festlegen
#define DHTTYPE DHT11 //DHT Typ festlegen: Hier DHT11
#define TEMPERATURE 1
#define HUMIDITY 0
int feuchte;
int temperatur;
DHT dht(DHTPIN, DHTTYPE); //Sensor initializieren
const int chipSelect = 4; //Chip Pin für die SD Karte(bei UNO 4,bei MEGA 53)
void setup() {
if (startSDCard() == true) // Durch den Rückgriff auf den Programmblock "startSDCard" wird die SD-Karte geprüft. Wenn die SD Karte gelesen werden kann dann soll die onboard-LED an Pin13 zweimal blinken
{
digitalWrite(13, HIGH); //an
delay(500);
digitalWrite(13, LOW); //aus
delay(500);
digitalWrite(13, HIGH); //an
delay(500);
digitalWrite(13, LOW); //aus
delay(500);
}
Serial.begin(9600); //Serielle Verbindung starten
dht.begin(); //DHT Sensor starten
}
void loop()
{
File dataFile = SD.open("werte.csv", FILE_WRITE); //Excel Datei mit dem Namen „werte“ auf der SD-Karte anlegen
feuchte = dht.readHumidity(); //Feuchtigkeit auslesen
temperatur = dht.readTemperature(); //Temperatur auslesen
dataFile.print(feuchte); //Feuchtigkeit in die Excel Datei eintragen
dataFile.print(";"); // Es wird ein Semikolon in die CSV-Datei gespeichert, daduch lassen sich die Werte später als Tabelle getrennt darstellen.
dataFile.println(temperatur); //Temperatur in die Excel Datei eintragen. Durch den Befehl mit der Endung „ln“ („LN“ in Kleinbuchstaben) wird ein Zeilenumsprung in der Wertetabelle erzeugt.
dataFile.close(); // Die Datei wird vorrübergehend geschlossen.
delay(1000);
Serial.print("Luftfeuchtigkeit: "); //Im seriellen Monitor den Text und
Serial.print(feuchte); //die Dazugehörigen Werte anzeigen
Serial.println(" %");
Serial.print("Temperatur: ");
Serial.print(temperatur);
Serial.println(" Grad Celsius");
}
boolean startSDCard() {
boolean result = false;
pinMode(4, OUTPUT); // 4 bei UNO, bei MEGA in 53 ändern
if (!SD.begin(chipSelect)) { //Überprüfen ob die SD Karte gelesen werden kann
result = false;
}
else { // Wenn ja Datei wie im Loop anlegen
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.close();
result = true;
}
}
return result;
}
float readSensor( int thisValue) {
float result;
if (thisValue == TEMPERATURE) {
result = dht.readTemperature(); //Sensorwert auslesen und unter Temperature speichern
}
else if (thisValue == HUMIDITY) //Sensorwert auslesen und unter Humidity speichern
{
result = dht.readHumidity();
}
if (isnan(result)) // Die Library des DHT Sensors sendet beim Verlust des Signals vom DHT11 den Befehl „isnan“. Die passiert bspw. wenn das Kabel der Datenleitung zum Sensor den Kontakt verliert. In dem Fall soll als „Messwert“ die Zahl -273 angezeigt und gespeichert werden. Da dies der tiefste zu erreichende Punkt ist, dürfte jedem klar sein, dass mit den Werten etwas nicht stimmt, und dass der Aufbau überprüft werden muss.
{
result = -273.0;
}
return result;
}Analyze measurement data
Project 3: The Ethernet Shield as a Web Server
Set the IP address
The thermometer in the browser
1 / 2
The Ethernet shield as a web server thermometer
// Loading the required library
#include "Ethernet.h"
int TMP36 = A3; // Variable declaration
int sensorwert;
int temperatur = 0;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
}; // MAC address of the Arduino board !IMPORTANT! MAC address may only appear once on the home network
// statischeIP = false -> Assign IP address via DHCP
// statischeIP = true -> Set a static IP address
bool statischeIP = false;
// static IP
IPAddress ip(192, 168, 1, 200);
EthernetServer server(80); // Port setting (standard for HTML: 80)
void setup()
{
Serial.begin(9600);
// Start Ethernet with a static IP
if (statischeIP) Ethernet.begin(mac, ip);
// Start Ethernet with DHCP
else Ethernet.begin(mac);
server.begin(); // Wait for clients
// Display the IP address in the Serial Monitor for verification
// localIP -> address to enter in the browser
Serial.print(F("IP address of the Ethernet shield: "));
Serial.println(Ethernet.localIP());
}
void loop()
{
sensorwert = analogRead(TMP36); //Read the sensor value on pin A3
temperatur = map(sensorwert, 0, 410, -50, 150); // Convert the sensor value using the "map" command
delay(500);
EthernetClient client = server.available(); //Check whether a client is accessing the page
if (client) { //Page accessed by user
// Output in HTML
server.print("HTTP/1.0 200 OK\r\nServer: arduino\r\nContent-Type: text/html\r\n\r\n");
server.print("<HTML><HEAD><TITLE>");
server.print("Funduino Thermometer");
server.print("</TITLE>");
server.print("</HEAD><BODY>");
server.print("<h2 align=center><font size=7><b>Funduino Thermometer </b></font></h2>");
server.print("<center><font size=5><b>The temperature is </font>");
server.print(temperatur); // Output the temperature
server.print("<font size=5> degrees Celsius. </font");
delay(500); // Brief delay to send the data
client.stop(); // Disconnect from the client
}
}Rate the instructions