Você está na página 1de 48

Motor stepper

const int in1Pin = 8;


const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;
const int btnOff = 5;
const int btnCcw = 6;
int i;

const int btnCw = 4 ;


const int btnGab = 7 ;

byte kode,kode1=0;

void stepper_cw() {
digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(10);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(10);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);digitalWrite(in4Pin,LOW);
delay(10);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,HIGH);
delay(10); }

void stepper_ccw() {
//ubah urutannya kebalikannya yg stepper_cw
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,HIGH);
delay(10);

digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);digitalWrite(in4Pin,LOW);
delay(10);

digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(10);

digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(10);
}
void stepper_off()
{
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
}

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);pinMode(in4Pin, OUTPUT);
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
Serial.println("Kendali Motor DC Stepper Unipolar Berbasis Serial");

Serial.println("Tekan '1' utk Motor DC Stepper CCW 2 putaran ");


Serial.println("Tekan '2' utk Motor DC Stepper CW Continue");
Serial.println("Tekan '3' utk Motor DC Stepper Bergantian 1 putaran");

Serial.println("Tekan 's' utk Motor DC Stepper OFF");


}
void loop() {
if (digitalRead(btnOff)==LOW)
{
kode1=0;
}

if (digitalRead( btnCw)==LOW)
{
kode1=3;
}

if (digitalRead( btnCcw)==LOW)
{
for(i=1;1<=1200;i++)
stepper_ccw();

kode1=0;
}

if (digitalRead( btnGab)==LOW)
{

kode1=4;
}
if(Serial.available()) {

kode = Serial.read();

if(kode=='1')
{ Serial.println("Motor DC Stepper CCW 2 Putaran");

kode1=2; }

if(kode=='2')
{ Serial.println("Motor DC Stepper CW Continue");
kode1=3; }

if(kode=='s')
{ Serial.println("Motor DC Stepper OFF");
kode1=0; }

if(kode=='3')
{ Serial.println("Motor DC Stepper bergantian ");
kode1=4; }

}
{

if(kode1==2)
{
for(i=1;i<=1100;i++)
stepper_ccw();

kode1=0;
}
if(kode1==3)
stepper_cw();

if( kode1==4)
{
for(i=1;i<=600;i++)
{
stepper_ccw();
}
delay(500);

for(i=1;i<=6000;i++)
{
stepper_cw();
}
delay(500);

if(kode1==0)
stepper_off();
}
}
Wemos

#include <ESP8266WiFi.h>
WiFiServer server(80); //Initialize the server on Port 80
const int in1Pin = 8;
const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;

//Looking under the hood


Serial.println("Somebody has connected :)");
String request = client.readStringUntil('\r');
Serial.println(request);

if (request.indexOf("/OFF") != -1){
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);
}
if (request.indexOf("/CW") != -1){
digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);digitalWrite(in4Pin,LOW);
delay(100);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,HIGH);
delay(100);
}

if (request.indexOf("/CCW") != -1){
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,HIGH);
delay(100);

digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,HIGH);digitalWrite(in4Pin,LOW);
delay(100);

digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);

digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);
}
void setup() {

WiFi.mode(WIFI_AP);
//Our ESP8266-12E is an AccessPoint
WiFi.softAP("Hello_IoT", "12345678");
// Provide the (SSID, password); .
server.begin();
// Start the HTTP Server
Serial.begin(115200);
//Start communication between the ESP8266-12E and the monitor window
IPAddress HTTPS_ServerIP= WiFi.softAPIP();
// Obtain the IP of the Server
Serial.print("Server IP is: ");
// Print the IP to the monitor
window Serial.println(HTTPS_ServerIP);
pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);pinMode(in4Pin, OUTPUT);
digitalWrite(in1Pin,LOW);
digitalWrite(in2Pin,LOW);
digitalWrite(in3Pin,LOW);
digitalWrite(in4Pin,LOW);

}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}

String s = "HTTP/1.1 200 OK\r\n" ;


