first commit
This commit is contained in:
commit
4e58b0c22d
|
@ -0,0 +1,98 @@
|
||||||
|
#include <TinyGPS++.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
static const int RXPin = 12, TXPin = 13; //RXPin 12 = D7 TXPin 13 = D6
|
||||||
|
static const uint32_t GPSBaud = 9600;
|
||||||
|
TinyGPSPlus gps;
|
||||||
|
SoftwareSerial ss(RXPin, TXPin);
|
||||||
|
|
||||||
|
const char* ssid = "Dimscull";
|
||||||
|
const char* password = "10102000";
|
||||||
|
|
||||||
|
const char* thingSpeakApiKey = "XJFU1VN466VZPI1W";
|
||||||
|
const char* thingSpeakChannelId = "2567862";
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
ss.begin(GPSBaud);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
|
{
|
||||||
|
delay(15000);
|
||||||
|
Serial.println("Connecting to WiFi...");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Connected to WiFi");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
while (ss.available() > 0)
|
||||||
|
{
|
||||||
|
if (gps.encode(ss.read()))
|
||||||
|
{
|
||||||
|
if (gps.location.isValid())
|
||||||
|
{
|
||||||
|
float latitude = gps.location.lat();
|
||||||
|
float longitude = gps.location.lng();
|
||||||
|
|
||||||
|
Serial.print("Latitude: ");
|
||||||
|
Serial.println(latitude, 6);
|
||||||
|
|
||||||
|
Serial.print("Longitude: ");
|
||||||
|
Serial.println(longitude, 6);
|
||||||
|
|
||||||
|
// Kirim data ke ThingSpeak
|
||||||
|
sendDataToThingSpeak(latitude, longitude);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("No GPS data available.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendDataToThingSpeak(float latitude, float longitude)
|
||||||
|
{
|
||||||
|
WiFiClient client;
|
||||||
|
|
||||||
|
// URL untuk permintaan HTTP POST ke ThingSpeak
|
||||||
|
String url = "/update?";
|
||||||
|
url += "api_key=";
|
||||||
|
url += thingSpeakApiKey;
|
||||||
|
url += "&field1=";
|
||||||
|
url += String(latitude, 6);
|
||||||
|
url += "&field2=";
|
||||||
|
url += String(longitude, 6);
|
||||||
|
|
||||||
|
// Hubungkan ke server ThingSpeak
|
||||||
|
if (client.connect("api.thingspeak.com", 80))
|
||||||
|
{
|
||||||
|
// Kirim permintaan HTTP POST ke ThingSpeak
|
||||||
|
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
|
||||||
|
"Host: api.thingspeak.com\r\n" +
|
||||||
|
"Connection: close\r\n\r\n");
|
||||||
|
|
||||||
|
// Tunggu hingga tanggapan dari server
|
||||||
|
while (!client.available())
|
||||||
|
delay(1);
|
||||||
|
|
||||||
|
// Cetak tanggapan dari server (opsional)
|
||||||
|
while (client.available())
|
||||||
|
{
|
||||||
|
Serial.write(client.read());
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Data berhasil dikirim ke ThingSpeak.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.println("Gagal terhubung ke ThingSpeak.");
|
||||||
|
}
|
||||||
|
client.stop();
|
||||||
|
}
|
Loading…
Reference in New Issue