레이블이 arduino mega인 게시물을 표시합니다. 모든 게시물 표시
레이블이 arduino mega인 게시물을 표시합니다. 모든 게시물 표시

2014년 11월 18일 화요일

Note9 – WebServer controlled LED Sketch

ArduinoNote List-page
PREVIOUS: Note8 - WebServer controlled LED Demo

Web Server controlled LED Sketch



[code lang=cpp]
/*
Web Server

A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 15 Nov 2014
by Soohwan Kim
*/

#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:
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
;
#else
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
#endif

//#define __USE_DHCP__

IPAddress ip(192,168,1,20);
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );
// fill in your Domain Name Server address here:
IPAddress myDns(8, 8, 8, 8); // google puble dns

// 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 check_led_status();
// Define the LED PORT NUMBER
#define LED_PORT 53

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

// initialize the LED PORT
pinMode(LED_PORT, OUTPUT);
// initaial value is HIGH
digitalWrite(LED_PORT, HIGH);

// initialize the ethernet device
#if defined __USE_DHCP__
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin();
#else
Ethernet.begin(mac);
#endif
#else
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin(ip, myDns, gateway, subnet);
#else
Ethernet.begin(mac, ip, myDns, gateway, subnet);
#endif
#endif

// start the Ethernet connection and the server:
server.begin();
Serial.println("WebServerControlLED");
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;
String buffer = ""; // Declare the buffer variable
while (client.connected()) {
if (client.available()) {
char c = client.read();
buffer += c; // Assign to the buffer
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();
//client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");

// check the LED status
if (digitalRead(LED_PORT)>0){
client.println("LED is <font color='green'>ON</font>");
}else{
client.println("LED is <font color='red'>OFF</font>");
}

// generate the Form
client.println("<br />");
client.println("<FORM method="get" action="/led.cgi">");
client.println("<P> <INPUT type="radio" name="status" value="1">ON");
client.println("<P> <INPUT type="radio" name="status" value="0">OFF");
client.println("<P> <INPUT type="submit" value="Submit"> </FORM>");

client.println("</body>");
client.println("</html>");
break;
}
if (c == 'n') {
// you're starting a new line
currentLineIsBlank = true;
buffer="";
}
else if ( c == 'r') {
//do cgi parser for LED-On
if(buffer.indexOf("GET /led.cgi?status=1")>=0){
// cgi action : LED-On
digitalWrite(LED_PORT, HIGH);
// send web-page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<body>");
// check the LED status
if (digitalRead(LED_PORT)>0){
client.println("LED is <font color='green'>ON</font>");
}else{
client.println("LED is <font color='red'>OFF</font>");
}
client.println("<br />");
client.println("<a href="/led.htm">Go to control-page</a>");

client.println("</body>");
client.println("</html>");
currentLineIsBlank = false;
break;
}

//do cgi parser for LED-Off
if(buffer.indexOf("GET /led.cgi?status=0")>=0){
// action : LED-Off
digitalWrite(LED_PORT ,LOW);
// send web-page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<body>");
// check the LED status
if (digitalRead(LED_PORT)>0){
client.println("LED is <font color='green'>ON</font>");
}else{
client.println("LED is <font color='red'>OFF</font>");
}
client.println("<br />");
client.println("<a href="/led.htm">Go to control-page</a>");

client.println("</body>");
client.println("</html>");
currentLineIsBlank = false;
break;
}
}
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 disonnected");
}
}
[/code]

Note7 - WebServer controlled LED

ArduinoNote List-page
PREVIOUS: Note6 - WebServer

Hand-on #4: WebServer controlled LED



-Ethernet Shield를 이용한 Remote Control(LED On/Off)과 Monitoring(LED Status)을 함께 해보자

Common Gateway Interface




WWW 서버와 서버 상에서 등장하는 다른 프로그램이나 스크립트와의 인터페이스. 폼을 사용한 메일의 송신이나 게임 등, HTML에서는 불가능한 인터랙티브(interactive)한 요소를 홈페이지에 받아들여 쓸 수 있다.
[네이버 지식백과] CGI [common gateway interface] (컴퓨터인터넷IT용어대사전, 2011.1.20, 일진사)



  • Get Request / Receive Req. / CGI parser
    Get Request:cgi


  • Run the CGI App: run CGI scripts
    Run the CGI App.


  • Response: call web_server_send from CGI app. and back to Response
    Web Response




Hardware



Arduino / WIZ550io / LED
* LED를 적절한 포트에 연결한다. 이때, LED에 과전류를 방지하기 위해 저항을 달아준다.

Install Ethenret Library





Network Configuration



Network Configuration


  • Network Configuration on PC side
    Network Configuration
    위와 같이 "Internet Protocol Properies"에서 Network구성을 Test를 위해 아래와 같이 설정해보자
    IP address : 192.168.1.2
    Subnet mask : 255.255.255.0



Default gateway : 192.168.1.1



WebServerControlLED Skeleton




  • Fixed IP address