s += "Content-Type: text/html\r\n\r\n"; s += "<!DOCTYPE HTML>\r\n<html>\r\n";
s += "<head><title>Wemos D1 R2 WIFI AP - Web Server LED
Control</title></head>”; s += "<body><h1>Wemos D1 R2 WIFI AP - Web Server
LED Control</h1>”; s += "<br><hr><br>";
s += "<br><input type=\"button\" name=\"b1\" value=\"Motor Stepper CW\"
onclick=\"location.href='CW'\">"; s += "&nbsp;&nbsp;&nbsp;";
s += "<br><input type=\"button\" name=\"b1\" value=\"Motor Stepper OFF\"
onclick=\"location.href='OFF'\">"; s += "</body></html>\n";
s += "<br><input type=\"button\" name=\"b1\" value=\"Motor Stepper CCW\"
onclick=\"location.href='CCW'\">"; s += "&nbsp;&nbsp;&nbsp;";
//Serve the HTML document to the browser. client.flush(); //clear previous info in the
stream client.print(s); // Send the response to the client delay(1);
Serial.println("Client disconnected"); }
1. Arduino Kendali LED Blink Momentary dg 2 Button

const int ledPin = 13; // the pin that the LED is


attached to const int btnStart = 2; const int btnStop = 3;

void led_blink() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000); }

void setup() {
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);digitalWrite(ledPin,LOW);
pinMode(btnStart, INPUT); digitalWrite(btnStart,HIGH);
pinMode(btnStop, INPUT); digitalWrite(btnStop,HIGH);
}

void loop() {
// check if data has been sent from the computer:
if(digitalRead(btnStart)==LOW)
{
//led blink 5x
led_blink();
led_blink();
led_blink();
led_blink();
led_blink();
}
if(digitalRead(btnStop)==LOW)
digitalWrite(ledPin,LOW); //matikan led
}
2. Arduino Kendali LED Blink Continue dg 2 Button

const int ledPin = 13; // the pin that the LED is


attached to const int btnStart = 2; const int btnStop = 3;
byte kode = 0;

void led_blink() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000); }

void setup() {
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);digitalWrite(ledPin,LOW);
pinMode(btnStart, INPUT); digitalWrite(btnStart,HIGH);
pinMode(btnStop, INPUT); digitalWrite(btnStop,HIGH);
}

void loop() {
// check if data has been sent from the computer:
if(digitalRead(btnStart)==LOW)
kode=1;
if(digitalRead(btnStop)==LOW)
kode=0;

if(kode==0) digitalWrite(ledPin,LOW);
//matikan led
if(kode==1)
led_blink();
}
3. Arduino Kendali LED Blink Momentary dan Continue dg 3 Button

const int ledPin = 13; // the pin that the LED is


attached to const int btnOff = 2; const int btnMomentary
= 3; const int btnContinue = 4; byte kode = 0;

void led_blink() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000); }

void setup() {
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);digitalWrite(ledPin,LOW);
pinMode(btnOff, INPUT); digitalWrite(btnOff,HIGH);
pinMode(btnMomentary, INPUT);
digitalWrite(btnMomentary,HIGH); pinMode(btnContinue,
INPUT); digitalWrite(btnContinue,HIGH);
}

void loop() {
// check if data has been sent from the computer:
if(digitalRead(btnMomentary)==LOW)
kode=1;
if(digitalRead(btnContinue)==LOW)
kode=2;
if(digitalRead(btnOff)==LOW)
kode=0;

if(kode==0) digitalWrite(ledPin,LOW);
//matikan led
if(kode==1) {
led_blink(); kode=0;
}
if(kode==2)
led_blink();
}
4. Arduino Kendali LED ON/OFF Berbasis Komunikasi Serial dg PC

const int ledPin = 13; // the pin that the LED is attached to

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
Serial.println("Kendali LED Serial");
Serial.println("Tekan '1' utk ON");
Serial.println("Tekan '2' utk OFF");
}

void loop() {
byte kode;

// check if data has been sent from the computer:


if (Serial.available()) {
// read the most recent byte (which will be from 0 to
255): kode = Serial.read(); if(kode=='1')
{
digitalWrite(ledPin,HIGH);
Serial.println("LED ON");
}
if(kode=='2')
{
digitalWrite(ledPin,LOW);
Serial.println("LED OFF");
}
}
}
5. Arduino Kendali LED Blink Momentary Berbasis Komunikasi Serial dg PC

