ALAT DETEKSI 6 TELUR BAGUS / JELEK ARDUINO THINGSPEAK IOT OUTPUT SUARA DF-PLAYER MODULE
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat mendeteksi 6 buah telur secara bersamaan menggunakan Arduino. alat ini memiliki fitur yang dapat mengirimkan data hasil pembacaan sensor ke server thingspeak. untuk lebih jelasnya berikut adalah komponen dan kodingnya.
a. Komponen
- Arduino Mega Wifi
- Timbangan Loadcell Hx711
- Lcd 16x2 I2C
- Modul Sensor Photodioda
- Led HPL Putih
- Power Supply Switching
- DF Player Mini
- Speaker
b. Program Arduino Uno
#include <Wire.h>
#include "HX711.h"
#include <DFPlayer_Mini_Mp3.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial mySerial(2, 3); // RX, TX
HX711 scale(4, 5);
float tera = 0;
float berat;
float fix;
int x;
int telur1;
int telur2;
int telur3;
int telur4;
int telur5;
int telur6;
int tombol = 7;
int tombolx;
int batastelur = 900;
void setup() {
Serial.begin(9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
delay(1); //wait 1ms for mp3 module to set volume
mp3_set_volume (30); //volume 0-30
lcd.begin();
lcd.clear();
lcd.noCursor();
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
lcd.setCursor (0,0);
lcd.print ("LOADING... ");
delay(5000);
lcd.clear();
pinMode(tombol,INPUT_PULLUP);
}
void loop() {
berat = scale.get_units(10) ;
fix = (berat + 0.3893)/0.0442;
fix = fix - 9.0;
if(fix < 0){
fix = 0;
}
//jika telur jelek adc 1023
//jika telur bagus adc kurang dari 1023
telur1 = analogRead(A0);
telur2 = analogRead(A1);
telur3 = analogRead(A2);
telur4 = analogRead(A3);
telur5 = analogRead(A6);
telur6 = analogRead(A7);
lcd.setCursor(0,0);
lcd.print("W:");
lcd.print(fix);
lcd.print(" gr ");
lcd.setCursor(0,1);
lcd.print(telur1);
lcd.print(" ");
lcd.print(telur2);
lcd.print(" ");
lcd.print(telur3);
lcd.print(" ");
lcd.setCursor(0,2);
lcd.print(telur4);
lcd.print(" ");
lcd.print(telur5);
lcd.print(" ");
lcd.print(telur6);
lcd.print(" ");
tombolx = digitalRead(tombol);
if(tombolx == 0){
if((telur1 < batastelur)&&(telur2 < batastelur)&&(telur3 < batastelur)&&(telur4 < batastelur)&&(telur5 < batastelur)&&(telur6 < batastelur)){
mp3_play(7); //play music file 0007.mp3
delay(10000);
mp3_stop();
}
if((telur1 > batastelur)||(telur2 > batastelur)||(telur3 > batastelur)||(telur4 > batastelur)||(telur5 > batastelur)||(telur6 > batastelur)){
mp3_play(8);
delay(5000);
if(telur1 > batastelur){
mp3_play(1);
delay(5000);
}
if(telur2 > batastelur){
mp3_play(2);
delay(5000);
}
if(telur3 > batastelur){
mp3_play(3);
delay(5000);
}
if(telur4 > batastelur){
mp3_play(4);
delay(5000);
}
if(telur5 > batastelur){
mp3_play(5);
delay(5000);
}
if(telur6 > batastelur){
mp3_play(6);
delay(5000);
}
}
}
Serial.print("*");
Serial.print(berat);
Serial.print(",");
Serial.print(telur1);
Serial.print(",");
Serial.print(telur2);
Serial.print(",");
Serial.print(telur3);
Serial.print(",");
Serial.print(telur4);
Serial.print(",");
Serial.print(telur5);
Serial.print(",");
Serial.print(telur6);
Serial.println("#");
scale.power_down();
delay(200);
scale.power_up();
}
c. Program Arduino Mega
#include <Wire.h>
#include "HX711.h"
#include <DFPlayer_Mini_Mp3.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySerial(2, 3); // RX, TX
HX711 scale(4, 5);
float tera = 0;
int berat;
float fix;
int x;
int telur1;
int telur2;
int telur3;
int telur4;
int telur5;
int telur6;
int tombol = 7;
int tombolx;
void setup() {
Serial.begin(9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
delay(1); //wait 1ms for mp3 module to set volume
mp3_set_volume (30); //volume 0-30
lcd.begin();
lcd.clear();
lcd.noCursor();
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
lcd.setCursor (0,0);
lcd.print ("LOADING... ");
delay(5000);
lcd.clear();
pinMode(tombol,INPUT_PULLUP);
}
void loop() {
berat = scale.get_units(10) ;
//fix = (berat - 0.4233)/0.1586;
//jika telur jelek adc 1023
//jika telur bagus adc kurang dari 1023
telur1 = analogRead(A0);
telur2 = analogRead(A1);
telur3 = analogRead(A2);
telur4 = analogRead(A3);
telur5 = analogRead(A4);
telur6 = analogRead(A5);
lcd.setCursor(0,0);
lcd.print("W:");
lcd.print(berat);
lcd.print(" ");
lcd.print(telur1);
lcd.print(" ");
lcd.print(telur2);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(telur3);
lcd.print(" ");
lcd.print(telur4);
lcd.print(" ");
lcd.print(telur5);
lcd.print(" ");
lcd.print(telur6);
lcd.print(" ");
tombolx = digitalRead(tombol);
if(tombolx == 0){
if((telur1 < 1000)&&(telur2 < 1000)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(7); //play music file 0007.mp3
delay(10000);
mp3_stop();
}
if((telur1 > 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(1);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 > 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(2);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 > 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(3);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 > 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(4);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 > 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(5);
delay(5000);
mp3_stop();
}
if((telur1 < 1000 )&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 > 950)){
mp3_play(8);
delay(5000);
mp3_play(6);
delay(5000);
mp3_stop();
}
}
Serial.print("*");
Serial.print(berat);
Serial.print(",");
Serial.print(telur1);
Serial.print(",");
Serial.print(telur2);
Serial.print(",");
Serial.print(telur3);
Serial.print(",");
Serial.print(telur4);
Serial.print(",");
Serial.print(telur5);
Serial.print(",");
Serial.print(telur6);
Serial.println("#");
scale.power_down();
delay(200);
scale.power_up();
}
#include "HX711.h"
#include <DFPlayer_Mini_Mp3.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial mySerial(2, 3); // RX, TX
HX711 scale(4, 5);
float tera = 0;
int berat;
float fix;
int x;
int telur1;
int telur2;
int telur3;
int telur4;
int telur5;
int telur6;
int tombol = 7;
int tombolx;
void setup() {
Serial.begin(9600);
mySerial.begin (9600);
mp3_set_serial (mySerial); //set softwareSerial for DFPlayer-mini mp3 module
delay(1); //wait 1ms for mp3 module to set volume
mp3_set_volume (30); //volume 0-30
lcd.begin();
lcd.clear();
lcd.noCursor();
scale.set_scale(2280.f); // this value is obtained by calibrating the scale with known weights; see the README for details
scale.tare(); // reset the scale to 0
lcd.setCursor (0,0);
lcd.print ("LOADING... ");
delay(5000);
lcd.clear();
pinMode(tombol,INPUT_PULLUP);
}
void loop() {
berat = scale.get_units(10) ;
//fix = (berat - 0.4233)/0.1586;
//jika telur jelek adc 1023
//jika telur bagus adc kurang dari 1023
telur1 = analogRead(A0);
telur2 = analogRead(A1);
telur3 = analogRead(A2);
telur4 = analogRead(A3);
telur5 = analogRead(A4);
telur6 = analogRead(A5);
lcd.setCursor(0,0);
lcd.print("W:");
lcd.print(berat);
lcd.print(" ");
lcd.print(telur1);
lcd.print(" ");
lcd.print(telur2);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(telur3);
lcd.print(" ");
lcd.print(telur4);
lcd.print(" ");
lcd.print(telur5);
lcd.print(" ");
lcd.print(telur6);
lcd.print(" ");
tombolx = digitalRead(tombol);
if(tombolx == 0){
if((telur1 < 1000)&&(telur2 < 1000)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(7); //play music file 0007.mp3
delay(10000);
mp3_stop();
}
if((telur1 > 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(1);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 > 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(2);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 > 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(3);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 > 950)&&(telur5 < 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(4);
delay(5000);
mp3_stop();
}
if((telur1 < 1000)&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 > 950)&&(telur6 < 950)){
mp3_play(8);
delay(5000);
mp3_play(5);
delay(5000);
mp3_stop();
}
if((telur1 < 1000 )&&(telur2 < 950)&&(telur3 < 950)&&(telur4 < 950)&&(telur5 < 950)&&(telur6 > 950)){
mp3_play(8);
delay(5000);
mp3_play(6);
delay(5000);
mp3_stop();
}
}
Serial.print("*");
Serial.print(berat);
Serial.print(",");
Serial.print(telur1);
Serial.print(",");
Serial.print(telur2);
Serial.print(",");
Serial.print(telur3);
Serial.print(",");
Serial.print(telur4);
Serial.print(",");
Serial.print(telur5);
Serial.print(",");
Serial.print(telur6);
Serial.println("#");
scale.power_down();
delay(200);
scale.power_up();
}
d. Program ESP8266
#include <SPI.h>
#include <ESP8266WiFi.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
int berat;
int telur1;
int telur2;
int telur3;
int telur4;
int telur5;
int telur6;
int datain1;
int datain2;
int datain3;
int datain4;
int datain5;
int datain6;
int datain7;
String dataIn;
String dt[10];
int i;
boolean parsing = false;
String apiKey = "976GJHG9IKHGHGGF"; // 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";
WiFiClient client;
void setup()
{
dataIn="";
Serial.begin(9600);
delay(10);
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");
}
void loop()
{
while(Serial.available()>0) {
// dataIn="";
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(berat);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + berat + "&field2=" + telur1 + "&field3=" + telur2 + "&field4=" + telur3 + "&field5=" + telur4 + "&field6=" + telur5 + "&field7=" + telur6 + " 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);
}
}
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();
datain5 = dt[3].toInt();
datain6 = dt[3].toInt();
datain7 = dt[3].toInt();
berat = datain1 / 1;
telur1 = datain2 / 1;
telur2 = datain3 / 1;
telur3 = datain4 / 1;
telur4 = datain5 / 1;
telur5 = datain6 / 1;
telur6 = datain7 / 1;
}
#include <ESP8266WiFi.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
int value3;
int value4;
int value5;
int value6;
int berat;
int telur1;
int telur2;
int telur3;
int telur4;
int telur5;
int telur6;
int datain1;
int datain2;
int datain3;
int datain4;
int datain5;
int datain6;
int datain7;
String dataIn;
String dt[10];
int i;
boolean parsing = false;
String apiKey = "976GJHG9IKHGHGGF"; // 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";
WiFiClient client;
void setup()
{
dataIn="";
Serial.begin(9600);
delay(10);
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");
}
void loop()
{
while(Serial.available()>0) {
// dataIn="";
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(berat);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + berat + "&field2=" + telur1 + "&field3=" + telur2 + "&field4=" + telur3 + "&field5=" + telur4 + "&field6=" + telur5 + "&field7=" + telur6 + " 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);
}
}
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();
datain5 = dt[3].toInt();
datain6 = dt[3].toInt();
datain7 = dt[3].toInt();
berat = datain1 / 1;
telur1 = datain2 / 1;
telur2 = datain3 / 1;
telur3 = datain4 / 1;
telur4 = datain5 / 1;
telur5 = datain6 / 1;
telur6 = datain7 / 1;
}
d. VIDEO HASILNYA
No comments:
Post a Comment