Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <Ethernet.h>
- #include <Arduino_WebServer.h>
- #include <SD.h>
- // Enter a MAC address and IP address for your controller below.
- // The IP address will be dependent on your local network:
- byte mac[] = { 0x90,0xA2,0xDA,0x00,0xD9,0x7E };
- int ipAddr[4] = { 161, 0, 0, 7 };
- IPAddress ip(ipAddr[0],ipAddr[1],ipAddr[2],ipAddr[3]);
- int port=8080;
- EthernetServer server(port);
- EthernetClient client;
- SdFile root;
- SdVolume volume;
- boolean fileTerm = false;
- /** Main setup function */
- 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() + ":" + port);
- Serial.println("Initializing SD Card...");
- pinMode(10,OUTPUT);
- if(!SD.begin(4)) {
- Serial.println("Initialization fail!");
- return;
- }
- Serial.println("Initialization done.");
- }
- /** Main loop function */
- void loop() {
- // listen for incoming clients
- 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); // write ethernet rcv to serial
- // 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
- PrintHTML();
- 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");
- }
- char character;
- String content="";
- while (Serial.available()) {
- character = Serial.read();
- content.concat(character);
- delay(1);
- }
- if (content != "") {
- Serial.println(content);
- /** Error from Serial command string.
- * 0 = No error
- * 1 = Invalid command
- */
- int err = testInput(content);
- }
- }
- int testInput(String content) {
- if (content == "term") {
- fileTerm = true;
- Serial.println("Starting Terminal Mode");
- return 0;
- }
- if (content == "exit" && fileTerm == true) {
- fileTerm = false;
- Serial.println("Exitting Terminal Mode");
- return 0;
- }
- return 1;
- }
- /** Print an html line to the ethernet client */
- void html(String inpLine) {
- // Write input to the web client connection.
- client.println(inpLine);
- }
- void PrintHTML() {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close");
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- // add a meta refresh tag, so the browser pulls again every 5 seconds:
- client.println("<head><meta http-equiv=\"refresh\" content=\"10\">");
- client.println("<title>Arduino Uno R3</title>");
- client.println("</head>\n<body>");
- client.println("<h1>Welcome to the Arduino Uno R3 Webserver!</h1>");
- // output the value of each analog input pin
- client.println("<h3>Analog inputs</h3>");
- client.println("<h4>10-bit Full-Scale Range (0 to 1023)</h4>");
- for (int analogChannel = 0; analogChannel <= 5; analogChannel++) {
- int sensorReading = analogRead(analogChannel);
- client.println("Analog Input ");
- client.println((String)analogChannel);
- client.println(" is ");
- client.println((String)sensorReading);
- client.println("<br />");
- }
- client.println("</body></html>");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement