भेजने और साजिश रचने के साथ Arduino तापमान और आर्द्रता सेंसर (भाग 1)

परियोजना का मुख्य उद्देश्य घर से 11-15 मीटर दूर ग्रीनहाउस में तापमान की निगरानी करना है। बिना यूटीपी के बिछाने और इसे शक्ति प्रदान करने की संभावना के बिना।

ग्रीनहाउस भाग:

  1. प्रो माइक्रो का उपयोग करके DHT-11 सेंसर के साथ तापमान / आर्द्रता लें
  2. प्रो माइक्रो के साथ fs1000a भेजें

घर का हिस्सा:

  1. Uno के साथ mx-rm-5v पर मान स्वीकार करें
  2. ईथरनेट ढाल (WIZNET W5100 HR911105A ) का उपयोग कर सर्वर को भेजें
  3. Mysql डेटाबेस में लिखें
  4. एक ग्राफ़ बनाएं (jpgraph)



उसने थोड़ा आउट ऑफ ऑर्डर किया। मेरे लिए, यह अनुक्रम अधिक जटिल था, इसलिए मैंने इसे पहले लागू करने का निर्णय लिया।

आवश्यक पैकेजों की सूची बहुत बड़ी नहीं है और वितरण पर निर्भर करती है, जैसे कि nginx, php-gd, php-mysql, mysql-server। Jpgraph के लिए, आपको ttf फोंट की आवश्यकता हो सकती है।

नग्नेक्स तैयार करें
server { listen 80; listen [::]:80; root /var/www/html; location / { } location ~ \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_buffer_size 128k; fastcgi_buffers 256 32k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_connect_timeout 1s; fastcgi_ignore_client_abort off; fastcgi_next_upstream timeout; fastcgi_read_timeout 5m; fastcgi_send_timeout 5m; } } 


Uno के लिए, हमें निम्न पुस्तकालयों की आवश्यकता है:
SPI.h
Ethernet.h
DHT.h

यूएनओ के लिए कोड
 #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); } 


स्केच प्रत्येक मिनट स्क्रिप्ट (index.php) के लिए एक GET अनुरोध भेजता है:

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); } 


जो बदले में डेटाबेस के लिए मान भेजता है:

टेबल निर्माण
 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; 



उपयोगकर्ता जोड़ें और अधिकार प्रदान करें
 CREATE USER 'arduino'@''' IDENTIFIED BY ''; 

 GRANT ALL PRIVILEGES ON * . * TO 'arduino'@''; 

 FLUSH PRIVILEGES; 



हम जाँच करते हैं:

 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 | 

डेटाबेस में डेटा!

JpGraph लाइब्रेरी स्थापित करें

इसे / var / www / src में निकालें

अब ग्राफ (day.php) ड्रा करें:

day.php
 <?php // content="text/plain; charset=utf-8" //define('__ROOT__', dirname(dirname())); require_once ('/var/www/src/jpgraph.php'); require_once ('/var/www/src/jpgraph_line.php'); require_once ('/var/www/src/jpgraph_error.php'); require_once ('/var/www/src/jpgraph_date.php'); $x_axis = array(); $y_axis = array(); $i = 0; $con=mysqli_connect('localhost','arduino','','greenhouse'); $result = mysqli_query($con,"SELECT * FROM data"); while($row = mysqli_fetch_array($result)) { $x_axis[$i] = strtotime($row["created_at"]); $y_axis[$i] = $row["temperature"]; $i++; } mysqli_close($con); $start = time(); $end = $start+NDATAPOINTS*SAMPLERATE; for( $i=0; $i < NDATAPOINTS; ++$i ) { $x_axis[$i] = rand(50,70); $xdata[$i] = $start + $i * SAMPLERATE; } $graph = new Graph(1000,400); $graph->img->SetMargin(50,30,30,80); $graph->img->SetAntiAliasing(); $graph->SetScale('datlin'); $graph->xaxis->scale->SetDateFormat('d:m H:i'); $graph->SetShadow(); $graph->title->Set("  "); $graph->xaxis->title->Set(''); $graph->xaxis->SetLabelAngle(45); $graph->yaxis->title->Set(''); $graph->xaxis->scale->SetTimeAlign(DAYADJ_1); $graph->title->SetFont(FF_TIMES,FS_BOLD); // Use 20% "grace" to get slightly larger scale then min/max of // data $graph->yscale->SetGrace(0); $p1 = new LinePlot($y_axis,$x_axis); $p1->mark->SetType(MARK_FILLEDCIRCLE); $p1->mark->SetFillColor("red"); $p1->mark->SetWidth(4); $p1->SetColor("blue"); $p1->SetCenter(); $graph->Add($p1); $graph->Stroke(); 


हमें ऐसी सुंदरता मिलती है:



अभी के लिए बस इतना ही। अब मैं एक रेडियो चैनल पर दो बोर्डों का एक बंडल स्थापित करने पर काम कर रहा हूं, जिसके बारे में मैं दूसरे भाग में लिखूंगा।

Source: https://habr.com/ru/post/hi460491/


All Articles