อ่านค่าอุณหภูมิผ่าน dashboard และ แจ้งเตือนผ่าน Line

ในโปรเจคนี้จะใช้ NodeMCU อ่านค่า sensor อุณภูมิและความชื้น DHT22 แล้วนำค่าไปพลอตบนกราฟของ Dashboard Anto และสามารถตั้งค่าอุณหภูมิสำหรับการแจ้งเตือนผ่าน Line ได้

สามารถเข้าไปดูวีดีโอได้ที่ :

Code example

/*
 * This example is meant to be used as a starting point
 * for working with Anto.io services and DHT sensor module
 *
 * 2017/06/17
 * by Anto.io team
 *
 */

#include <AntoIO.h>
#include <DHT.h>

#define DHTPIN 2    //port D4
#define DHTTYPE DHT22

const char *ssid = "xxx";
const char *pass = "xxx";
const char *user = "xxx";
const char *token = "xxx";
const char *thing = "nodeMCUwithLine";

// initialize AntoIO instance
AntoIO anto(user, token, thing);

//initialize dht instance
DHT dht(DHTPIN,DHTTYPE);

// Line config
#define LINE_TOKEN "xxx"

float tempCutOff = 1000;
float humidCutOff = 1000;

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Anto library version: ");
  Serial.println(anto.getVersion());

  Serial.print("\nTrying to connect ");
  Serial.print(ssid);
  Serial.println("...");

  anto.begin(ssid, pass, messageReceived);
  Serial.println("\nConnected Anto done");

  anto.sub("tempCutOff");
  anto.sub("humidCutOff");

  //dht start
  dht.begin();
}


void loop() {
  anto.mqtt.loop();

  if (!anto.mqtt.isConnected())
    Serial.println("Disconnected");

  float temp = dht.readTemperature();
  float humid = dht.readHumidity();

  Serial.print("TEMP: ");
  Serial.println(temp);

  Serial.print("HUMID: ");
  Serial.println(humid);

  if(temp >= 0){
     anto.pub("Temp",temp);   
     if(temp >= tempCutOff){
        Line_Notify("HaAHa farm kub");
     }
  }

  if(humid >= 0){
     anto.pub("Humid",humid);
     if(humid >= humidCutOff){
        Line_Notify("help! Humid");
     }
  } 

  delay(1000);
}

// a callback function for arriving data.
void messageReceived(String thing, String channel, String payload) {
  Serial.print("Recieved: ");
  Serial.print(thing);
  Serial.print("/");
  Serial.print(channel);
  Serial.print("-> ");
  Serial.println(payload);

  if(channel.equals("tempCutOff")){
        float value = payload.toFloat();
        tempCutOff = value;
  }
  else if(channel.equals("humidCutOff")){
        float value = payload.toFloat();
        humidCutOff = value;
  }
}

void Line_Notify(String message) {
  WiFiClientSecure client;

  if (!client.connect("notify-api.line.me", 443)) {
    Serial.println("connection failed");
    return;   
  }

  String req = "";
  req += "POST /api/notify HTTP/1.1\r\n";
  req += "Host: notify-api.line.me\r\n";
  req += "Authorization: Bearer " + String(LINE_TOKEN) + "\r\n";
  req += "Cache-Control: no-cache\r\n";
  req += "User-Agent: ESP8266\r\n";
  req += "Content-Type: application/x-www-form-urlencoded\r\n";
  req += "Content-Length: " + String(String("message=" + message).length()) + "\r\n";
  req += "\r\n";
  req += "message=" + message;
  client.print(req);

  delay(20);

  // Serial.println("-------------");
  while(client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
    //Serial.println(line);
  }
  // Serial.println("-------------");
}

อย่าลืมเข้าไปแก้ไขในส่วน
SSID,Password,Channel , Things , Key , Line token และ อื่นๆ ด้วยนะครับ

คอร์สสอน พี่ช้าง

results matching ""

    No results matching ""