Monitor PH dan Turbidity (kekeruhan air) Server Thingspeak
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa mengukur PH dan kekeruhan air (Turbidity). alat ini mengirimkan data sensor ke server Thingspeak menggunakan Arduino Uno WIFI. untuk lebih jelasnya berikut adalah koding dan komponennya.
Konfigurasi Switch UNO WIFI
sumber link: LINK
a. Skema Alat
b. Program Arduino Uno
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
float pHValue;
int adc;
int adcPH;
int pHValuex;
float ntu;
float ntunew;
int adcntu;
float volt;
void setup(){
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop(){
adcPH = analogRead(A0); //menggunakan pin A0 untuk membaca output sensor pH
pHValue = (adcPH + 3.026) / 98.009;
pHValuex = pHValue * 100.0;
volt = 0;
for(int i=0; i<800; i++)
{
volt += ((float)analogRead(A1)/1023)*5;
}
volt = volt/800;
volt = round_to_dp(volt,2);
if(volt < 2.5){
ntu = 3000;
}else{
ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
}
ntunew = ntu / 1;
lcd.setCursor(0,0);
lcd.print("PH:");
lcd.print(pHValue,2);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("NTU= ");
lcd.print(ntunew);
lcd.print(" ");
Serial.print("*");
Serial.print(pHValuex);
Serial.print(",");
Serial.print(ntunew * 100.0);
Serial.println("#");
delay(1000);
}
float round_to_dp( float in_value, int decimal_place )
{
float multiplier = powf( 10.0f, decimal_place );
in_value = roundf( in_value * multiplier ) / multiplier;
return in_value;
}
c. Program ESP8266
#include <SPI.h>
#include <ESP8266WiFi.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
float ph;
float ntu;
int datain1;
int datain2;
String dataIn;
String dt[10];
int i;
boolean parsing = false;
String apiKey = "7YGF2HJ3KHG678JH3"; // 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(ph);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + ph + "&field2=" + ntu + " 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();
ph = datain1 / 100.0;
ntu = datain2 / 100.0;
}
d. Interface Thingspeak
e. VIDEO HASILNYA
No comments:
Post a Comment