0% found this document useful (0 votes)
96 views13 pages

File 8

The document contains code for an Arduino program that controls 8 relays connected to devices based on data retrieved from Thingspeak. It defines the relay pins, establishes WiFi and REST client connections, then enters a loop to retrieve the last data value for each of 8 fields from Thingspeak and set the corresponding relay high or low based on the value. It also contains code for a web page that displays the status of each relay and buttons to toggle their states by updating the corresponding field in Thingspeak.

Uploaded by

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

File 8

The document contains code for an Arduino program that controls 8 relays connected to devices based on data retrieved from Thingspeak. It defines the relay pins, establishes WiFi and REST client connections, then enters a loop to retrieve the last data value for each of 8 fields from Thingspeak and set the corresponding relay high or low based on the value. It also contains code for a web page that displays the status of each relay and buttons to toggle their states by updating the corresponding field in Thingspeak.

Uploaded by

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

Kode Program Arduino

#include <SoftwareSerial.h>
#include <espduino.h>
#include <rest.h>

SoftwareSerial espPort(10, 11);


ESP esp(&espPort, &Serial, 9);
REST rest(&esp);
boolean wifiConnected = false;

const int relay1 = 2;


const int relay2 = 3;
const int relay3 = 4;
const int relay4 = 5;
const int relay5 = 6;
const int relay6 = 7;
const int relay7 = 8;
const int relay8 = 9;

boolean data1 = false;


boolean data2 = false;
boolean data3 = false;
boolean data4 = false;
boolean data5 = false;
boolean data6 = false;
boolean data7 = false;
boolean data8 = false;
int loop_count = 0;

char response[266];
char buff[64];
String strId,strData,strCode;
String strData_Last1,strData_Last2,strData_Last3,strData_Last4;

void(* resetFunc) (void) = 0;

void clearBuffer(void) {
for (int i = 0;i<266;i++ ) {
response[i]=0;
}
}

void wifiCb(void* response)


{
uint32_t status;
RESPONSE res(response);
if(res.getArgc() == 1) {
res.popArgs((uint8_t*)&status, 4);
if(status == STATION_GOT_IP) {
Serial.println("TERHUBUNG KE WIFI");

wifiConnected = true;
} else {
wifiConnected = false;
}

}
}

void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);

digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(relay5,HIGH);
digitalWrite(relay6,HIGH);
digitalWrite(relay7,HIGH);
digitalWrite(relay8,HIGH);

Serial.begin(9600);
espPort.begin(19200);

esp.enable();
delay(1000);
esp.reset();
delay(1000);
while(!esp.ready());

Serial.println("ARDUINO: Setup client");


if(!rest.begin("api.thingspeak.com")) {
Serial.println("ARDUINO: Gagal Setup client");
while(1);
}

Serial.println("ARDUINO: Menghubungkan dengan Wifi");


esp.wifiCb.attach(&wifiCb);

esp.wifiConnect("Oppo X9009","google123");
Serial.println("ARDUINO: System sudah siap!");
}

void loop() {
loop_start:

esp.process();
if(wifiConnected) {

char str_field1[6] , str_field2[6] , str_field3[6] , str_field4[6], str_field5[6], str_field6[6], str_field7[6],


str_field8[6];
loop_1:
sprintf(buff, "/channels/236342/fields/1/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();

if (strId == "1" || strId == "1.0" || strId == "1.00" ){


digitalWrite(relay1,LOW);
data1 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay1,HIGH);
data1 = false;
}
}
else{
goto loop_1;
}

delay(1000);
loop_2:
sprintf(buff, "/channels/236342/fields/2/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();
if (strId == "1" || strId == "1.0" || strId == "1.00"){
digitalWrite(relay2,LOW);
data2 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay2,HIGH);
data2 = false;
}
}
else{

clearBuffer();
goto loop_2;

delay(1000);
loop_3:
sprintf(buff, "/channels/236342/fields/3/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData(); // GET DATA

if (strId == "1" || strId == "1.0" || strId == "1.00"){


digitalWrite(relay3,LOW);
data3 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay3,HIGH);
data3 = false;
}
}
else{

clearBuffer();
goto loop_3;

}
delay(1000);
loop_4:
sprintf(buff, "/channels/236342/fields/4/last"); // field x last Data
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData(); // GET DATA

if (strId == "1" || strId == "1.0" || strId == "1.00"){


digitalWrite(relay4,LOW);
data4 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay4,HIGH);
data4 = false;
}
}
else{
clearBuffer();
goto loop_4;

delay(1000);
loop_5:
sprintf(buff, "/channels/236342/fields/5/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();

if (strId == "1" || strId == "1.0" || strId == "1.00"){


digitalWrite(relay5,LOW);
data2 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay5,HIGH);
data2 = false;
}
}
else{

clearBuffer();
goto loop_5;
}

