0% found this document useful (0 votes)
3 views6 pages

Deepseek

This document is an Arduino sketch that connects to WiFi and reads data from two INA219 sensors and a DHT22 temperature and humidity sensor. It sends the collected data to ThingSpeak every 20 seconds using HTTP requests. The code includes configurations for WiFi, ThingSpeak API, and sensor initialization, along with error handling for sensor readings and WiFi connectivity.

Uploaded by

yulian3924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Deepseek

This document is an Arduino sketch that connects to WiFi and reads data from two INA219 sensors and a DHT22 temperature and humidity sensor. It sends the collected data to ThingSpeak every 20 seconds using HTTP requests. The code includes configurations for WiFi, ThingSpeak API, and sensor initialization, along with error handling for sensor readings and WiFi connectivity.

Uploaded by

yulian3924
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <Wire.

h>

#include <Adafruit_INA219.h>

#include <DHT.h>

#include <WiFi.h>

#include <HTTPClient.h>

// Konfigurasi WiFi

const char* ssid = "Infinix HOT 50"; // SSID WiFi Anda

const char* password = "qwerty098"; // Password WiFi Anda

// Konfigurasi ThingSpeak

const char* thingspeakServer = "api.thingspeak.com";

String apiKey = "N0VLA5T04RB61QD2"; // Write API Key ThingSpeak Anda

// Konfigurasi DHT22

#define DHTPIN 4 // Pin data DHT22

#define DHTTYPE DHT22 // Tipe sensor DHT22

DHT dht(DHTPIN, DHTTYPE);

// Inisialisasi dua sensor INA219

Adafruit_INA219 ina219_1(0x40); // Alamat I2C untuk INA219 pertama

Adafruit_INA219 ina219_2(0x41); // Alamat I2C untuk INA219 kedua

// Variabel untuk menyimpan data

float shuntVoltage_1 = 0;
float busVoltage_1 = 0;

float current_1 = 0;

float power_1 = 0;

float shuntVoltage_2 = 0;

float busVoltage_2 = 0;

float current_2 = 0;

float power_2 = 0;

float temperature = 0;

float humidity = 0;

void setup() {

Serial.begin(115200);

delay(10);

// Mulai koneksi WiFi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

Serial.println("WiFi connected");

// Inisialisasi sensor INA219


if (!ina219_1.begin()) {

Serial.println("Gagal menemukan INA219 pertama");

while (1);

if (!ina219_2.begin()) {

Serial.println("Gagal menemukan INA219 kedua");

while (1);

// Inisialisasi sensor DHT22

dht.begin();

void loop() {

// Baca data dari INA219 pertama

shuntVoltage_1 = ina219_1.getShuntVoltage_mV();

busVoltage_1 = ina219_1.getBusVoltage_V();

current_1 = ina219_1.getCurrent_mA();

power_1 = ina219_1.getPower_mW();

// Baca data dari INA219 kedua

shuntVoltage_2 = ina219_2.getShuntVoltage_mV();

busVoltage_2 = ina219_2.getBusVoltage_V();

current_2 = ina219_2.getCurrent_mA();

power_2 = ina219_2.getPower_mW();
// Baca data dari DHT22

humidity = dht.readHumidity();

temperature = dht.readTemperature();

// Cek jika pembacaan DHT22 gagal

if (isnan(humidity) || isnan(temperature)) {

Serial.println("Gagal membaca data dari DHT22");

return;

// Tampilkan data di Serial Monitor

Serial.print("INA219 1 - Voltage: "); Serial.print(busVoltage_1); Serial.print(" V, Current: ");


Serial.print(current_1); Serial.print(" mA, Power: "); Serial.print(power_1); Serial.println(" mW");

Serial.print("INA219 2 - Voltage: "); Serial.print(busVoltage_2); Serial.print(" V, Current: ");


Serial.print(current_2); Serial.print(" mA, Power: "); Serial.print(power_2); Serial.println(" mW");

Serial.print("DHT22 - Temperature: "); Serial.print(temperature); Serial.print(" *C, Humidity: ");


Serial.print(humidity); Serial.println(" %");

// Kirim data ke ThingSpeak menggunakan HTTPClient

if (WiFi.status() == WL_CONNECTED) {

HTTPClient http;

// Buat URL untuk mengirim data ke ThingSpeak

String url = "http://" + String(thingspeakServer) + "/update?api_key=" + apiKey +

"&field1=" + String(busVoltage_1) +

"&field2=" + String(current_1) +
"&field3=" + String(power_1) +

"&field4=" + String(busVoltage_2) +

"&field5=" + String(current_2) +

"&field6=" + String(power_2) +

"&field7=" + String(temperature) +

"&field8=" + String(humidity);

// Mulai koneksi HTTP

http.begin(url);

// Kirim HTTP GET request

int httpResponseCode = http.GET();

if (httpResponseCode > 0) {

Serial.print("HTTP Response code: ");

Serial.println(httpResponseCode);

String response = http.getString();

Serial.println(response);

} else {

Serial.print("Error code: ");

Serial.println(httpResponseCode);

// Tutup koneksi HTTP

http.end();

} else {
Serial.println("WiFi terputus");

// Tunggu sebelum mengirim data berikutnya

delay(20000); // Kirim data setiap 20 detik

You might also like