[code lang=cpp]
//#define __USE_DHCP__

IPAddress ip(192,168,1,20);
IPAddress gateway( 192, 168, 1, 1 );
IPAddress subnet( 255, 255, 255, 0 );
// fill in your Domain Name Server address here:
IPAddress myDns(8, 8, 8, 8); // google puble dns
}
[/code]


  • Port Setting



[code lang=cpp]
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
[/code]


  • setup()



[code lang=cpp]
// Define the LED PORT NUMBER
// ...insert codes... #define LED_PORT XX

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


// initialize the LED PORT
// ...insert codes ...
// initaial value is HIGH
// ...insert codes ...


// initialize the ethernet device
#if defined __USE_DHCP__
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin();
#else
Ethernet.begin(mac);
#endif
#else
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io
Ethernet.begin(ip, myDns, gateway, subnet);
#else
Ethernet.begin(mac, ip, myDns, gateway, subnet);
#endif
#endif

// start the Ethernet connection and the server:
server.begin();
// ...insert codes ... Welcome messages
Serial.print("server is at ");
// ...insert codes ... print localIP
}

[/code]


  • loop()



[code lang=cpp]
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;
String buffer = ""; // Declare the buffer variable
while (client.connected()) {
if (client.available()) {
char c = client.read();
buffer += c; // Assign to the buffer
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();
//client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");

// check the LED status
// ...insert codes ...

// generate the Form
// ...insert codes ...

client.println("</body>")
client.println("</html>");
break;
}
if (c == 'n') {
// you're starting a new line
currentLineIsBlank = true;
buffer="";
}
else if ( c == 'r') {
//do cgi parser for LED-On
//if(...insert codes ...
// action : LED-On
//digitalWrite(...insert codes ...
// send web-page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<body>");
// check the LED status
// ...insert codes ...
client.println("<br />");
client.println("<a href="/led.htm">Go to control-page</a>");

client.println("</body>");
client.println("</html>");
currentLineIsBlank = false;
break;
}

//do cgi parser for LED-Off
//if(...insert codes ...
// action : LED-Off
//digitalWrite(...insert codes ...
// send web-page
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html>");
client.println("<body>");
// check the LED status
// ...insert codes ...
client.println("<br />");
client.println("<a href="/led.htm">Go to control-page</a>");

client.println("</body>");
client.println("</html>");
currentLineIsBlank = false;
break;
}
}
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 disonnected");
}
}
[/code]

NEXT : Note8 - WebServer controlled LED Demo

Note2 - Blink sketch

ArduinoNote List-page
PREVIOUS: Note1 - ARDUINO

Hand-on #1 : Blink



-Arduino를 경험해 본다.
-Sketch IDE를 설치, 간단한 Blink Example 따라하기

Preparation Materials




  • Arudino platform : Arduino Mega
    Arduino Mega
    (from http://Arduino.cc)



Arduino Mega

(* form http://duino4projects.com/multiserial-mega-using-arduino/)


























































Spec. Description
Microcontroller ATmega2560
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limits) 6-20V
Digital I/O Pins 54 (of which 15 provide PWM output)
Analog Input Pins 16
DC Current per I/O Pin 40 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 256 KB of which 8 KB used by bootloader
SRAM 8 KB
EEPROM 4 KB
Clock Speed 16 MHz




Blink Sketch :




  • Load Blink Sketch : IDE의 파일>예제>Basics>Blink

    Blink example


  • Blink codes

    [code lang=cpp]
    // the setup function runs once when you press reset or power the board
    void setup() {
    // initialize digital pin 13 as an output.
    pinMode(13, OUTPUT);
    }
    // the loop function runs over and over again forever
    void loop() {
    digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
    delay(1000); // wait for a second
    digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
    delay(1000); // wait for a second
    }
    [/code]




Pin 13 회로도에서 살펴보자



L LED on Schemetic

Pin 13으로 L LED를 On/Off 한다.!



L LED on Board

Configuration Platform




  • IDE 메뉴의 도구>보드>Arduino Mega of Arduino Mega 2560
    Selett Serial Port



Compile




  • IDE 메뉴의 스케치>확인/컴파일
    Verify



Program




  • IDE 메뉴의 파일>프로그램어를통해업로드
    Upload



NEXT: Note3 - Hello world

2014년 10월 9일 목요일

Pinoccio - A Complete Ecosystem for Building the Internet of Things | Indiegogo

Pinoccio - A Complete Ecosystem for Building the Internet of Things | Indiegogo.

 

Pinoccio


  • Atmel ATmega128RFA1 microcontroller at 16MHz and 128k of flash

  • 550mAh LiPo rechargeable battery, rechargeable via the USB jack

  • 2.4GHz radio using the 802.15.4 wireless standard

  • 17 digital I/O pins, including four with PWM

  • 8 analog input pins

  • 2 hardware UART serial ports

  • Hardware SPI bus

  • Dedicated I2C bus

  • On-board temperature sensor

  • On-board RGB LED