dirakit community
Published © CC BY

Kontrol Lampu dengan Agnosthings API

Kontrol Lampu dengan Agnosthings API menggunakan board NodeMCU ESP8266

IntermediateProtip1 hour92
Kontrol Lampu dengan Agnosthings API

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Grove - Relay
Seeed Grove - Relay
×1
Jumper wires (generic)
Jumper wires (generic)
×3

Software apps and online services

Arduino IDE
Arduino IDE
api agnosthings

Story

Read more

Code

sketch

Arduino
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>

ESP8266WiFiMulti WiFiMulti;

int lampu = 0; //pin pada lampu adalah pin 3 pada nodemcu

void setup()
{
    Serial.begin(9600);

    WiFiMulti.addAP("akusukakamu", "akujugasuka"); //nama wifi dan password wifi

    pinMode(lampu, OUTPUT);
}

void loop()
{
    if((WiFiMulti.run() == WL_CONNECTED))
    {
        HTTPClient http;

        http.begin("http://agnosthings.com/1cd82030-4ec0-11e6-8001-005056805279/channel/last/feed/timestamp/588"); //copy paste URL API Last feed's timestamp dari agnosthings.com

        int httpCode = http.GET();

        if(httpCode > 0)
        {
    
            Serial.printf("[HTTP] GET... code: %d\n", httpCode);

            if(httpCode == HTTP_CODE_OK)
            {
                String json = http.getString();
                Serial.println(json);

                if(json == "{\"value\":\"hidup\",\"code\":200}"){
                    digitalWrite(lampu, LOW); //relay module yang saya gunakan adalah active low, sehingga di sini ditulis LOW
                } 
                if(json == "{\"value\":\"mati\",\"code\":200}"){
                    digitalWrite(lampu, HIGH); //mematikan relay module
                }
            }

        } else {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
        }
        http.end();
    }

    delay(5000);
}

Credits

dirakit community

dirakit community

5 projects • 64 followers
Indonesia IoT Community

Comments

Add projectSign up / Login