Translate

Monitor Suhu Kelembaban dan Kendali Lampu IOT Thingspeak

Monitor Suhu Kelembaban dan Kendali Lampu IOT Thingspeak
 

         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor suhu dan kelembaban serta bisa mengendalikan lampu atau device lain melalui ONLINE dengan server Thingspeak. untuk lebih jelasnya berikut adalah program dan komponennya.
 
 
a. Komponen
 

 
 
b. Program Arduino IDE
 
#include <Wire.h>
#include "ThingSpeak.h"
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>

#define DHTPIN 0         // PIN D3 (GPIO 0) dari NodeMCU terhubung dengan PIN Data dari DHT22
#define DHTTYPE DHT22     // Kita menggunakan tipe DHT22
DHT dht(DHTPIN, DHTTYPE); // Inisialisasi DHT22

LiquidCrystal_I2C lcd(0x27,16,2);

//change your channel number here
unsigned long channel = 2233667;

String apiKey = "HGKJ67576HJF3HGFJ7H";     //  Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";

const char *ssid =  "hotspothpku";     // replace with your wifi ssid and wpa2 key
const char *pass =  "123456789";
const char* server = "api.thingspeak.com";

//2,3 and 4 are channel fields in thingspeak.
unsigned int flampu1 = 3;
unsigned int flampu2 = 4;
unsigned int fpompa1 = 5;
unsigned int fpompa2 = 6;

float h;
float t;
int led_1;
int led_2;
int led_3;
int led_4;

WiFiClient client;

void setup(){

pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(16, OUTPUT);
pinMode(14, OUTPUT);

Serial.begin(115200);
delay(10);
lcd.begin(); //set lcd i2c
lcd.noCursor(); //biar gak ada cursor di lcd
lcd.clear(); //clear lcd
dht.begin();

       Serial.println("Connecting to ");
       Serial.println(ssid);

// Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

while (WiFi.status() != WL_CONNECTED)
     {
            delay(500);
            Serial.print(".");
     }
      Serial.println("");
      Serial.println("WiFi connected");
      delay(100);

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
ThingSpeak.begin(client);
}

void loop(){

  h = dht.readHumidity();
  t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(t);
lcd.print(" C   ");
lcd.setCursor(0, 1);
lcd.print("Humi: ");
lcd.print(h);
lcd.print(" %   ");

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }


if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
{
                            
                             String postStr = apiKey;
                             postStr +="&field1=";
                             postStr += String(t);
                             postStr += "\r\n\r\n";

                             client.print(String("GET ") + resource + apiKey + "&field1=" + t + "&field2=" + h + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
                             //client.print(String("GET ") + resource + apiKey + "&field1=" + t + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");                             
                                        
                             client.print("Host: api.thingspeak.com\n");
                             client.print("Connection: close\n");
                             client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
                             client.print("Content-Type: application/x-www-form-urlencoded\n");
                             client.print("Content-Length: ");
                             client.print(postStr.length());
                             client.print("\n\n");
                             client.print(postStr);
                             
                             Serial.print("Temperature: ");
                             Serial.print(t);
                             Serial.print(" humidity: ");
                             Serial.print(h);
                             Serial.println(". Send to Thingspeak.");
                        }
                        
          client.stop();

          Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
  delay(5000);

kendali();
delay(5000);
}


void kendali(){

  //get the last data of the fields
  led_1 = ThingSpeak.readFloatField(channel, 3);
  led_2 = ThingSpeak.readFloatField(channel, 4);
  led_3 = ThingSpeak.readFloatField(channel, 5);
  led_4 = ThingSpeak.readFloatField(channel, 6);
 
  if(led_1 == 1){
    digitalWrite(13,HIGH);
    Serial.println("lampu1 is On..!");
  }
  else if(led_1 == 0){
    digitalWrite(13,LOW);
    Serial.println("lampu1 is OFF");
  }

  if(led_2 == 1){
    digitalWrite(12,HIGH);
    Serial.println("lampu2 is On..!");
  }
  else if(led_2 == 0){
    digitalWrite(12,LOW);
    Serial.println("lampu2 is OFF");
  }

  if(led_3 == 1){
    digitalWrite(16,HIGH);
    Serial.println("POMPA1 is On..!");
  }
  else if(led_3 == 0){
    digitalWrite(16,LOW);
    Serial.println("POMPA1 is OFF");
  }

  if(led_4 == 1){
    digitalWrite(14,HIGH);
    Serial.println("POMPA2 is On..!");
  }
  else if(led_4 == 0){
    digitalWrite(14,LOW);
    Serial.println("POMPA2 is OFF");
  }
 
}



c. Tampilan Interface Web

d. Program index.php Website

<html>
<head>
<title>LED ON/OFF</title>
</head>
<body>

<p align="center">
<font size="8">

<b>LAMPU 1 = &nbsp <a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field3=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field3=0">OFF</a></b>
<br>
<b>LAMPU 2 = &nbsp <a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field4=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field4=0">OFF</a></b>
<br>
<b>POMPA 1 = &nbsp <a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field5=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field5=0">OFF</a></b>
<br>
<b>POMPA 2 = &nbsp <a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field6=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=HGKJ67576HJF3HGFJ7H&field6=0">OFF</a></b>
<br>

</font>
</p>

</body>
</html>



e. VIDEO HASILNYA
 

 
 

No comments:

Post a Comment