Translate

Monitor Suhu Kelembaban ONLINE 2 Node ESP8266 Thingspeak Penampil DMD P10 Arduino

Monitor Suhu Kelembaban ONLINE 2 Node ESP8266 Thingspeak Penampil DMD P10 Arduino

         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitor 2 ruangan yang jaraknya berjauhan,  parameter yang dimonitor adalah suhu dan kelembaban ruangan, tampilan angka berada di ruang kontrol yang jaraknya juga berjauhan dengan ruangan yang dimonitor untuk itu project ini menggunakan internet dan server Thingspeak untuk transfer datanya. untuk lebih jelasnya berikut adalah koding dan komponennya.

 

a. Komponen yang dipakai


b. Program Receiver Arduino Uno

#include <Wire.h>
#include <Keypad.h>
#include <SPI.h>        //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h>        //Library DMD yang menyediakan fungsi penampilan teks, gambar dsb
#include <TimerOne.h>   //Library peripheral Timer1 untuk menjalankan prosedur pindai panel DMD
#include <Time.h>     //Library waktu yang menyediakan tipe data, struktur, dan obyek waktu
#include "Arial_black_16.h"
#include "Arial_Black_16_ISO_8859_1.h"
#include "Arial14.h"
#include "DejaVuSans9.h"
#include "Droid_Sans_12.h"
#include "Droid_Sans_16.h"
#include "DejaVuSansItalic9.h"
#include "Mono5x7.h"
#include "SystemFont5x7.h"
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>  
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define WAKTU_TAMPIL_JAM      10    //detik
#define WAKTU_TAMPIL_KALENDAR 5     //detik

//Isi dengan jumlah panel DMD yang di susun dalam kolom dan baris
#define DISPLAY_COLUMN_COUNT  4
#define DISPLAY_ROW_COUNT     1

#define PIXELS_PER_COLUMN    32
#define PIXELS_PER_ROW       16

DMD dmd(DISPLAY_COLUMN_COUNT, DISPLAY_ROW_COUNT);
unsigned char show = 0;

char lineBuff[20];
char lineBuff2[20];
 
char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {A3,A2,A1,A0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,4,3,2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

long batasmaxtemp;
long batasmaxtemp2;
long batasmintemp;
long batasmintemp2;

long batashum;
int suhu1;
int kelembaban1;
int suhu2;
int kelembaban2;

int savesuhu1;
int savekelembaban1;
int savesuhu2;
int savekelembaban2;

String dataIn;
String dt[10];
int i;
boolean parsing=false;
int datain1;
int datain2;
int datain3;
int datain4;
int relay = 10;
int relay2 = 12;
char lineBuffx1[20];
char lineBuffx2[20];
char lineBuffz1[20];
char lineBuffz2[20];

DS3231  rtc(SDA, SCL);
Time  t;

void ScanDMD()
{
  dmd.scanDisplayBySPI();
}

void setup(){
 
lcd.begin();
lcd.noCursor();
lcd.clear();
Serial.begin(9600);
dataIn="";

pinMode(relay,OUTPUT);
digitalWrite(relay,HIGH);
pinMode(12,OUTPUT);
digitalWrite(12,HIGH);

dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)

//initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 1000 );           //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD );   //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
 
//clear/init the DMD pixels held in RAM
dmd.clearScreen( true );
//clear/init the DMD pixels held in RAM
dmd.clearScreen( true );
rtc.begin();
//rtc.setDOW(FRIDAY);     // Set Day-of-Week to SUNDAY
//rtc.setTime(15, 17, 0);     // Set the time to 12:00:00 (24hr format)
//rtc.setDate(20, 1, 2022);    
}