delay(1000);
loop_6:
sprintf(buff, "/channels/236342/fields/6/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();

if (strId == "1" || strId == "1.0" || strId == "1.00"){


digitalWrite(relay6,LOW);
data2 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay6,HIGH);
data2 = false;
}
}
else{

clearBuffer();
goto loop_6;

delay(1000);
loop_7:
sprintf(buff, "/channels/236342/fields/7/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();
if (strId == "1" || strId == "1.0" || strId == "1.00"){
digitalWrite(relay7,LOW);
data2 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay7,HIGH);
data2 = false;
}
}
else{

clearBuffer();
goto loop_7;

delay(1000);
loop_8:
sprintf(buff, "/channels/236342/fields/8/last");
Serial.println(buff);

rest.get((const char*)buff);
if(rest.getResponse(response, 266) == HTTP_STATUS_OK){

strId = "";
getData();

if (strId == "1" || strId == "1.0" || strId == "1.00"){


digitalWrite(relay8,LOW);
data2 = true;
}

if (strId == "0" || strId == "-1" || strId == "0.1" || strId == "0.10"){


digitalWrite(relay8,HIGH);
data2 = false;
}
}
else{

clearBuffer();
goto loop_8;

delay(1000);
loop_count++;
Serial.println("LOOP : ");
Serial.println(loop_count);

else{
}

void getData(){
int i=0;

for (i = 0; i < 10; i++){

if((response[i] == '\r') || (response[i] == '\n')) {


}
else{
strId += response[i];
}

if (response[i] == '\n'){
i++;
break;
}
}

Serial.println("");
Serial.print("ID : ");
Serial.print(strId);
Serial.println("");
}
boolean hardReset() {
String tmpData;
}

Kode Program Web


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Laporan Akhir</title>
<link href="css/bootstrap.css" rel="stylesheet">

</head>
<style>
div.polaroid {
margin: auto;
width: 80%;
background-color: white;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
margin-bottom: 25px;
}
img {
border-radius: 8px;
}

div.polaroidbot {
text-align: center;
padding: 10px 20px;
}
</style>
<body>
<div class="container">
<div class="container">
<h1 style="text-align:center;">Laporan Akhir</h1>
<br>
<br>
<br>
</div>

<div class="polaroid">
<img src="a.jpg" alt="Norway" style="width:100%">
<div class="polaroidbot">
<p>Kendali Stop Kontak Melalui Internet. Anda Dapat dengan mudah mengendalikan perabotan
rumah anda seperti Televisi, Pompa Air, Kulkas, Penanak Nasi, Kipas, Air Conditioner, dan peralatan
lainnya yang terhubung langsung dengan Stop Kontak A.K.A memerlukan listrik</p>
</div>
</div>
<div class="container">
<br>
<br>
</div>
<div class="container">
<table style="width:100%">
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url1='https://api.thingspeak.com//channels/236342/fields/1/last';
$data=file_get_contents($url1);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field1=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field1=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 1</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url2='https://api.thingspeak.com//channels/236342/fields/1/last';
$data=file_get_contents($url2);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?>
</td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url3='https://api.thingspeak.com//channels/236342/fields/2/last';
$data=file_get_contents($url3);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field2=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field2=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 2</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url4='https://api.thingspeak.com//channels/236342/fields/2/last';
$data=file_get_contents($url4);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url5='https://api.thingspeak.com//channels/236342/fields/3/last';
$data=file_get_contents($url5);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field3=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field3=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 3</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url6='https://api.thingspeak.com//channels/236342/fields/3/last';
$data=file_get_contents($url6);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url7='https://api.thingspeak.com//channels/236342/fields/4/last';
$data=file_get_contents($url7);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field4=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field4=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 4</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url8='https://api.thingspeak.com//channels/236342/fields/4/last';
$data=file_get_contents($url8);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url9='https://api.thingspeak.com//channels/236342/fields/5/last';
$data=file_get_contents($url9);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field5=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field5=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 5</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url10='https://api.thingspeak.com//channels/236342/fields/5/last';
$data=file_get_contents($url10);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url11='https://api.thingspeak.com//channels/236342/fields/6/last';
$data=file_get_contents($url11);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field6=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field6=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 6</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url12='https://api.thingspeak.com//channels/236342/fields/6/last';
$data=file_get_contents($url12);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url13='https://api.thingspeak.com//channels/236342/fields/7/last';
$data=file_get_contents($url13);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field7=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field7=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 7</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url14='https://api.thingspeak.com//channels/236342/fields/7/last';
$data=file_get_contents($url14);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
<tr>
<td style="text-align: right;width:40%"><a href=https://<?php
$url15='https://api.thingspeak.com//channels/236342/fields/8/last';
$data=file_get_contents($url15);
if ($data == "1") {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field8=0";
} else {
echo "api.thingspeak.com/update?key=KWK24V4E5I9RBNZ0&field8=1";
} ?> target="iframe_a"><button class="btn btn-default"
onClick="document.location.reload(true)">Relay 8</button></a></td>
<td style="width:20%"></td>
<td style="width:40%"><?php
$url16='https://api.thingspeak.com//channels/236342/fields/8/last';
$data=file_get_contents($url16);
if ($data == "1") {
echo "Hidup";
} else {
echo "Mati";
} ?></td>
</tr>
</div>
<iframe src="demo_iframe.htm" height="200" width="300" name="iframe_a" hidden></iframe>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>

<!-- Div Modal -->


<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">

<!-- Modal content-->


<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
<div class="modal-footer">

</div>
</div>

</div>
</div>
<br>
<br>
<br>
</body>
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy