SlideShare a Scribd company logo
1 of 10
#include <ArduinoJson.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include<DHT.h>
#include <SoftwareSerial.h>
#include <BH1750.h>
#include <Wire.h>
#define sensor A3
const byte Rx= 8;
const byte Tx =9;
SoftwareSerial myserial=SoftwareSerial(Rx,Tx); //Rx-Tx
ThreeWire myWire(13, 12, 3); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
const int DHTPIN = 2; //Đọc dữ liệu từ DHT11 ở chân 2 trên mạch Arduino
const int DHTTYPE = DHT11; //Khai báo loại cảm biến, có 2 loại là DHT11 và DHT2
DHT dht(DHTPIN, DHTTYPE);
BH1750 lightMeter; // cường độ sáng
int bom = 7;
int led = 6;
int quat= 5;
int trang_thai_bom;
int trang_thai_led;
int trang_thai_quat;
int pump_nhan;
int led_nhan;
int quat_nhan;
int che_do;
int nhiet_do_dat;
int do_am_dat_dat;
float do_am_thuc; //a
float nhiet_do_thuc; //b
float lux_thuc; //c
float do_am_dat_thuc;//d
String DataSend= "";
String Dataesp = "";
void Json(String thamso1, String thamso2, String thamso3, String thamso4 , String
thamso5,String thamso6);
void xulyjson(String Data);
void chuongtrinhcambien(void);
void read_uno(void);
long t = 0;
// ham khoi tao
void setup() {
// khởi tạo cổng uart chính thống
myserial.begin(9600);// khởi tạo cổng uart => tx,rx(0,1) tốc độ truyền 9600
Serial.begin(9600);
Wire.begin(); // cường độ sáng SCL and SDA pins using a5 ,a4
dht.begin(); // Khởi động cảm biến
lightMeter.begin();
t= millis();
pinMode(bom, OUTPUT);
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
pinMode(quat, OUTPUT);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
}
//////////////////////ham chinh
void loop() {
//
RtcDateTime now = Rtc.GetDateTime();
int gio = now.Hour();
//Serial.println(gio);
// truyền dữ liệu
if(che_do==0)
{
if( pump_nhan == 1)
{
digitalWrite(bom, LOW);
trang_thai_bom=1;
}
if( pump_nhan == 0 )
{
digitalWrite(bom, HIGH);
trang_thai_bom=0;
}
if( led_nhan==0)
{
digitalWrite(led, HIGH);
trang_thai_led=0;
}
if(led_nhan==1 )
{
digitalWrite(led, LOW);
trang_thai_led=1;
}
if( quat_nhan==1)
{
// bật quat
digitalWrite(quat, HIGH);
trang_thai_quat=1;
}
if( quat_nhan==0 )
{
// tắt quat
digitalWrite(quat, LOW);
trang_thai_quat=0;
}
///////////
}
if(che_do==1)
{
digitalWrite(led,HIGH);
trang_thai_led=0;
if( lux_thuc >= 5000 && gio >=5 && gio <=18 )
{
digitalWrite(led,HIGH);
trang_thai_led=0;
}
if( lux_thuc <= 5000&& gio >=5 && gio <=18 )
{
digitalWrite(led, LOW);
trang_thai_led=1;
}
///////////
if( do_am_dat_thuc <= do_am_dat_dat)
{
// bật bơm
digitalWrite(bom, LOW);
trang_thai_bom=1;
}
if( do_am_dat_thuc >= do_am_dat_dat )
{
// tắt bơm
digitalWrite(bom, HIGH);
trang_thai_bom=0;
}
if( nhiet_do_thuc>= nhiet_do_dat)
{
// bật quat
digitalWrite(quat, HIGH);
trang_thai_quat=1;
}
if( nhiet_do_thuc<= nhiet_do_dat )
{
// tắt quat
digitalWrite(quat, LOW );
trang_thai_quat=0;
}
}
if(millis()-t >= 1500)
{
sendata();
t= millis();
}
read_uno();
}
/////////////////////////////
void sendata(void)
{
chuongtrinhcambien();
Json(String(do_am_thuc) , String(nhiet_do_thuc) , String(lux_thuc) , String
(do_am_dat_thuc) , String(trang_thai_bom), String(trang_thai_led), String (trang_thai_quat));
//myserial.flush();
}
/////////////////hàm gửi dữ liệu
void Json(String thamso1, String thamso2, String thamso3, String thamso4 , String thamso5,
String thamso6, String thamso7)
{
DataSend = "";
// cấu trúc của json {"A":""gia tri cua A"", "B":"gia tri cua B", ...................}
DataSend = "{"a":"" + String(thamso1) + "", " +
""b":"" + String(thamso2) + "", " +
""d":"" + String(thamso3) + "", " +
""e":"" + String(thamso5) + "", " +
""f":"" + String(thamso6) + "", " +
""g":"" + String(thamso7) + "", " +
""c":"" + String(thamso4) + ""} " ;
// gửi dữ liệu về phần mềm
myserial.println(DataSend);
Serial.println(DataSend);
myserial.flush();
}
/////////////// ngat uart
void read_uno(void)
{
// kiểm tra cổng RX có tín hiệu hay ko
while (myserial.available())
{
//đọc
char InChar = (char)myserial.read();
if(InChar !='n')
{
Dataesp += InChar;
}
else
{
// xử lí Json
xulyjson(String(Dataesp));
myserial.println(Dataesp);
Serial.println(Dataesp);
myserial.flush();
Dataesp = "";
}
}
}
void xulyjson(String Data)
{
const size_t capacity = JSON_OBJECT_SIZE(2) + 256;
DynamicJsonDocument JSON(capacity);
DeserializationError error = deserializeJson( JSON, Data);
if(error)
{
Serial.println("json loi");
return;
}
else
{
String t= JSON["T"];
nhiet_do_dat=t.toInt();
String cd= JSON["chedo"];
che_do=cd.toInt();
String h= JSON["H"];
do_am_dat_dat=h.toInt();
sendata();
if(JSON["pump"] == "0")
{
pump_nhan=0;
trang_thai_bom=0;
sendata();
}
if(JSON["pump"] == "1")
{
pump_nhan=1;
trang_thai_bom=1;
sendata();
}
if(JSON["led"] == "0")
{
led_nhan=0;
trang_thai_led=0;
sendata();
}
if(JSON["led"] == "1")
{
led_nhan=1;
trang_thai_led=1;
sendata();
}
if(JSON["quat"] == "0")
{
quat_nhan=0;
trang_thai_quat=0;
sendata();
}
if(JSON["quat"] == "1")
{
quat_nhan=1;
trang_thai_quat=1;
sendata();
}
JSON.clear();
}
}
/////////////////////////////////////
void chuongtrinhcambien(void)
{
do_am_thuc = dht.readHumidity(); //Đọc độ ẩm
nhiet_do_thuc = dht.readTemperature(); //Đọc nhiệt độ
do_am_dat_thuc = analogRead(sensor); // độ ẩm đất
do_am_dat_thuc = map(do_am_dat_thuc, 0, 1023, 100, 0);
lux_thuc = lightMeter.readLightLevel(); // cường độ sáng
}
////////////////////////////////////
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt)
{
char datestring[50];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u %02u/%02u/%04u %02u:%02u:%02u"),
dt.DayOfWeek(),
dt.Day(),
dt.Month(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.println(datestring);
}

More Related Content

Featured

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 

code-arduino-uno-R3.docx

  • 1. #include <ArduinoJson.h> #include <ThreeWire.h> #include <RtcDS1302.h> #include<DHT.h> #include <SoftwareSerial.h> #include <BH1750.h> #include <Wire.h> #define sensor A3 const byte Rx= 8; const byte Tx =9; SoftwareSerial myserial=SoftwareSerial(Rx,Tx); //Rx-Tx ThreeWire myWire(13, 12, 3); // IO, SCLK, CE RtcDS1302<ThreeWire> Rtc(myWire); const int DHTPIN = 2; //Đọc dữ liệu từ DHT11 ở chân 2 trên mạch Arduino const int DHTTYPE = DHT11; //Khai báo loại cảm biến, có 2 loại là DHT11 và DHT2 DHT dht(DHTPIN, DHTTYPE); BH1750 lightMeter; // cường độ sáng int bom = 7; int led = 6; int quat= 5; int trang_thai_bom; int trang_thai_led; int trang_thai_quat; int pump_nhan; int led_nhan; int quat_nhan; int che_do; int nhiet_do_dat; int do_am_dat_dat; float do_am_thuc; //a float nhiet_do_thuc; //b
  • 2. float lux_thuc; //c float do_am_dat_thuc;//d String DataSend= ""; String Dataesp = ""; void Json(String thamso1, String thamso2, String thamso3, String thamso4 , String thamso5,String thamso6); void xulyjson(String Data); void chuongtrinhcambien(void); void read_uno(void); long t = 0; // ham khoi tao void setup() { // khởi tạo cổng uart chính thống myserial.begin(9600);// khởi tạo cổng uart => tx,rx(0,1) tốc độ truyền 9600 Serial.begin(9600); Wire.begin(); // cường độ sáng SCL and SDA pins using a5 ,a4 dht.begin(); // Khởi động cảm biến lightMeter.begin(); t= millis(); pinMode(bom, OUTPUT); pinMode(led, OUTPUT); pinMode(sensor, INPUT); pinMode(quat, OUTPUT); Serial.print("compiled: "); Serial.print(__DATE__); Serial.println(__TIME__); Rtc.Begin(); RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); printDateTime(compiled); Serial.println(); } //////////////////////ham chinh void loop() {
  • 3. // RtcDateTime now = Rtc.GetDateTime(); int gio = now.Hour(); //Serial.println(gio); // truyền dữ liệu if(che_do==0) { if( pump_nhan == 1) { digitalWrite(bom, LOW); trang_thai_bom=1; } if( pump_nhan == 0 ) { digitalWrite(bom, HIGH); trang_thai_bom=0; } if( led_nhan==0) { digitalWrite(led, HIGH); trang_thai_led=0; } if(led_nhan==1 ) { digitalWrite(led, LOW);
  • 4. trang_thai_led=1; } if( quat_nhan==1) { // bật quat digitalWrite(quat, HIGH); trang_thai_quat=1; } if( quat_nhan==0 ) { // tắt quat digitalWrite(quat, LOW); trang_thai_quat=0; } /////////// } if(che_do==1) { digitalWrite(led,HIGH); trang_thai_led=0; if( lux_thuc >= 5000 && gio >=5 && gio <=18 ) { digitalWrite(led,HIGH); trang_thai_led=0; } if( lux_thuc <= 5000&& gio >=5 && gio <=18 )
  • 5. { digitalWrite(led, LOW); trang_thai_led=1; } /////////// if( do_am_dat_thuc <= do_am_dat_dat) { // bật bơm digitalWrite(bom, LOW); trang_thai_bom=1; } if( do_am_dat_thuc >= do_am_dat_dat ) { // tắt bơm digitalWrite(bom, HIGH); trang_thai_bom=0; } if( nhiet_do_thuc>= nhiet_do_dat) { // bật quat digitalWrite(quat, HIGH); trang_thai_quat=1; } if( nhiet_do_thuc<= nhiet_do_dat ) { // tắt quat digitalWrite(quat, LOW ); trang_thai_quat=0;
  • 6. } } if(millis()-t >= 1500) { sendata(); t= millis(); } read_uno(); } ///////////////////////////// void sendata(void) { chuongtrinhcambien(); Json(String(do_am_thuc) , String(nhiet_do_thuc) , String(lux_thuc) , String (do_am_dat_thuc) , String(trang_thai_bom), String(trang_thai_led), String (trang_thai_quat)); //myserial.flush(); } /////////////////hàm gửi dữ liệu void Json(String thamso1, String thamso2, String thamso3, String thamso4 , String thamso5, String thamso6, String thamso7) { DataSend = ""; // cấu trúc của json {"A":""gia tri cua A"", "B":"gia tri cua B", ...................} DataSend = "{"a":"" + String(thamso1) + "", " + ""b":"" + String(thamso2) + "", " + ""d":"" + String(thamso3) + "", " + ""e":"" + String(thamso5) + "", " + ""f":"" + String(thamso6) + "", " + ""g":"" + String(thamso7) + "", " + ""c":"" + String(thamso4) + ""} " ;
  • 7. // gửi dữ liệu về phần mềm myserial.println(DataSend); Serial.println(DataSend); myserial.flush(); } /////////////// ngat uart void read_uno(void) { // kiểm tra cổng RX có tín hiệu hay ko while (myserial.available()) { //đọc char InChar = (char)myserial.read(); if(InChar !='n') { Dataesp += InChar; } else { // xử lí Json xulyjson(String(Dataesp)); myserial.println(Dataesp); Serial.println(Dataesp); myserial.flush(); Dataesp = ""; } }
  • 8. } void xulyjson(String Data) { const size_t capacity = JSON_OBJECT_SIZE(2) + 256; DynamicJsonDocument JSON(capacity); DeserializationError error = deserializeJson( JSON, Data); if(error) { Serial.println("json loi"); return; } else { String t= JSON["T"]; nhiet_do_dat=t.toInt(); String cd= JSON["chedo"]; che_do=cd.toInt(); String h= JSON["H"]; do_am_dat_dat=h.toInt(); sendata(); if(JSON["pump"] == "0") { pump_nhan=0; trang_thai_bom=0; sendata(); } if(JSON["pump"] == "1") { pump_nhan=1; trang_thai_bom=1; sendata(); }
  • 9. if(JSON["led"] == "0") { led_nhan=0; trang_thai_led=0; sendata(); } if(JSON["led"] == "1") { led_nhan=1; trang_thai_led=1; sendata(); } if(JSON["quat"] == "0") { quat_nhan=0; trang_thai_quat=0; sendata(); } if(JSON["quat"] == "1") { quat_nhan=1; trang_thai_quat=1; sendata(); } JSON.clear(); } } ///////////////////////////////////// void chuongtrinhcambien(void) { do_am_thuc = dht.readHumidity(); //Đọc độ ẩm nhiet_do_thuc = dht.readTemperature(); //Đọc nhiệt độ
  • 10. do_am_dat_thuc = analogRead(sensor); // độ ẩm đất do_am_dat_thuc = map(do_am_dat_thuc, 0, 1023, 100, 0); lux_thuc = lightMeter.readLightLevel(); // cường độ sáng } //////////////////////////////////// #define countof(a) (sizeof(a) / sizeof(a[0])) void printDateTime(const RtcDateTime& dt) { char datestring[50]; snprintf_P(datestring, countof(datestring), PSTR("%02u %02u/%02u/%04u %02u:%02u:%02u"), dt.DayOfWeek(), dt.Day(), dt.Month(), dt.Year(), dt.Hour(), dt.Minute(), dt.Second() ); Serial.println(datestring); }