#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "KalmanFilter.h"
const char* ssid = "BC SMKN 2 Polines 4G";
const char* password = "1sampai8";
const char* serverUrl = "http://192.168.0.104:8000/api/baca-sensor";
// PIN SENSOR
#define ARUS_SENSOR_PIN A0
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature SensorDS(&oneWire);
WiFiClient client;
HTTPClient http;
Adafruit_MPU6050 mpu;
Adafruit_BMP280 bmp;
KalmanFilter kalmanX;
KalmanFilter kalmanY;
float initialRoll, initialPitch, roll, pitch;
float temperature, pressure, altitude;
float arus;
unsigned long timer;
unsigned long lastMsg = 0;
const long interval = 10000; // interval send data
void setup() {
Serial.begin(115200);
// Setup semua sensor
setupIMU();
setupBMP();
setupSensorArus();
setupDS18B20();
// Setup koneksi
setup_wifi();
}
void loop() {
unsigned long now = millis();
if (now - lastMsg > interval) {
lastMsg = now;
// Baca semua nilai sensor terbaru
updateIMU();
updateBMP();
updateSensorArus();
updateDS18B20();
// Buat format data JSON
char jsonPayload[256];
snprintf(jsonPayload, sizeof(jsonPayload),
"{\"pitch\":%.2f,\"roll\":%.2f,\"temperature\":%.2f,\"pressure\":%.2f,\"altitude\":%.2f,\"current\":%.2f}",
pitch, roll, temperature, pressure, altitude, arus);
//POST ke server
http.begin(client, serverUrl);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonPayload); // Kirim request POST
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
Serial.print("Response: ");
Serial.println(response);
} else {
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
}
http.end();
Serial.println(jsonPayload);
}
}