void loop(){
t = rtc.getTime();

customKey = customKeypad.getKey();

if(customKey == 'A'){
lcd.clear();
delay(200);
batasmaxtemp = 0;
batasmintemp = 0;
inputbatasmaxsuhu();
inputbatasminsuhu();
}

if(customKey == 'B'){
lcd.clear();
delay(200);
batasmaxtemp2 = 0;
batasmintemp2 = 0;
inputbatasmaxsuhu2();
inputbatasminsuhu2();
}

 while(Serial.available()>0) {
//   dataIn="";
    char inChar = (char)Serial.read();
    dataIn += inChar;
    if (inChar == '\n') {
    parsing = true;
  }
}

if(parsing){
  parsingData();
}

lcd.setCursor(0,0);
lcd.print("S1:");
lcd.print(savesuhu1);
lcd.print("/");
lcd.print(savekelembaban1);
lcd.print(" ");
lcd.print(batasmintemp);
lcd.print("/");
lcd.print(batasmaxtemp);

lcd.setCursor(0,1);
lcd.print("S2:");
lcd.print(savesuhu2);
lcd.print("/");
lcd.print(savekelembaban2);
lcd.print(" ");
lcd.print(batasmintemp2);
lcd.print("/");
lcd.print(batasmaxtemp2);

sprintf(lineBuff2, "%d:%d ", savesuhu1, savekelembaban1);
dmd.selectFont(Droid_Sans_12);
dmd.drawString( 3,  3, lineBuff2, strlen(lineBuff2), GRAPHICS_NORMAL);

sprintf(lineBuff, "%d:%d ", savesuhu2, savekelembaban2);
dmd.selectFont(Droid_Sans_12);
dmd.drawString( 37,  3, lineBuff, strlen(lineBuff), GRAPHICS_NORMAL);  

sprintf(lineBuffx1, "%d:%d", t.hour, t.min);
sprintf(lineBuffx2, "%d ", t.sec);

dmd.selectFont(DejaVuSans9);
dmd.selectFont(Mono5x7);
dmd.drawString( 65,  1, lineBuffx1, strlen(lineBuffx1), GRAPHICS_NORMAL);  
dmd.drawString( 75,  8, lineBuffx2, strlen(lineBuffx2), GRAPHICS_NORMAL);

sprintf(lineBuffz1,"RUANG");
sprintf(lineBuffz2,"OBAT");
dmd.selectFont(DejaVuSans9);
dmd.selectFont(Mono5x7);
dmd.drawString( 95,  1, lineBuffz1, strlen(lineBuffz1), GRAPHICS_NORMAL);  
dmd.drawString( 95,  8, lineBuffz2, strlen(lineBuffz2), GRAPHICS_NORMAL);  

 
if((savesuhu1 > batasmaxtemp)&&(batasmaxtemp > 0)){
  digitalWrite(relay,LOW);
}

if((savesuhu1 < batasmintemp)&&(batasmintemp > 0)){
  digitalWrite(relay,LOW);
}

if((savesuhu2 > batasmaxtemp2)&&(batasmaxtemp2 > 0)){
  digitalWrite(relay,LOW);
}

if((savesuhu2 < batasmintemp2)&&(batasmintemp2 > 0)){
  digitalWrite(relay,LOW);
}

if((savesuhu1 > batasmintemp)&&(savesuhu1 < batasmaxtemp)&&(savesuhu2 > batasmintemp2)&&(savesuhu2 < batasmaxtemp2)){
  digitalWrite(relay,HIGH);
}

delay(1000);
//dmd.clearScreen( true );   //true is normal (all pixels off), false is negative (all pixels on)

}


void parsingData(){
int j=0;

//kirim data yang telah diterima sebelumnya
//Serial.print("data masuk : ");
//Serial.print(dataIn);
//Serial.print("\n");

//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]="";       //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}

datain1 = dt[0].toInt();
datain2 = dt[1].toInt();
datain3 = dt[2].toInt();
datain4 = dt[3].toInt();

/*
//kirim data hasil parsing
Serial.print("data 1 : ");
Serial.print(datain1);
Serial.print("\n");
Serial.print("data 2 : ");
Serial.print(datain2);
Serial.print("\n");
Serial.print("data 3 : ");
Serial.print(datain3);
Serial.print("\n");
Serial.print("data 4 : ");
Serial.print(datain4);
Serial.print("\n");
*/

suhu1 = datain1 / 1;
kelembaban1 = datain2 / 1;
suhu2 = datain3 / 1;
kelembaban2 = datain4 / 1;

if(suhu1 > 0){
  savesuhu1 = suhu1;
}

if(kelembaban1 > 0){
  savekelembaban1 = kelembaban1;
}

if(suhu2 > 0){
  savesuhu2 = suhu2;
}

if(kelembaban2 > 0){
  savekelembaban2 = kelembaban2;
}

 
}



