Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat berbasis IOT dengan mikrokontroller yang digunakan adalah NodeMCU ESP8266. server yang digunakan adalah THINGSPEAK dan sensor suhu yang digunakan adalah LM35. untuk lebih jelasnya berikut adalah program dan daftar komponennya.
a. NodeMCU ESP8266
b. Sensor Suhu LM35
c. LED
d. Gambar Hasilnya
e. Program Arduino IDE
//alamat kendali
//https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field2=1&field3=1&field4=0
#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
//Replace your wifi credentials here
const char* ssid = "Hotspot Wifiku";
const char* password = "123456789";
//change your channel number here
unsigned long channel = 884148;
//2,3 and 4 are channel fields in thingspeak.
unsigned int led1 = 2;
unsigned int led2 = 3;
unsigned int led3 = 4;
String apiKey = "TTPXYPWJTOC07JYW"; // Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";
const char* server = "api.thingspeak.com";
const int AnalogIn = A0;
float suhu;
int dataadc;
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
delay(100);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
digitalWrite(D1, 0);
digitalWrite(D2, 0);
digitalWrite(D3, 0);
// We start by connecting to a WiFi network
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() {
//get the last data of the fields
int led_1 = ThingSpeak.readFloatField(channel, led1);
int led_2 = ThingSpeak.readFloatField(channel, led2);
int led_3 = ThingSpeak.readFloatField(channel, led3);
if(led_1 == 1){
digitalWrite(D1, 1);
Serial.println("D1 is On..!");
}
else if(led_1 == 0){
digitalWrite(D1, 0);
Serial.println("D1 is Off..!");
}
if(led_2 == 1){
digitalWrite(D2, 1);
Serial.println("D2 is On..!");
}
else if(led_2 == 0){
digitalWrite(D2, 0);
Serial.println("D2 is Off..!");
}
if(led_3 == 1){
digitalWrite(D3, 1);
Serial.println("D3 is On..!");
}
else if(led_3 == 0){
digitalWrite(D3, 0);
Serial.println("D3 is Off..!");
}
Serial.println(led_1);
Serial.println(led_2);
Serial.println(led_3);
delay(5000);
dataadc = analogRead(AnalogIn);
suhu = dataadc * (3.2/1023.0) * 100.0;
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(suhu);
postStr += "\r\n\r\n";
//client.print(String("GET ") + resource + apiKey + "&field1=" + temperatureTemp + "&field2=" + humidityTemp + " HTTP/1.1\r\n" + "Host: " + server + "\r\n" + "Connection: close\r\n\r\n");
client.print(String("GET ") + resource + apiKey + "&field1=" + suhu + " 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(suhu);
Serial.println("C. 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. Program index.php
<html>
<head>
<title>LED ON/OFF</title>
</head>
<body>
<p align="center">
<font size="8">
<b>LAMPU 1 =   <a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field2=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field2=0">OFF</a></b>
<br>
<b>LAMPU 2 =   <a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field3=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field3=0">OFF</a></b>
<br>
<b>LAMPU 3 =   <a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field4=1">ON </a></b> /
<b><a href="https://api.thingspeak.com/update?api_key=TTPXYPWJTOC07JYW&field4=0">OFF</a></b>
<br>
</font>
</p>
</body>
</html>
g. VIDEO HASILNYA
No comments:
Post a Comment