Weather Station (Wind Speed Anemometer + Arah Mata Angin + Suhu Udara) Arduino
b. Program Arduino IDE
#include <Wire.h> // i2C Conection Library
#include "DHT.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h> //i2C LCD Library
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTTYPE DHT22
#define DHTPIN 8
DHT dht(DHTPIN, DHTTYPE);
// anemometer parameters
volatile byte rpmcount; // count signals
volatile unsigned long last_micros;
unsigned long timeold;
unsigned long timemeasure = 2.00; // seconds
int timetoSleep = 1; // minutes
unsigned long sleepTime = 15; // minutes
unsigned long timeNow;
int countThing = 0;
int GPIO_pulse = 2; // Arduino = D2
float rpm, rps; // frequencies
float radius = 0.1; // meters - measure of the lenght of each the anemometer wing
float velocity_kmh; // km/h
float velocity_ms; //m/s
float omega = 0; // rad/s
float calibration_value = 2.0;
float t;
SoftwareSerial dataserial(7, 6); // D7,D6
String data, arah_angin, s_angin;
int a, b;
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
dht.begin();
dataserial.begin(9600);
pinMode(GPIO_pulse, INPUT_PULLUP);
digitalWrite(GPIO_pulse, LOW);
detachInterrupt(digitalPinToInterrupt(GPIO_pulse)); // force to initiate Interrupt on zero
attachInterrupt(digitalPinToInterrupt(GPIO_pulse), rpm_anemometer, RISING); //Initialize the intterrupt pin
rpmcount = 0;
rpm = 0;
timeold = 0;
timeNow = 0;
}
void loop()
{
t = dht.readTemperature();
//Measure RPM
if ((millis() - timeold) >= timemeasure * 1000)
{
//countThing++;
detachInterrupt(digitalPinToInterrupt(GPIO_pulse)); // Disable interrupt when calculating
rps = float(rpmcount) / float(timemeasure); // rotations per second
rpm = 60 * rps; // rotations per minute
omega = 2 * PI * rps; // rad/s
velocity_ms = omega * radius * calibration_value; // m/s
velocity_kmh = velocity_ms * 3.6; // km/h
timeold = millis();
rpmcount = 0;
attachInterrupt(digitalPinToInterrupt(GPIO_pulse), rpm_anemometer, RISING); // enable interrupt
}
lcd.setCursor(0, 0);
lcd.print("S/T:");
lcd.print(velocity_ms,1);
lcd.print("/");
lcd.print(t,1);
lcd.print(" ");
Serial.print("*");
Serial.print(velocity_ms*100);
Serial.print(",");
Serial.print(t*100);
Serial.print(",");
Serial.print(s_angin);
Serial.println("#");
bacaarahangin();
}
void rpm_anemometer()
{
if (long(micros() - last_micros) >= 5000)
{ // time to debounce measures
rpmcount++;
last_micros = micros();
}
}
void bacaarahangin(){
if (dataserial.available()) // Jika ada data yang diterima dari sensor
{
data = dataserial.readString(); // data yang diterima dari sensor berawalan tanda * dan diakhiri tanda #, contoh *1#
a = data.indexOf("*"); // a adalah index tanda *
b = data.indexOf("#"); // b adalah index tanda #
s_angin = data.substring(a + 1, b); // membuang tanda * dan # sehingga di dapat nilai dari arah angin
if (s_angin.equals("1")) { // jika nilai dari sensor 1 maka arah angin utara
arah_angin = "utara ";
}
if (s_angin.equals("2")) {
arah_angin = "timur laut";
}
if (s_angin.equals("3")) {
arah_angin = "timur ";
}
if (s_angin.equals("4")) {
arah_angin = "tenggara ";
}
if (s_angin.equals("5")) {
arah_angin = "selatan ";
}
if (s_angin.equals("6")) {
arah_angin = "barat daya";
}
if (s_angin.equals("7")) {
arah_angin = "barat ";
}
if (s_angin.equals("8")) {
arah_angin = "barat laut";
}
lcd.setCursor(0, 1); // menampilkan pada lcd kolom 0, baris 0
lcd.print(arah_angin);
}
}
c. Program ESP8266
#include <SPI.h>
#include <ESP8266WiFi.h>
int temp;
int x = 5;
int y;
int value1;
int value2;
int value3;
float speednya;
float t;
int arah;
int datain1;
int datain2;
int datain3;
String dataIn;
String dt[10];
int i;
boolean parsing = false;
String apiKey = "7687JHGJHF6576GJ"; // 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(speednya);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + speednya + "&field2=" + t + "&field3=" + arah + " 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();
speednya = datain1 / 100.0;
t = datain2 / 100.0;
arah = datain3 / 1;
}
d. VIDEO HASILNYA
No comments:
Post a Comment