const int ledPin = 13; // the pin that the LED is attached to

void led_blink() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000); }

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
Serial.println("Kendali LED Blink Momentary Serial");
Serial.println("Tekan '1' utk Blink 5x ");

void loop() {
byte kode;

// check if data has been sent from the computer:


if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
led_blink();Serial.println("LED Blink 1");
led_blink();Serial.println("LED Blink 2");
led_blink();Serial.println("LED Blink 3");
led_blink();Serial.println("LED Blink 4");
led_blink();Serial.println("LED Blink 5");

}
}
6. Arduino Kendali LED Blink Continue Berbasis Komunikasi Serial dg PC

const int ledPin = 13; // the pin that the LED is attached
to byte kode,kode1=0;

void led_blink() {
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000); }

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
Serial.println("Kendali LED Blink Continue Serial");
Serial.println("Tekan '1' utk LED Blink ");
Serial.println("Tekan '2' utk LED OFF ");
}

void loop() {
// check if data has been sent from the computer:
if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
Serial.println("LED Blink");
kode1=1;
}
if(kode=='2')
{
Serial.println("LED OFF");
kode1=0;
}
}

if(kode1==1)
led_blink();
if(kode1==0)
digitalWrite(ledPin,LOW);
}
7. Arduino Kendali Motor DC ON/OFF Berbasis Komunikasi Serial dg PC

const int MotorPin = 7;


byte kode;

void setup() {
Serial.begin(9600);
pinMode(MotorPin, OUTPUT); digitalWrite(MotorPin, LOW);
Serial.println("Kendali Motor DC ON/OFF PWM Berbasis
Serial");
Serial.println("Tekan '1' utk Motor DC ON");
Serial.println("Tekan '2' utk Motor DC ON 10 detik");
Serial.println("Tekan 's' utk Motor DC OFF ");
}

void loop() {
// check if data has been sent from the computer:
if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
Serial.println("Motor DC ON");
digitalWrite(MotorPin, HIGH);
}
if(kode=='2')
{
Serial.println("Motor DC ON 10
detik"); digitalWrite(MotorPin,
HIGH); delay(10000);
digitalWrite(MotorPin, LOW);
}
if(kode=='s')
{
Serial.println("Motor DC OFF");
digitalWrite(MotorPin, LOW);
}
}
}
8. Arduino Kendali Motor DC ON/OFF PWM Berbasis Komunikasi Serial dg PC

const int MotorPin = 9; // pastikan pin PWM


byte kode;

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(MotorPin, OUTPUT);
Serial.println("Kendali Motor DC ON/OFF PWM Berbasis
Serial");
Serial.println("Tekan '1' utk Motor DC ON Low Speed ");
Serial.println("Tekan '2' utk Motor DC ON Medium Speed ");
Serial.println("Tekan '3' utk Motor DC ON High Speed ");
Serial.println("Tekan 's' utk Motor DC OFF ");
}

void loop() {
// check if data has been sent from the computer:
if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
Serial.println("Motor DC ON Low Speed");
analogWrite(MotorPin,10);
}
if(kode=='2')
{
Serial.println("Motor DC ON Medium Speed");
analogWrite(MotorPin,127);
}
if(kode=='3')
{
Serial.println("Motor DC ON High Speed");
analogWrite(MotorPin,255);
}
if(kode=='s')
{
Serial.println("Motor DC OFF");
analogWrite(MotorPin,0);
}
}
}
9. Arduino Kendali Motor DC CW CCW Berbasis Komunikasi Serial dg PC

const int in1Pin = 6; const int in2Pin = 7; byte kode; void