void inputbatasmaxsuhu(){
 
lcd.setCursor(0,0);
lcd.print("MAX Suhu 1:");
        
customKey = customKeypad.getKey();

if(customKey >= '0' && customKey <= '9')
  {
      batasmaxtemp = batasmaxtemp * 10 + (customKey - '0');
      lcd.setCursor(0,1);
      lcd.print(batasmaxtemp);
  }

if(customKey == 'D'){
lcd.clear();
delay(200);  
return;
}

if(customKey == 'C'){
lcd.clear();
delay(200);
batasmaxtemp = 0;
}
 
delay(200);
   
inputbatasmaxsuhu();  
}


void inputbatasminsuhu(){
 
lcd.setCursor(0,0);
lcd.print("MIN Suhu 1:");
        
customKey = customKeypad.getKey();

if(customKey >= '0' && customKey <= '9')
  {
      batasmintemp = batasmintemp * 10 + (customKey - '0');
      lcd.setCursor(0,1);
      lcd.print(batasmintemp);
  }

if(customKey == 'D'){
lcd.clear();
delay(200);  
return;
}

if(customKey == 'C'){
lcd.clear();
delay(200);
batasmintemp = 0;
}

delay(200);
   
inputbatasminsuhu();  
}


void inputbatasmaxsuhu2(){
 
lcd.setCursor(0,0);
lcd.print("MAX Suhu 2:");
        
customKey = customKeypad.getKey();

if(customKey >= '0' && customKey <= '9')
  {
      batasmaxtemp2 = batasmaxtemp2 * 10 + (customKey - '0');
      lcd.setCursor(0,1);
      lcd.print(batasmaxtemp2);
  }

if(customKey == 'D'){
lcd.clear();
delay(200);  
return;
}

if(customKey == 'C'){
lcd.clear();
delay(200);
batasmaxtemp2 = 0;
}
 
delay(200);
   
inputbatasmaxsuhu2();  
}


void inputbatasminsuhu2(){
 
lcd.setCursor(0,0);
lcd.print("MIN Suhu 2:");
        
customKey = customKeypad.getKey();

if(customKey >= '0' && customKey <= '9')
  {
      batasmintemp2 = batasmintemp2 * 10 + (customKey - '0');
      lcd.setCursor(0,1);
      lcd.print(batasmintemp2);
  }

if(customKey == 'D'){
lcd.clear();
delay(200);  
return;
}

if(customKey == 'C'){
lcd.clear();
delay(200);
batasmintemp2 = 0;
}
 
delay(200);
   
inputbatasminsuhu2();  
}
 


c. Program Receiver Nodemcu ESP8266

#include <Wire.h>
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>  
LiquidCrystal_I2C lcd(0x27, 16, 2);

unsigned long channel = 1740399;

String apiKey = "76HGJHE437KJLH98H";     
const char* resource = "/update?api_key=";

const char *ssid =  "wifikujoss";     
const char *pass =  "123456789";
const char* server = "api.thingspeak.com";

int suhu1;
int kelembaban1;
int suhu2;
int kelembaban2;

WiFiClient client;


void setup(){
 
lcd.begin();
lcd.noCursor();
lcd.clear();
Serial.begin(9600);

// 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(){

suhu1 = ThingSpeak.readFloatField(channel, 1);
kelembaban1 = ThingSpeak.readFloatField(channel, 2);
suhu2 = ThingSpeak.readFloatField(channel, 3);
kelembaban2 = ThingSpeak.readFloatField(channel, 4);

Serial.print("*");
Serial.print(suhu1);
Serial.print(",");
Serial.print(kelembaban1);
Serial.print(",");
Serial.print(suhu2);
Serial.print(",");
Serial.print(kelembaban2);
Serial.println("#");

delay(1000);  
}


 

d. Program Transmitter Ruangan 1

#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);

unsigned long channel = 1233456;

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

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

float h;
float t;

WiFiClient client;

void setup(){

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);

}



e. Program Transmitter Ruangan 2

#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);

unsigned long channel = 1233456;

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

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

float h;
float t;

WiFiClient client;

void setup(){

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 +="&field3=";
                             postStr += String(t);
                             postStr += "\r\n\r\n";

                             client.print(String("GET ") + resource + apiKey + "&field3=" + t + "&field4=" + 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);

}


 

f. VIDEO HASILNYA



No comments:

Post a Comment