#include <math.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SD.h> // sd library
#include <SPI.h> // sd library
#include <DS3231.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
SoftwareSerial mySerial(13,12);
DS3231 rtc;
char t[32];
File myFile; // file on sd card
int VaneValue;
int Direction;
int sensorValue;
float outvoltage;
int runway = 250;
float tailwind;
float crosswind;
float Level;
float averageKecepatan;
float averageArah;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
//LCD ukuran 20x4
lcd.begin(16, 2);
delay(1000);
lcd.clear();
lcd.backlight();
//membaca RTC
DateTime now = rtc.begin();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // following line sets the RTC to the date & time this sketch was compiled
// Set the current date, and time in the following format:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); // This line sets the RTC with an explicit date & time, for example to set
}
//membaca SD Card
Serial.print("Initializing SD Card...");
if (!SD.begin(4)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println(" Card initialized.");
String header = "Tanggal, Jam, Kecepatan, Arah, Tailwind, Crosswind";
File myFile = SD.open("Data.txt", FILE_WRITE);
if (myFile) {
myFile.println(" ");
myFile.println(header);
myFile.close();
Serial.println(" ");
Serial.println(header);
}
else {
Serial.println("error opening Data.txt");
}
}
void waktu_rtc() {
DateTime now = rtc.begin();
lcd.setCursor(0, 0);
if (now.day() < 10) lcd.print("0");
lcd.print(now.day(), DEC);
lcd.print("/");
if (now.month() < 10) lcd.print("0");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(12, 0);
if (now.hour() < 10) lcd.print("0");
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) lcd.print("0");
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10) lcd.print("0");
lcd.print(now.second(), DEC);
}
void sd_card() {
DateTime now = rtc.begin();
File myFile = SD.open("Data.txt", FILE_WRITE);
if(myFile) {
myFile.print(now.day(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.year(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
myFile.print(averageKecepatan);
myFile.print(",");
myFile.print(averageArah);
myFile.print(",");
myFile.print(tailwind);
myFile.print(",");
myFile.println(crosswind);
myFile.close();
if (now.day()<10) Serial.print("0");
Serial.print(now.day(), DEC);
Serial.print('/');
if (now.month()<10) Serial.print("0");
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.year(), DEC);
Serial.print(", ");
if (now.hour()<10) Serial.print("0");
Serial.print(now.hour(), DEC);
Serial.print(':');
if (now.minute()<10) Serial.print("0");
Serial.print(now.minute(), DEC);
Serial.print(':');
if (now.second()<10) Serial.print("0");
Serial.print(now.second(), DEC);
Serial.print(",");
Serial.print(averageKecepatan);
Serial.print(",");
Serial.print(averageArah);
Serial.print(",");
Serial.print(tailwind);
Serial.print(",");
Serial.print(crosswind);
Serial.println(" ");
}
else {
Serial.println("error opening Data.txt" );
}
}
void loop()
{
waktu_rtc();
float jumlahKecepatan = 0;
float jumlahArah = 0;
for (int i = 0; i < 60; i++) {
//Kecepatan angin
sensorValue = analogRead(A3);
outvoltage = sensorValue * (5.0 / 1023.0);
Level = ((6*outvoltage*1.23)-0.034)*1.94;
delay(10);
//Arah angin
VaneValue = analogRead(A4);
Direction = map(VaneValue, 0, 1023, 0, 360);
jumlahKecepatan = jumlahKecepatan + Level;
jumlahArah = jumlahArah + Direction;
Serial.print("jumlah kecepatan = ");
Serial.println(jumlahKecepatan);
Serial.print("jumlah arah = ");
Serial.println(jumlahArah);
delay(2000);
}
averageKecepatan = jumlahKecepatan/60;
averageArah = jumlahArah/60;
//tailwind
tailwind = averageKecepatan*cos((averageArah-runway)*(M_PI/180));
//crosswind
crosswind = averageKecepatan*sin((averageArah-runway)*(M_PI/180));
Serial.println();
sd_card();
lcd.setCursor(0, 1);
lcd.print("V = ");
lcd.setCursor(11, 1);
lcd.print("kts");
lcd.setCursor(5, 1);
lcd.print(averageKecepatan);
lcd.setCursor(0, 2);
lcd.print("D = ");
lcd.setCursor(5, 2);
lcd.print(averageArah);
lcd.setCursor(0, 3);
lcd.print("TW = ");
lcd.setCursor(5, 3);
lcd.print(tailwind);
lcd.setCursor(10, 3);
lcd.print("CW = ");
lcd.setCursor(15, 3);
lcd.print(crosswind);
delay(1000);
delay(2);
SendData();
if (mySerial.available())
Serial.write(mySerial.read());
}
void SendData()
{
int site = 1;
mySerial.println("AT");
delay(1000);
mySerial.println("AT+CPIN?");
delay(1000);
mySerial.println("AT+CREG?");
delay(1000);
mySerial.println("AT+CGATT?");
delay(1000);
mySerial.println("AT+CIPSHUT");
delay(1000);
mySerial.println("AT+CIPSTATUS");
delay(2000);
mySerial.println("AT+CIPMUX=0");
delay(2000);
ShowSerialData();
mySerial.println("AT+CSTT=\"internet\"");//start task and setting the APN,
delay(1000);
ShowSerialData();
mySerial.println("AT+CIICR");//bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println("AT+CIFSR");//get local IP adress
delay(2000);
ShowSerialData();
mySerial.println("AT+CIPSPRT=0");
delay(3000);
ShowSerialData();
mySerial.println("AT+CIPSTART=\"TCP\",\"45.32.109.192\",\"80\"");//start up the connection
delay(6000);
ShowSerialData();
mySerial.println("AT+CIPSEND");//begin send data to remote server
delay(4000);
ShowSerialData();
String str="GET http://45.32.109.192/Anemometer/tambahdata.php?data=" +String(site)+","+String(averageKecepatan)+","+String(averageArah)+","+String(crosswind)+","+String(tailwind);
mySerial.println(str);//begin send data to remote server
delay(4000);
ShowSerialData();
mySerial.println((char)26);//sending
delay(5000);//waitting for reply, important! the time is base on the condition of internet
mySerial.println();
ShowSerialData();
mySerial.println("AT+CIPSHUT");//close the connection
delay(100);
ShowSerialData();
}
void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}
can you help me