mtr_cw() { digitalWrite(in1Pin,
HIGH);digitalWrite(in2Pin, LOW);
}
void mtr_ccw() { digitalWrite(in1Pin,
LOW);digitalWrite(in2Pin, HIGH);
}
void mtr_off() { digitalWrite(in1Pin,
LOW);digitalWrite(in2Pin, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);
digitalWrite(in1Pin, LOW);digitalWrite(in2Pin, LOW);
Serial.println("Kendali Motor DC CW CCW Berbasis Serial");
Serial.println("Tekan '1' utk Motor DC CW "); Serial.println("Tekan '2' utk Motor
DC CCW ");
Serial.println("Tekan '3' utk Motor DC CW 10 detik ");
Serial.println("Tekan 's' utk Motor DC OFF ");
} void loop() { if
(Serial.available()) {
kode = Serial.read();
if(kode=='1')
{
Serial.println("Motor DC CW"); mtr_cw();
}
if(kode=='2')
{
Serial.println("Motor DC CCW"); mtr_ccw();
}
if(kode=='3')
{
Serial.println("Motor DC CW 10 detik"); mtr_cw(); delay(10000); mtr_off();
}
if(kode=='s')
{
Serial.println("Motor DC OFF"); mtr_off();
}
}
}
10. Arduino Kendali Motor DC CW CCW PWM Berbasis Komunikasi Serial dg PC

const int in1Pin = 6; const


int in2Pin = 7;
const int speedPin = 9; // pastikan pin PWM
byte kode;

void mtr_cw() { digitalWrite(in1Pin,


HIGH);digitalWrite(in2Pin, LOW);
} void mtr_ccw() { digitalWrite(in1Pin,
LOW);digitalWrite(in2Pin, HIGH);
} void mtr_off() { digitalWrite(in1Pin,
LOW);digitalWrite(in2Pin, LOW);
}
void setup() {
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);
digitalWrite(in1Pin, LOW);digitalWrite(in2Pin, LOW);
pinMode(speedPin, OUTPUT);analogWrite(speedPin,0);
Serial.println("Kendali Motor DC CW CCW PWM Berbasis
Serial");
Serial.println("Tekan '1' utk Motor DC CW Low Speed");
Serial.println("Tekan '2' utk Motor DC CCW Medium Speed");
Serial.println("Tekan '3' utk Motor DC CW High Speed");
Serial.println("Tekan 's' utk Motor DC OFF ");
}

void loop() {
// check if data has been sent from the computer:
if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
Serial.println("Motor DC CW Low Speed");
analogWrite(speedPin,55); mtr_cw();
}
if(kode=='2')
{
Serial.println("Motor DC CCW Medium Speed");
analogWrite(speedPin,127); mtr_ccw();
}
if(kode=='3') {
Serial.println("Motor DC CW High Speed");
analogWrite(speedPin,255);mtr_cw();
}
if(kode=='s')
{
Serial.println("Motor DC OFF");
analogWrite(speedPin,0); mtr_off();
}
}
}
11. Arduino Kendali Motor DC Stepper Unipolar Berbasis Komunikasi Serial dg PC

const int in1Pin = 8;


const int in2Pin = 9;
const int in3Pin = 10;
const int in4Pin = 11;
byte kode,kode1=0;

void stepper_cw() {
digitalWrite(in1Pin,HIGH);digitalWrite(in2Pin,LOW);
digitalWrite(in13Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,HIGH;
digitalWrite(in13Pin,LOW);digitalWrite(in4Pin,LOW);
delay(100);

digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW
);
digitalWrite(in13Pin,HIGH);digitalWrite(in4Pin,LO
W); delay(100);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW
);
digitalWrite(in13Pin,LOW);digitalWrite(in4Pin,HIG
H); delay(100); }

void stepper_ccw() {
//ubah urutannya kebalikannya yg stepper_cw
}

void stepper_off()
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in13Pin,LOW);digitalWrite(in4Pin,LOW);
}

void setup() {
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(in1Pin, OUTPUT);pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);pinMode(in4Pin, OUTPUT);
digitalWrite(in1Pin,LOW);digitalWrite(in2Pin,LOW);
digitalWrite(in13Pin,LOW);digitalWrite(in4Pin,LOW);
Serial.println("Kendali Motor DC Stepper Unipolar Berbasis
Serial");
Serial.println("Tekan '1' utk Motor DC Stepper CW 20 Steps");
Serial.println("Tekan '2' utk Motor DC Stepper CCW 20 Steps");
Serial.println("Tekan '3' utk Motor DC Stepper CW Continue");
Serial.println("Tekan '4' utk Motor DC Stepper CCW Continue");
Serial.println("Tekan 's' utk Motor DC Stepper OFF");
}

