Idenya adalah mengukur jarak menggunakan sensor ultrasonik dimana ESP32 berperan sebagai pemproses data dan mengirimkan data yang telah diolah ke platform IoT. Platform ThingSpeak digunakan untuk menampilkan hasil data sensor ultrasonik dalam bentuk grafik.
Alat & Bahan:
1. ESP32 & Kabel USB
2. Sensor Ultrasonik
3. Breadboard & Jumper
Scematic Rangkaian:
Program:
#include <ThingSpeak.h>
#include <WiFi.h>
const int trigPin = 2;
const int echoPin = 5;
// defines variables
long duration;
int distance;
//network information
const char *ssid = "rea"; //your network SSID (name)
const char *pass = "rearamada12"; //your network password
WiFiClient client;
//thingspeak settings
unsigned long channel = 833244; //your channel ID number
const char *apiKey = "9X6OW1HMNWBXWUOD"; //your channel write API Key
//int lum, i = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
delay(10);
//connect to WiFi
Serial.print("Connecting to: "); Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected\n");
ThingSpeak.begin(client); //initialize ThingSpeak
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// i++;
// lum = analogRead(34);
int x = ThingSpeak.writeField(channel, 1, distance, apiKey);
if (x == 200) {
Serial.println("Data " + String(distance));
Serial.print("distance: "); Serial.println(distance);
Serial.println("Update to ThingSpeak Successful.");
}
else {
Serial.println("Data " + String(distance));
Serial.println("Problem updating data. HTTP error code " + String(x));
}
Serial.println("Waiting...\n");
delay(20000);
}
#include <WiFi.h>
const int trigPin = 2;
const int echoPin = 5;
// defines variables
long duration;
int distance;
//network information
const char *ssid = "rea"; //your network SSID (name)
const char *pass = "rearamada12"; //your network password
WiFiClient client;
//thingspeak settings
unsigned long channel = 833244; //your channel ID number
const char *apiKey = "9X6OW1HMNWBXWUOD"; //your channel write API Key
//int lum, i = 0;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200);
delay(10);
//connect to WiFi
Serial.print("Connecting to: "); Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected\n");
ThingSpeak.begin(client); //initialize ThingSpeak
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// i++;
// lum = analogRead(34);
int x = ThingSpeak.writeField(channel, 1, distance, apiKey);
if (x == 200) {
Serial.println("Data " + String(distance));
Serial.print("distance: "); Serial.println(distance);
Serial.println("Update to ThingSpeak Successful.");
}
else {
Serial.println("Data " + String(distance));
Serial.println("Problem updating data. HTTP error code " + String(x));
}
Serial.println("Waiting...\n");
delay(20000);
}
Cara Kerja Alat & Hasil:
1. Masuk ke ThingSpeak di www.thingspeak.com kemudian buat channel baru (Channels> My Channels> New Channels). Pada percobaan ini diberi nama "Jarak Benda". Jika sudah kemudian simpan.
2. Setelah itu klik API Keys lalu copy 'Write API Keys' ke program arduino. Lalu upload ke ESP32.
3. Lihat tampilan serial monitor pada software Arduino IDE. Untuk memastikan status WiFi sudah terhubung. Serial monitor dapat menampilkan hasil jarak yang terukur dalam satuan centimeter.
2 comments:
kalau pakai esp8266 kaya gimana gan caranya nyambunginnya? saya coba pakai yg di atas gk bisa
Nama board yg dipake apa?
Post a Comment