1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Project request sensor data via SMS (SMS với cảm biến nhiệt độ và độ ẩm)

21 59 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Project : Request Sensor Data via SMS (SMS với cảm biến nhiệt độ độ ẩm) ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ Trong dự án này, cho bạn cách yêu cầu liệu cảm biến qua SMS với Arduino Như ví dụ, chúng tơi u cầu nhiệt độ độ ẩm từ cảm biến DHT11 Để gửi nhận SMS với Arduino, sử dụng SIM900 GSM shield Khi bạn gửi tin nhắn SMS đến Arduino với thơng báo “STATE”, trả lời đọc nhiệt độ độ ẩm Trước tiếp tục với hướng dẫn này, khuyên bạn nên đọc tài liẹu sau: • Hướng dẫn SIM900 GSM GPRS Shield với Arduino • Hướng dẫn cảm biến độ ẩm nhiệt độ DHT11 / DHT22 với Arduino Watch the video demonstration SIM900 GSM Shield Có số mơ-đun bạn sử dụng để gửi nhận SMS với Arduino Chúng thực dự án cách sử dụng SIM900 GSM shield SIM900 GSM hiển thị hình đây: ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ Để biết giới thiệu khiên GSM cách thiết lập, đọc Guide to SIM900 GSM GPRS Shield with Arduino Parts Required • • • • • • • • • Arduino UNO – read Best Arduino Starter Kits SIM900 GSM Shield 5V 2A Power Adaptor FTDI programmer (optional) SIM Card DHT11 or DHT22 Temperature and Humidity Sensor 10 kOhm resistor Breadboard Jumper Wires Bước sơ Trước bắt đầu với mơ-đun GSM SIM900, bạn cần tìm hiểu thẻ SIM nguồn cung cấp điện cho Thẻ SIM trả trước Chúng khuyên bạn nên sử dụng gói trả trước gói có SMS khơng giới hạn cho mục đích thử nghiệm Mặt khác, có cố xảy ra, bạn cần phải trả hóa đơn lớn cho hàng trăm tin nhắn văn SMS gửi nhầm lẫn Trong hướng dẫn này, chúng tơi sử dụng gói trả trước với SMS không giới hạn ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ Cài đặt:Settings Advanced Settings Security SIM lock Cung cấp lượng phù hợp Module SIM có giắc DC cho nguồn điện hình bên Bên cạnh giắc nguồn có công tắc bật tắt để chọn nguồn điện Bên cạnh cơng tắc bật tắt bảng, có mũi tên vị trí bật tắt để sử dụng nguồn điện bên ngồi - di chuyển cơng tắc bật tắt để sử dụng nguồn điện bên ngồi hình Nên sử dụng nguồn điện 5V cung cấp 2A hình ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ SMS.Hãy chắn nguồn cung cấp 5V 2A Thiết lập SIM900 GSM Shield Các bước sau cho bạn biết cách thiết lập module SIM900 GSM 1) Lắp thẻ SIM vào ngăn chứa thẻ SIM Bạn cần thẻ SIM với kích thước tiêu chuẩn Module SIM khơng tương thích với thẻ micro nano SIM Nếu bạn cần, bạn nhận chuyển đổi kích thước thẻ sim Ngồi ra, nên sử dụng thẻ SIM với gói trả trước SMS không giới hạn 2) Kiểm tra ăng-ten kết nối tốt 3) Trên cổng serial chọn, đảm bảo jumper cap kết nối thể hình bên để sử dụng nối tiếp phần mềm ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 4) Cấp nguồn cho module SIM nguồn điện 5V bên 5) Để bật / tắt nguồn module SIM, nhấn phím nguồn khoảng giây 6) Sau đó, đèn Status LED sáng lên đèn LED NetLight nhấp nháy sau 800 ms tìm thấy mạng Khi tìm thấy mạng, đèn LED NetLight bắt đầu nhấp nháy sau ba giây ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 7) Bạn kiểm tra xem module SIM có hoạt động tốt hay không cách gửi lệnh AT từ Arduino IDE cách sử dụng FTDI programmer - hình Testing the Shield with FTDI Programmer Bạn không cần phải làm bước để module SIM hoạt động bình thường Đây bước bổ sung để đảm bảo bạn giao tiếp với module GSM gửi lệnh AT từ Arduino IDE serial monitor Vì thế, bạn cần FTDI programmer người hình bên 1) Kết nối FTDI programmerI với module GSM hình bên ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 2) Open the Arduino IDE and select the right COM port 3) Open the Serial monitor 4) Select 19200 baud rate – the shield default setting is 19200 – and Carriage return Write AT at the box highlighted in red and then press enter See figure below ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 5) Module phản hồi với OK, thứ hoạt động tốt Bây sẵn sàng để bắt đầu xây dựng dự án Sơ đồ Hình cho thấy sơ đồ mạch cho dự án Bạn phải kết nối module SIM900 GSM cảm biến nhiệt độ độ ẩm DHT11 với Arduino hình ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ Cài đặt thư viện DHT Tải thư viện cảm biến DHT Bạn nên có thư mục zip thư mục Downloads bạn Giải nén thư mục zip bạn nhận thư mục DHT-sensor-library-master Đổi tên thư mục bạn từ DHT-sensor-library-master thành DHT Di chuyển thư mục DHT vào thư mục thư viện cài đặt Arduino IDE bạn Cuối cùng, mở lại Arduino IDE bạn Installing the Adafruit_Sensor library Để sử dụng cảm biến nhiệt độ độ ẩm DHT, bạn cần cài đặt Installing the Adafruit_Sensor library Thực theo bước để cài đặt thư viện Arduino IDE bạn: Tải xuống thư viện Installing the Adafruit_Sensor library Bạn nên có thư mục zip thư mục Downloads bạn Giải nén thư mục zip bạn nhận thư mục Adaf nhung_Sensor-master Đổi tên thư mục bạn từ Adaf nhung_Sensor-master thành Adaf nhung_Sensor Di chuyển thư mục Adaf nhung_Sensor vào thư mục thư viện cài đặt Arduino IDE bạn Cuối cùng, mở lại Arduino IDE bạn ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 10 Code /* * Rui Santos * Complete Project Details http://randomnerdtutorials.com */ // Include DHT library and Adafruit Sensor Library #include "DHT.h" #include //Include Software Serial library to communicate with GSM #include // Pin DHT is connected to #define DHTPIN // Uncomment whatever type of sensor you're using #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 //#define DHTTYPE DHT21 (AM2302) // DHT 21 (AM2301) // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); // Create global varibales to store temperature and humidity float t; // temperature in celcius float f; // temperature in fahrenheit float h; // humidity // Configure software serial port SoftwareSerial SIM900(7, 8);// TX,RX Sim nối với chân 7, Arduino UNO ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 11 // Create variable to store incoming SMS characters char incomingChar; void setup() { dht.be gin(); Serial.begin(19 200); SIM900.begin(1 9200); // Give time to your GSM shield log on to network delay(20000); Serial.print("SIM900 ready "); // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); // Set module to send SMS data to serial out upon receipt SIM900.print("AT+CNMI=2,2,0,0,0\r"); delay(100); } void loop(){ if (SMSRequ est()){ if(readData ()){ delay(10); // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 12 // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS SIM900.println("AT + CMGS = \"+XXXXXXXXXX\""); delay(100); // REPLACE WITH YOUR OWN SMS MESSAGE CONTENT String dataMessage = ("Temperature: " + String(t) + "*C " + " Humidity: " + String(h) + "%"); // Uncomment to change message with farenheit temperature // String " dataMessage = ("Temperature: " + String(f) + " Humidity: " + String(h) + "%"); + "*F // Send the SMS text message SIM900.print(dataMessa ge); delay(100); // End AT command with a ^Z, ASCII code 26 SIM900.println((char)26); delay(100); SIM900.prin tln(); // Give module time to send SMS delay(5000); } } delay(10); } boolean readData() { //Read humidity ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 13 h = dht.readHumidity(); // Read temperature as Celsius t = dht.readTemperature(); // Read temperature as Fahrenheit f = dht.readTemperature(true); // Compute temperature values in Celcius t = dht.computeHeatIndex(t,h,false); // Uncomment to compute temperature values in Fahrenheit //f = dht.computeHeatIndex(f,h,false); // Check if any reads failed and exit early (to try again) if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return false; } Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); //Uncomment to print temperature in Farenheit //Serial.print(f); //Serial.print(" *F\t"); return true; } ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 14 boolean SMSRequest() { if(SIM900.available() >0) { incomingChar=SIM900 read(); if(incomingChar=='S') { delay(10); Serial.print(incomingC har); incomingChar=SIM900 read(); if(incomingChar =='T') { delay(10); Serial.print(incomingChar); incomingChar=SIM900.read(); if(incomingChar=='A') { delay(10); Serial.print(incomingChar); incomingChar=SIM900.read(); if(incomingChar=='T') { delay(10); Serial.print(incomingChar); incomingChar=SIM900.read(); if(incomingChar=='E') { delay(10); Serial.print(incomingChar); Serial.print(" Request Received \n"); return true; } } } } } } ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 15 return false; } //////////////////////////////////////////////////////// Đã biên dịch thành công // Include DHT library and Adafruit Sensor Library #include "DHT.h" #include //Include Software Serial library to communicate with GSM #include // Pin DHT is connected to #define DHTPIN // Uncomment whatever type of sensor you're using #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); // Create global varibales to store temperature and humidity float t; // temperature in celcius float f; // temperature in fahrenheit float h; // humidity // Configure software serial port SoftwareSerial SIM900(7, 8); // Create variable to store incoming SMS characters char incomingChar; void setup() { dht.begin(); Serial.begin(19200); SIM900.begin(19200); // Give time to your GSM shield log on to network delay(20000); Serial.print("SIM900 ready "); // AT command to set SIM900 to SMS mode SIM900.print("AT+CMGF=1\r"); delay(100); ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 16 // Set module to send SMS data to serial out upon receipt SIM900.print("AT+CNMI=2,2,0,0,0\r"); delay(100); } void loop() { if (SMSRequest()) { if (readData()) { delay(10); // REPLACE THE X's WITH THE RECIPIENT'S MOBILE NUMBER // USE INTERNATIONAL FORMAT CODE FOR MOBILE NUMBERS SIM900.println("AT + CMGS = \"+XXXXXXXXXX\""); delay(100); // REPLACE WITH YOUR OWN SMS MESSAGE CONTENT String dataMessage = ("Temperature: " + String(t) + "*C " + " Humidity: " + String(h) + "%"); // Uncomment to change message with farenheit temperature // String dataMessage = ("Temperature: " + String(f) + "*F " + " Humidity: " + String(h) + "%"); // Send the SMS text message SIM900.print(dataMessage); delay(100); // End AT command with a ^Z, ASCII code 26 SIM900.println((char)26); delay(100); SIM900.println(); // Give module time to send SMS delay(5000); } } delay(10); } boolean readData() { //Read humidity h = dht.readHumidity(); // Read temperature as Celsius t = dht.readTemperature(); // Read temperature as Fahrenheit f = dht.readTemperature(true); // Compute temperature values in Celcius t = dht.computeHeatIndex(t, h, false); // Uncomment to compute temperature values in Fahrenheit ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 17 //f = dht.computeHeatIndex(f,h,false); // Check if any reads failed and exit early (to try again) if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return false; } Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); //Uncomment to print temperature in Farenheit //Serial.print(f); //Serial.print(" *F\t"); return true; } boolean SMSRequest() { if (SIM900.available() > 0) { incomingChar = SIM900.read(); if (incomingChar == 'S') { delay(10); Serial.print(incomingChar); incomingChar = SIM900.read(); if (incomingChar == 'T') { delay(10); Serial.print(incomingChar); incomingChar = SIM900.read(); if (incomingChar == 'A') { delay(10); Serial.print(incomingChar); incomingChar = SIM900.read(); if (incomingChar == 'T') { delay(10); Serial.print(incomingChar); incomingChar = SIM900.read(); if (incomingChar == 'E') { delay(10); Serial.print(incomingChar); Serial.print(" Request Received \n"); return true; } } } } } } ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 18 return false; } Importing libraries Trước tiên, bạn bao gồm thư viện cần thiết cho dự án này: thư viện DHT để đọc từ cảm biến DHT thư viện SoftwareSerial để liên lạc với mô-đun SIM900 GSM #include "DHT.h" #include #include DHT sensor Sau đó, bạn khai báo với Arduino chân liệu DHT kết nối với chân 2, chọn loại cảm biến DHT #define DHTPIN #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); Tạo biến float để lưu trữ giá trị nhiệt độ độ ẩm float t; // temperature in celcius float f; // temperature in fahrenheit float h; // humidity GSM shield Kêt nối chân 8, chân cấu hình RX chân TX Phần mềm SIM900 (7, 8); Bạn tạo biến char để lưu trữ ký tự SMS đến incomingChar; setup() In the setup(), you begin the DHT and the SIM900 shield The SIM900 shield is set to text mode and you also set the module to send the SMS data to the serial monitor when it receives it This is done with the following two lines, respectively: Trong setup (), bắt đầu kêt nối DHT SIM900 Module SIM900 đặt thành chế độ văn bạn đặt mô-đun để gửi liệu SMS đến serial monitor nhận Điều thực với hai dòng sau, tương ứng: ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 19 SIM900.print("AT+CMGF=1\r"); SIM900.print("AT+CNMI=2,2,0,0,0\r"); Functions Chúng ta tạo hàm để đọc nhiệt độ độ ẩm gọi readData () Hàm lưu trữ giá trị biến t h Code sử dụng nhiệt độ tính Celsius, chuẩn bị bạn muốn Fahrenheit Chúng tạo hàm để kiểm tra xem tin nhắn đến có STATE - hàm SMSRequest () Hàm trả true Arduino nhận thơng báo có chữ STATE false không Bạn đọc ký tự SMS đến cách sử dụng: incomingChar = SIM900.read(); loop() Trong loop (), bạn kiểm tra xem có yêu cầu SMS với hàm SMSRequest () không - bạn kiểm tra xem Arduino có nhận thơng báo STATE khơng Nếu đúng, đọc nhiệt độ độ ẩm gửi qua SMS cho bạn Số mà câu trả lời Arduino đặt dòng sau: SIM900.println ("AT + CMGS = \" XXXXXXXXXXXX \ ""); Thay XXXXXXXXXXXX số điện thoại người nhận Lưu ý: bạn phải thêm số theo định dạng số điện thoại quốc tế Ví dụ: Bồ Đào Nha, số đặt trước + 351XXXXXXXXX Sau đó, bạn lưu trữ thơng điệp bạn muốn gửi biến dataMessage Cuối cùng, bạn gửi tin nhắn văn SMS cách sử dụng: SIM900.print (dataMessage); Trình diễn Khi bạn gửi tin nhắn STATE tới Arduino, trả lời liệu cảm biến Xem video bắt đầu dự án để có demo dự án chuyên sâu ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 20 Kết thúc Đây dự án tuyệt vời để giúp bạn bắt đầu với moule SIM900 GSM Bạn học cách đọc gửi tin nhắn văn SMS với Arduino Bạn áp dụng khái niệm học nhiều dự án Ở đây, số ý tưởng dự án: • Hệ thống giám sát gửi tin nhắn SMS phát chuyển động • Điều khiển rơle qua SMS • Yêu cầu liệu cảm biến cụ thể từ sưu tập cảm biến cách thêm nhiều điều kiện vào mã ARDUINO IOT VIETNAM https://www.facebook.com/groups/486295328966960/ 21 ... liệu cảm biến qua SMS với Arduino Như ví dụ, chúng tơi u cầu nhiệt độ độ ẩm từ cảm biến DHT11 Để gửi nhận SMS với Arduino, sử dụng SIM900 GSM shield Khi bạn gửi tin nhắn SMS đến Arduino với thơng... đọc nhiệt độ độ ẩm Trước tiếp tục với hướng dẫn này, chúng tơi khun bạn nên đọc tài liẹu sau: • Hướng dẫn SIM900 GSM GPRS Shield với Arduino • Hướng dẫn cảm biến độ ẩm nhiệt độ DHT11 / DHT22 với. .. đọc nhiệt độ độ ẩm gọi readData () Hàm lưu trữ giá trị biến t h Code sử dụng nhiệt độ tính Celsius, chuẩn bị bạn muốn Fahrenheit Chúng tạo hàm để kiểm tra xem tin nhắn đến có STATE - hàm SMSRequest

Ngày đăng: 24/10/2020, 00:16

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w