Friday, January 9, 2015

Web Server example run on Arduino Uno with Ethernet Shield

Web Server example run on Arduino Uno with Ethernet Shield.




WebServer.ino
/*
 *  Reference: http://arduino.cc/en/Tutorial/WebServer
 */
#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 Leonardo 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) {
        
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.print("<h1>hello Arduino Ethernet Shield</h1>");
          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");
  }
}

Reference:



Friday, January 2, 2015

Using a RFID Card Reader with the Arduino


This tutorial was developed for the MAKE course at the University of South Florida (www.makecourse.com). This tutorial discusses the use of a MF522-AN RFID reader and MIFARE Classic 1k tags/cards with the Arduino. The basics of RFID communication are reviewed as well as the MIFARE card hardware. A library for the MF522 that can be found at github (https://github.com/miguelbalboa/rfid) is being used for a simple Arduino sketch that allows the writing and reading of blocks on a MIFARE card.


Related:
- Mifare MFRC522 RFID Reader/Writer

Thursday, January 1, 2015

Happy New Year 2015 - LCD Scrolling example


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.print(" Happy New Year ");
  lcd.setCursor(0, 1);
  lcd.print("      2015      ");
  delay(3000);
}

void loop() {
  // scroll 13 positions (string length) to the left 
  // to move it offscreen left:
  for (int positionCounter = 0; positionCounter < 15; positionCounter++) {
    // scroll one position left:
    lcd.scrollDisplayLeft(); 
    // wait a bit:
    delay(1000);
  }

  // scroll 29 positions (string length + display length) to the right
  // to move it offscreen right:
  for (int positionCounter = 0; positionCounter < 30; positionCounter++) {
    // scroll one position right:
    lcd.scrollDisplayRight(); 
    // wait a bit:
    delay(1000);
  }
  
  for (int positionCounter = 0; positionCounter < 15; positionCounter++) {
    // scroll one position left:
    lcd.scrollDisplayLeft(); 
    // wait a bit:
    delay(1000);
  }
  
  // delay at the end of the full loop:
  delay(3000);

}


Example: LiquidCrystal - scrollDisplayLeft() and scrollDisplayRight()