Monitor Arus Sensor ACS712 dengan WEMOS D1 ESP8266 THINGSPEAK
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan esp8266 dengan sensor acs712 untuk monitoring arus dc. jadi alat ini hanya untuk monitoring arus saja menggunakan webserver thingspeak. untuk lebih jelasnya berikut adalah komponen dan kodingnya.
a. Wemos D1 ESP8266
b. Sensor Arus ACS712
c. Program Arduino IDE
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //library lcd
String apiKey = "76JHGJHNNTDFDFSE"; // Enter your Write API key from ThingSpeak
const char* resource = "/update?api_key=";
const char *ssid = "hotspot hpku"; // replace with your wifi ssid and wpa2 key
const char *pass = "123456789";
const char* server = "api.thingspeak.com";
int adcarus;
float v;
float arus;
WiFiClient client;
void setup()
{
lcd.begin();
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
delay(10);
Serial.println("Connecting to ");
Serial.println(ssid);
// Connect to WiFi network
Serial.println();
Serial.println();
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()
{
adcarus = analogRead(A0);
v = adcarus * (4.1 / 1023.0);
arus = (v - 2.5)/0.1;
if(arus <= 0.5 ){
arus = 0;
}
lcd.setCursor(0,0);
lcd.print("Arus: ");
lcd.print(arus);
lcd.print(" ");
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(arus);
postStr += "\r\n\r\n";
client.print(String("GET ") + resource + apiKey + "&field1=" + arus + " 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.print("arus: ");
Serial.print(arus);
Serial.println(". Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
delay(10000);
}
d. VIDEO HASILNYA
No comments:
Post a Comment