Thursday, February 15, 2018

Interactive Arduino Uno Ethernet Shield Program

I have developed a Interactive Arduino UNO Ethernet Shield demo program.  It requires lots of memory.  It was a offshoot while I was developing a Ethernet based method of updating my LED signs. However the UNO suffers from a memory shortage just running this demo program!  This is what the screen looks like.  You cannot change D10-D13 because they are used by the Ethernet card.


Here is the video:

I have finally found an easy way to post code on blogger!  Thanks to "http://codeformatter.blogspot.com/" the code can be easily formatted.

 // Arduino Ethernet Interactive with table  
 // By bob Davis  
 // Based on code from Rui Santos  
  
 #include <Ethernet.h>  
 int led1 = 2;  
 int led2 = 3;  
 int led3 = 4;  
 int led4 = 5;  
 int led5 = 6;  
 int led6 = 7;  
 int din1 = 8;  
 int din2 = 9;  
 int din3 = 10;  
 int din4 = 11;  
 int din5 = 12;  
 int din6 = 13;  
 int data1 = 0;  
 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  //physical mac address  
 byte ip[] = { 192, 168, 1, 21 };           // ip in network  
 EthernetServer server(80);               //server port     
 String readString;  
 void setup() {  
  // Open serial communications and wait for port to open:  
  pinMode(led1, OUTPUT);  
  pinMode(led2, OUTPUT);  
  pinMode(led3, OUTPUT);  
  pinMode(led4, OUTPUT);   
  pinMode(led5, OUTPUT);  
  pinMode(led6, OUTPUT);  
  pinMode(din1, INPUT);  
  pinMode(din2, INPUT);  
  // start the Ethernet connection and the server:  
  Ethernet.begin(mac, ip);  
  server.begin();  
 }  
 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;  
      } //if HTTP request has ended  
      if (c == '\n') {       
       client.println("HTTP/1.1 200 OK"); //send new page  
       client.println("Content-Type: text/html");  
       client.println("Refresh: 2"); // refresh every second   
       client.println();     
       client.println("<HTML><BODY>");  
       client.println("<style>table, th, td {padding: 3px; border: 1px solid black;</style>");  
       client.println("<table width=350><tr>");  
       client.println("<th colspan=6>Digital Outputs</th>");  
       client.println("</tr><tr>");  
       client.println("<th>D2</th><th>D3</th>");  
       client.println("<th>D4</th><th>D5</th>");  
       client.println("<th>D6</th><th>D7</th>");  
       client.println("</tr><tr>");  
       client.println("<td><a href=\"/?button1on\"\">Turn On</a></td>");  
       client.println("<td><a href=\"/?button2on\"\">Turn On</a></td>");  
       client.println("<td><a href=\"/?button3on\"\">Turn On</a></td>");  
       client.println("<td><a href=\"/?button4on\"\">Turn On</a></td>");  
       client.println("<td><a href=\"/?button5on\"\">Turn On</a></td>");  
       client.println("<td><a href=\"/?button6on\"\">Turn On</a></td>");  
       client.println("</tr><tr>");  
       client.println("<td><a href=\"/?button1off\"\">Turn Off</a></td>");    
       client.println("<td><a href=\"/?button2off\"\">Turn Off</a></td>");   
       client.println("<td><a href=\"/?button3off\"\">Turn Off</a></td>");    
       client.println("<td><a href=\"/?button4off\"\">Turn Off</a></td>");   
       client.println("<td><a href=\"/?button5off\"\">Turn Off</a></td>");   
       client.println("<td><a href=\"/?button6off\"\">Turn Off</a></td>");   
       client.println("</tr><tr>");  
       client.println("<th colspan=6>Digital Inputs</th>");  
       client.println("</tr><tr>");  
       client.println("<th>D8</th><th>D9</th>");  
       client.println("<th>D10</th><th>D11</th>");  
       client.println("<th>D12</th><th>D13</th>");  
       client.println("</tr><tr>");   
       client.println("<td align='center'>");  
       data1=digitalRead(din1);  
       client.println(data1);  
       client.println("</td>");    
       client.println("<td align='center'>");  
       data1=digitalRead(din2);  
       client.println(data1);  
       client.println("</td>");    
       client.println("</tr><tr>");  
       client.println("<th colspan=6>Analog Inputs</th>");  
       client.println("</tr><tr>");  
       client.println("<th>A0</th><th>A1</th>");  
       client.println("<th>A2</th><th>A3</th>");  
       client.println("<th>A4</th><th>A5</th>");  
       client.println("</tr><tr>");  
       for (int aChannel = 0; aChannel < 6; aChannel++) {  
        int sensorReading = analogRead(aChannel);  
        client.println(" <td align='center'> ");  
        client.println(sensorReading);  
        client.println("</td>");  
      }  
      client.println("</table></BODY></HTML>");  
       delay(1);  
       //stopping client  
       client.stop();  
       //controls the Arduino if you press the buttons  
       if (readString.indexOf("?button1on") >0){ digitalWrite(led1, HIGH); }  
       if (readString.indexOf("?button1off") >0){ digitalWrite(led1, LOW); }  
       if (readString.indexOf("?button2on") >0){ digitalWrite(led2, HIGH); }  
       if (readString.indexOf("?button2off") >0){ digitalWrite(led2, LOW); }  
       if (readString.indexOf("?button3on") >0){ digitalWrite(led3, HIGH); }  
       if (readString.indexOf("?button3off") >0){ digitalWrite(led3, LOW); }  
       if (readString.indexOf("?button4on") >0){ digitalWrite(led4, HIGH); }  
       if (readString.indexOf("?button4off") >0){ digitalWrite(led4, LOW); }  
       if (readString.indexOf("?button5on") >0){ digitalWrite(led5, HIGH); }  
       if (readString.indexOf("?button5off") >0){ digitalWrite(led5, LOW); }  
       if (readString.indexOf("?button6on") >0){ digitalWrite(led6, HIGH); }  
       if (readString.indexOf("?button6off") >0){ digitalWrite(led6, LOW); }  
       //clearing string for next read  
       readString="";   
      }  
     }  
   }  
 }  
 }  

No comments: