Monitor Jarak Secara ONLINE Thingspeak
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat memonitoring jarak secara online jarak jauh dengan menggunakan webserver Thingspeak. alat ini menggunakan 3 buah sensor HC-SRF04 dan wemos D1 sebagai mikrokontrollernya. untuk lebih jelasnya berikut adalah daftar komponen dan programnya.
a. Wemos D1
b. LCD I2C
c. Sensor Jarak HC-SRF04
d. Program Arduino IDE
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define trigPin1 D0
#define echoPin1 D3
#define trigPin2 D5
#define echoPin2 D6
#define trigPin3 D7
#define echoPin3 D8
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
String apiKey = "456FHJVCD9HGTSFGG"; // Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";
const char *ssid = "Hotspot WIFIKU"; // replace with your wifi ssid and wpa2 key
const char *pass = "123456789";
const char* server = "api.thingspeak.com";
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
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");
lcd.begin();
lcd.clear();
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
}
void loop() {
digitalWrite(trigPin1, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
digitalWrite(trigPin2, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;
digitalWrite(trigPin3, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin3, LOW);
duration3 = pulseIn(echoPin3, HIGH);
distance3 = (duration3/2) / 29.1;
lcd.setCursor(0,0);
lcd.print("S1:");
lcd.print(distance1);
lcd.print(" S2:");
lcd.print(distance2);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("S3:");
lcd.print(distance3);
lcd.print(" ");
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field4=";
postStr += String(distance1);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field4=" + distance1 + "&field5=" + distance2 + "&field6=" + distance3 +" 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.println(". Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
delay(10000);
}
e. VIDEO HASILNYA
No comments:
Post a Comment