El objetivo principal del proyecto es controlar la temperatura en el invernadero a 11-15 metros de la casa. Sin la posibilidad de organizar la colocaci贸n de UTP y el poder para ello.
Parte de invernadero:
- Tome la temperatura / humedad con el sensor DHT-11 usando pro micro
- Env铆e fs1000a con pro micro
Parte de la casa:
- Acepte valores en mx-rm-5v con Uno
- Enviar al servidor utilizando el escudo de Ethernet (WIZNET W5100
HR911105A ) - Escribir en la base de datos mysql
- Dibujar un gr谩fico (jpgraph)

Comenz贸 un poco fuera de servicio. Para m铆, esta secuencia fue m谩s complicada, as铆 que decid铆 implementarla primero.
La lista de paquetes requeridos no es enorme y depende de la distribuci贸n, es decir, nginx, php-gd, php-mysql, mysql-server. Para jpgraph, es posible que necesite fuentes ttf.
Preparar nginxserver { listen 80; listen [::]:80; root /var/www/html; location / { } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Para uno, necesitamos las siguientes bibliotecas:
SPI.h
Ethernet.h
DHT.h
C贸digo para la ONU #include <SPI.h> #include <Ethernet.h> #include <DHT.h> // MAC address for controller byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // byte ip[] = { 192, 168, 156, 192 }; // byte subnet[] = { 255, 255, 255, 0 }; // byte gateway[] = { 192, 168, 0, 1 }; // char server[] = "192.168.156.186"; #define DHTPIN 2 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); // EthernetClient client; void setup() { dht.begin(); Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect } { Ethernet.begin(mac,ip,gateway,subnet); } // give the Ethernet shield a second to initialize delay(1000); } void loop() { if (client.connect(server, 80)) { // give the Ethernet shield a second to initialize float h = dht.readHumidity(); float t = dht.readTemperature(); Serial.println("connected"); // GET client.println(String("GET /index.php?temp=")+ t +"&hum="+h); client.println(); Serial.println("connection close"); } else Serial.println("connection failed"); delay(60000); }
El boceto env铆a una solicitud GET al script (index.php) cada minuto:
index.php <?php /** * @param string $message * @param PDOException|null $exception */ function writeMessage($message, $exception = null) { $logfile = '/var/www/html/data/arduino.log'; $datetime = date('dmY H:i:s', time()); if ($exception !== null) { $message .= ': ' . $exception->getFile() . ' (line: ' . $exception->getLine() . ') - ' . $exception->getMessage(); } file_put_contents($logfile, '[' . $datetime . '] ' . $message."\n", FILE_APPEND); } /* MySQL */ $dsn = 'mysql:dbname=greenhouse;host=localhost'; $user = 'arduino'; $password = ''; try { $dbh = new PDO($dsn, $user, $password, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', ]); } catch (PDOException $e) { writeMessage(' ', $e); } try { $sth = $dbh->prepare('INSERT INTO data (sensor, temperature, humidity, created_at) VALUES (?, ?, ?, NOW())'); $sth->execute(['grass', $_REQUEST['temp'], $_REQUEST['hum']]); } catch (PDOException $e) { writeMessage(' ', $e); }
que a su vez env铆a valores a la base de datos:
Creaci贸n de tabla CREATE DATABASE `greenhouse` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `table` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sensor` varchar(255) DEFAULT NULL, `temperature` decimal(6,3) DEFAULT '0.00', `humidity` decimal(6,3) DEFAULT '0.00', `created_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
Agregar usuario y otorgar derechos CREATE USER 'arduino'@''' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON * . * TO 'arduino'@'';
FLUSH PRIVILEGES;
Comprobamos:
select * from data;
+-----+--------+-------------+----------+---------------------+ | id | sensor | temperature | humidity | created_at | +-----+--------+-------------+----------+---------------------+ | 1 | grass | NULL | NULL | 2019-07-15 13:29:13 | | 2 | grass | 24.100 | 49.000 | 2019-07-15 13:44:44 | | 3 | grass | 24.100 | 49.000 | 2019-07-15 13:44:54 | | 4 | grass | 24.000 | 49.000 | 2019-07-15 13:45:04 | | 5 | grass | 24.000 | 49.000 | 2019-07-15 13:45:15 | | 6 | grass | 24.100 | 49.000 | 2019-07-15 13:45:25 | | 7 | grass | 24.100 | 49.000 | 2019-07-15 13:45:35 | | 8 | grass | 24.100 | 49.000 | 2019-07-15 13:45:45 | | 9 | grass | 24.100 | 49.000 | 2019-07-15 13:45:55 | | 10 | grass | 24.100 | 48.000 | 2019-07-15 13:46:47 | | 11 | grass | 24.100 | 48.000 | 2019-07-15 13:46:58 | | 12 | grass | 24.100 | 48.000 | 2019-07-15 13:47:08 |
Datos en la base de datos!
Instale la biblioteca
JpGrapheliminarlo en / var / www / src
Ahora dibuja el gr谩fico (day.php):
Obtenemos tanta belleza:

Eso es todo por ahora. Ahora estoy trabajando en configurar un paquete de dos tableros en un canal de radio, sobre el que escribir茅 en la segunda parte.