Báo mưa bằng Facebook Messenger

07/08/2019
bao-mua-bang-facebook-messenger

Mô tả dự án: 

Sài Gòn đang vào mùa mưa rồi anh em ạ, mỗi khi mưa xuống lại một nỗi buồn man mác vì quên không cất quần áo :(

Vậy nên hôm nay mình xin hướng dẫn làm một bộ báo mưa bằng qua FB Messenger cực đơn giản, chỉ cắm là chạy!  

1

Chuẩn bị

  1. Đọc qua bài viết này để biết nguyên lý hoạt động cũng như lấy key để nhắn tin bằng FB Messenger
  2. Module báo mưa (Loại của mình chân digital có mưa là 0 không mưa là 1) 
  3. ESP8266 ở đây mình dùng module nodeMCU

2

Nào cùng làm

Nối dây:

Nối dây D0 từ cảm biến mưa vào chân D2 (GPIO 4) trên NodeMCU

Cấp nguồn cho cảm biến mưa vào 2 chân GND và VCC

Cắm cáp USB vào NodeMCU

Nạp code:

Các bạn nạp code bên dưới với phần key là key get được như hướng dẫn ở bài dưới đây

Message là nội dung tin nhắn sẽ được gửi đi khi có mưa được mã hóa dạng URL Encode,

các bạn vào https://hs2t.com/itemized/url-encode... để chuyển đổi từ Tiếng Việt qua URL Encode

WiFiMulti.addAP("KemShop-55LTT", ""); các bạn thay thế bằng tên wifi và password nhà mình để ESP8266 kết nối đến

Còn đây là code:

 
  1. #include <Arduino.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WiFiMulti.h>
  5. #include <ESP8266HTTPClient.h>
  6. #include <WiFiClientSecureBearSSL.h>
  7.  
  8. ESP8266WiFiMulti WiFiMulti;
  9.  
  10. // constants won't change. They're used here to set pin numbers:
  11. const int rainPin = 4; // the number of the pushbutton pin
  12. const int ledPin = 2; // the number of the LED pin
  13.  
  14. // variables will change:
  15. int rainState = 0; // variable for reading the pushbutton status
  16. int lastState = 0;
  17. String key = "g183ftk4h";
  18. String message = "M%C6%B0a%20r%E1%BB%93i%20anh%20%C6%A1i%2C%20mau%20c%E1%BA%A5t%20qu%E1%BA%A7n%20%C3%A1o%20%C4%91i%20k%E1%BA%BBo%20v%E1%BB%A3%20m%E1%BA%AFng";
  19.  
  20. void setup()
  21. {
  22.  
  23. Serial.begin(115200);
  24. // Serial.setDebugOutput(true);
  25.  
  26. Serial.println();
  27. Serial.println();
  28. Serial.println();
  29.  
  30. // initialize the LED pin as an output:
  31. pinMode(ledPin, OUTPUT);
  32. // initialize the pushbutton pin as an input:
  33. pinMode(rainPin, INPUT);
  34.  
  35. for (uint8_t t = 4; t > 0; t--)
  36. {
  37. Serial.printf("[SETUP] WAIT %d...\n", t);
  38. Serial.flush();
  39. delay(1000);
  40. }
  41.  
  42. WiFi.mode(WIFI_STA);
  43. WiFiMulti.addAP("KemShop-55LTT", "");
  44. }
  45.  
  46.  
  47.  
  48. void loop()
  49. {
  50. // read the state of the pushbutton value:
  51. rainState = digitalRead(rainPin);
  52.  
  53. // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  54. if (rainState == 0 && lastState == 0) {
  55. Serial.printf("RAIN\n");
  56. lastState = 1;
  57. // turn LED on:
  58. digitalWrite(ledPin, HIGH);
  59. // wait for WiFi connection
  60. if ((WiFiMulti.run() == WL_CONNECTED))
  61. {
  62.  
  63. std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
  64.  
  65. // client->setFingerprint(fingerprint);
  66. client->setInsecure();
  67.  
  68. HTTPClient https;
  69.  
  70. Serial.print("[HTTPS] begin...\n");
  71. if (https.begin(*client, "https://taymay.herokuapp.com/send/?key="+key+"&message="+message))
  72. { // HTTPS
  73.  
  74. Serial.print("[HTTPS] GET...\n");
  75. // start connection and send HTTP header
  76. int httpCode = https.GET();
  77.  
  78. // httpCode will be negative on error
  79. if (httpCode > 0)
  80. {
  81. // HTTP header has been send and Server response header has been handled
  82. Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
  83.  
  84. // file found at server
  85. if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
  86. {
  87. String payload = https.getString();
  88. Serial.println(payload);
  89. }
  90. }
  91. else
  92. {
  93. Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
  94. }
  95.  
  96. https.end();
  97. }
  98. else
  99. {
  100. Serial.printf("[HTTPS] Unable to connect\n");
  101. }
  102. }
  103. } else if (rainState == 1 && lastState == 1) {
  104. lastState = 0;
  105. // turn LED off:
  106. digitalWrite(ledPin, LOW);
  107. }
  108.  
  109. delay(5000);
  110. }

 

3

Thành quả

Vậy thôi khá đơn giản giờ thì cứ thoải mái lướt Facebook không sợ mưa rơi :

Bình luận
Nội dung này chưa có bình luận, hãy gửi bình luận đầu tiên của bạn.
VIẾT BÌNH LUẬN CỦA BẠN