void loop() {
// check if data has been sent from the computer:
if (Serial.available()) { //
read the most recent byte
kode = Serial.read();
if(kode=='1')
{
Serial.println("Motor DC Stepper CW 20 Steps");
kode1=1;
}
if(kode=='2')
{
Serial.println("Motor DC Stepper CCW 20 Steps");
kode1=2;
}
if(kode=='3')
{
Serial.println("Motor DC Stepper CW Continue");
kode1=3;
}
if(kode=='4')
{
Serial.println("Motor DC Stepper CCW Continue");
kode1=4;
}
if(kode=='s')
{
Serial.println("Motor DC Stepper OFF");
kode1=0;
}
}

if(kode1=='1')
{

stepper_cw();stepper_cw();stepper_cw();
stepper_cw();stepper_cw(); kode1=0;
}
if(kode1=='2')
{

stepper_ccw();stepper_ccw();stepper_ccw();
stepper_ccw();stepper_ccw(); kode1=0;
} if(kode1=='3')
stepper_cw();
if(kode1=='4')
stepper_ccw();
if(kode1=='0’)
stepper_off();
}
12. Arduino Ethernet Shield W5100 Web Client

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.


// Newer Ethernet shields have a MAC address printed on a
sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE,
0xEF, 0xFE, 0xED }; // if you don't want to use DNS (and
reduce your sketch size) // use the numeric IP instead of the
name for the server:
//IPAddress server(74,125,232,128); // numeric IP for
Google (no DNS) char server[] = "www.google.com"; //
name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign


IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client


library // with the IP address and
port of the server
// that you want to connect to (port 80 is default for
HTTP): EthernetClient client;

void setup() {
// Open serial communications and wait for
port to open: Serial.begin(9600); while
(!Serial) {
; // wait for serial port to connect. Needed for native
USB port only }

// start the Ethernet


connection: if
(Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet
using DHCP"); // try to congifure using IP
address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to
initialize: delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via


serial: if (client.connect(server, 80)) {
Serial.println("connected"); // Make a
HTTP request: client.println("GET
/search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}

void loop() {
// if there are incoming bytes
available // from the server, read
them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop


the client: if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

// do nothing forevermore:
while (true);
}
}
13. Arduino Ethernet Shield W5100 Web Server Display Analog Input Pins

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.


// The IP address will be dependent on your
local network: byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

// Initialize the Ethernet server


library // with the IP address and
port you want to use // (port 80 is
default for HTTP): EthernetServer
server(80);

void setup() {
// Open serial communications and wait for
port to open: Serial.begin(9600); while
(!Serial) {
; // wait for serial port to connect. Needed for native
USB port only }

// start the Ethernet connection and


the server: Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop() {
// listen for incoming clients
EthernetClient client =
server.available(); if (client) {
Serial.println("new client");
// an http request ends with a
blank line boolean
currentLineIsBlank = true;
while (client.connected()) { if
(client.available()) { char c
= client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has
ended,
// so you can send a reply if
(c == '\n' && currentLineIsBlank) {
// send a standard http response
header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type:
text/html");
client.println("Connection: close"); // the connection will be closed after
completion of the response client.println("Refresh: 5"); // refresh the page
automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Analog Pins
Monitoring</title>"); client.println("</head>");
client.println("<body>");
client.println("<h1>Arduino Analog Pins
Monitoring</h1>");
client.println("<hr />");
client.println("<br />");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6;
analogChannel++) { int sensorReading =
analogRead(analogChannel); client.print("analog
input "); client.print(analogChannel);
client.print(" is "); client.print(sensorReading);
client.println("<br />");
}

client.println("</html>");
break; }
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the
data delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
14. Arduino Ethernet Shield W5100 Web Server LED Control

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//physical mac address byte ip[] = { 192, 168, 1, 177 }; // ip in
lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString; const int ledPin=6; void
setup(){ pinMode(ledPin, OUTPUT); //pin
led digitalWrite(ledPin,LOW);//make sure
led off
//start Ethernet
Ethernet.begin(mac, ip, gateway,
subnet); server.begin(); //enable
serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so I can keep track
of what is loaded
Serial.println(Ethernet.localIP());
} void
loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) { while (client.connected()) { if
(client.available()) { char c =
client.read(); //read char by char HTTP
request if (readString.length() < 100) {
//store characters to string readString +=
c;
//Serial.print(c);
}
//if HTTP request has
ended if (c == '\n') {
Serial.println(readString); //print to serial
monitor for debuging client.println("HTTP/1.1
200 OK"); //send new page
client.println("Content-Type: text/html");
client.println(); client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Home
Automation</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Home Automation</H1>");
client.println("<hr />");
client.println("<br />");
client.println("<a href=\"/?lighton\"\">Turn On
Light</a>"); client.println("<a
href=\"/?lightoff\"\">Turn Off Light</a><br />");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
client.stop();
if(readString.indexOf("?lighton") >0)//checks
for on
{
digitalWrite(ledPin, HIGH); // set led
on Serial.println("Led On");
} else{
if(readString.indexOf("?lightoff")
>0)//checks for off
{
digitalWrite(ledPin, LOW); // set led off
Serial.println("Led Off");
}
}
//clearing string for next read
readString="";
}
}
}
}
}
15. Arduino Ethernet Shield W5100 Web Server Relay Control with status
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 111);
EthernetServer server(80);
// Relay state and pin
String relay1State =
"Off"; const int relay =
7; // Client variables
char linebuf[80]; int
charcount=0; void
setup() {
// Relay module prepared
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// Open serial communication at a baud rate of 9600
Serial.begin(9600);
// start the Ethernet connection and
the server: Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void dashboardPage(EthernetClient &client) {
client.println("<!DOCTYPE
HTML><html><head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-
scale=1\"></
head><body>"); client.println("<h3>Arduino Web Server - <a
href=\"/\">Refresh</a></h3>");
// Generates buttons to control the relay
client.println("<h4>Relay 1 - State: " + relay1State +
"</h4>"); // If relay is off, it shows the button to turn the
output on if(relay1State == "Off"){ client.println("<a
href=\"/relay1on\"><button>ON</button></a>");
}
// If relay is on, it shows the button to turn the output off
else if(relay1State == "On"){ client.println("<a
href=\"/relay1off\"><button>OFF</button></a>");
}
client.println("</body></html>");
}
void loop() {
// listen for incoming clients
EthernetClient client =
server.available(); if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a
blank line boolean
currentLineIsBlank = true; while
(client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP
request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1)
charcount++;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply if (c
== '\n' &&
currentLineIsBlank) {
dashboardPage(client); break;
}
if (c == '\n') { if (strstr(linebuf,"GET
/relay1off") > 0){
digitalWrite(relay, HIGH);
relay1State = "Off";
}
else if (strstr(linebuf,"GET
/relay1on") > 0){
digitalWrite(relay, LOW);
relay1State = "On";
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf)
); charcount=0;
}
else if (c != '\r') {
// you've gotten a character on the
current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the
data delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
16. Arduino Ethernet Shield W5100 Web Server Button Status Monitoring
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE,
0xED }; IPAddress ip(192, 168, 0, 102); // Ubah
dan sesuaikan IP Adress EthernetServer
server(80); //Buat server pada port 80 const int
btn=3;
void setup()
{
Ethernet.begin(mac, ip);
server.begin();
pinMode(btn, INPUT); //Set Pin 3 sebagai input
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
} void
loop()
{
//Mencoba mendapatkan client
EthernetClient client =
server.available(); if (client) { //
Dapat client?
boolean currentLineIsBlank =
true; while (client.connected()) {
// Data client tersedia untuk
dibaca if (client.available()) {
// Baca 1 byte (karakter) dari
client char c = client.read();
if (c == '\n' && currentLineIsBlank) {
//Kirim sebuah standard http response
header client.println("HTTP/1.1 200
OK"); client.println("Content-Type:
text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Baca Digital
Input</title>"); client.println("<meta http-
equiv=\"refresh\" content=\"1\">");
client.println("</head>");
client.println("<body>");
dapatkanStatusButton(client);
client.println("</body>");
client.println("</html>");
break;
}
// Setiap baris yang di terima, diakhiri
dengan "enter" if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
// Sebuah text karakter telah diterima
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop(); // Tutup koneksi
}
}
void dapatkanStatusButton(EthernetClient cl)
{
if (digitalRead(btn)) {
cl.println("<h1>Button lagi di pencet
!</h1>");
}
else { cl.println("<h1>Button tidak di
pencet </h1>");
}
}
17. Wemos D1 R2 WIFI AP - Web Server LED Control
#include <ESP8266WiFi.h>
WiFiServer server(80); //Initialize the
server on Port 80 const int ledPin=14; void
setup() {
WiFi.mode(WIFI_AP); //Our ESP8266-12E is an
AccessPoint
WiFi.softAP("Hello_IoT", "12345678"); // Provide the
(SSID, password); . server.begin(); // Start the HTTP
Server
Serial.begin(115200); //Start communication between the ESP8266-12E and the
monitor window
IPAddress HTTPS_ServerIP= WiFi.softAPIP(); // Obtain the IP of the Server
Serial.print("Server IP is: "); // Print the IP to the
monitor window Serial.println(HTTPS_ServerIP);
pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,LOW);
}
void loop() {
WiFiClient client =
server.available(); if (!client) {
return;
}
//Looking under the hood
Serial.println("Somebody has connected :)");
String request = client.readStringUntil('\r');
Serial.println(request); if
(request.indexOf("/OFF") != -
1){ digitalWrite(ledPin,
LOW);
}
else if (request.indexOf("/ON") != -1){
digitalWrite(ledPin, HIGH);
}

String s = "HTTP/1.1 200 OK\r\n" ;


s += "Content-Type:
text/html\r\n\r\n"; s +=
"<!DOCTYPE
HTML>\r\n<html>\r\n";
s += "<head><title>Wemos D1 R2 WIFI AP - Web Server LED
Control</title></head>”; s += "<body><h1>Wemos D1 R2 WIFI
AP - Web Server LED Control</h1>”; s += "<br><hr><br>";
s += "<br><input type=\"button\" name=\"b1\" value=\"LED ON\"
onclick=\"location.href='ON'\">"; s += "&nbsp;&nbsp;&nbsp;";
s += "<br><input type=\"button\" name=\"b1\" value=\"LED OFF\"
onclick=\"location.href='OFF'\">"; s += "</body></html>\n";
//Serve the HTML document to the
browser. client.flush(); //clear previous info
in the stream client.print(s); // Send the
response to the client delay(1);
Serial.println("Client
disconnected"); }
18. Wemos D1 R2 Client AP - Web Server LED Control
#include <ESP8266WiFi.h>

const char* ssid = "your-ssid";


const char* password = "your-password";

const int ledPin = 14;


WiFiServer server(80);

void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

// Connect to WiFi network


Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() !=
WL_CONNECTED) { delay(500);
Serial.print("."); }
Serial.println("");
Serial.println("WiFi connected");

// Start the server


server.begin();
Serial.println("Server started");

// Print the IP address


Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");

}
void loop() {
// Check if a client has
connected WiFiClient client =
server.available(); if (!client) {
return;
}

// Wait until the client sends some data


Serial.println("new client");
while(!client.available()){
delay(1);
}

// Read the first line of the request


String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

// Match the request

int value = LOW;


if (request.indexOf(“/LED=ON”)
!= -1) { digitalWrite(ledPin,
HIGH); value = HIGH;
}
if
(request.indexOf(“/LED=OFF”)
!= -1){ digitalWrite(ledPin,
LOW); value = LOW;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);

// Return the response


client.println("HTTP/1.1 200 OK");
client.println("Content-Type:
text/html"); client.println(""); // do
not forget this one
client.println("<!DOCTYPE
HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title>Wemos D1 R2 Client AP - Web Server
LED Control</title>"); client.println("</head>");
client.println("<body>");
client.println("<h1>Wemos D1 R2 Client AP - Web Server
LED Control<h1>"); client.println("<br><hr><br>");
client.print("Led pin is now : ");

if(value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("Click <a href=\"/LED=ON\">here</a> turn the
LED ON<br>"); client.println("Click <a
href=\"/LED=OFF\">here</a> turn the LED OFF<br>");
client.println("</body>");
client.println("</html>");

delay(1);
Serial.println("Client disonnected");
Serial.println("");

Você também pode gostar