Projekt 3stellige 7Segmentanzeige per WiFi ansteuern

Das nachfolgend vorgestellte Programm zeigt wie Werte zu einem Arduino mit einem "Netzwerkshield" (WiFly,Ethershield) übetragen werden können. Die Idee dabei ist, das auf dem Arduino ein Webserver läuft und die Werte Ereignisorientiert übertragen werden. Das erspart zum einen die Installation weiterer Software auf dem übetragenden Rechner zum anderen kann genau gesteuert werden, wann Werte übetragen werden. Zum Daten senden wird das Tool wget verwendet oder ein browser, die zu übetragenden Werte werden als Parameter an die URL gehangen, und im Arduino ausgewertet. Als Ausgabe dienen drei Sieben-Segmentanzeigen, die per Schieberegister angesteuert werden. Clock und Data werden zusmmangefasst, das jeweilige Latch wird pro Segment aktiviert.

  1.  
  2. /*
  3.  * Web Server for receiving values for 7dgit-LED shiftout through three registers
  4.  * Sparkfun WiFly shield and library necessary
  5.  * used call  http://ip-address/&123 with browser or wget
  6.  * character 7,8,9 on URL Call is used
  7.  */
  8.  
  9. #include "WiFly.h"
  10. #include "Credentials.h"
  11.  
  12. const int CLOCK_PIN = 2; //common clock for shiftout
  13. const int DATA_PIN = 3; //common data for shiftout
  14. const int LATCH_PIN_1 = 4; //Latch for Digit 1 for shiftout
  15. const int LATCH_PIN_2 = 5; //Latch for Digit 2 for shiftout
  16. const int LATCH_PIN_3 = 6; //Latch for Digit 3 for shiftout
  17.  
  18. Server server(80);
  19. String readString = String(100);
  20. String shstring1; // shortened string for digit1
  21. String shstring2; // shortened string for digit2
  22. String shstring3; // shortened string for digit3
  23.  
  24. void setup() {
  25.   WiFly.begin();
  26.   if (!WiFly.join(ssid, passphrase)) {
  27.     while (1) {
  28.       // Hang on failure.
  29.     }
  30.   }
  31.  
  32.   Serial.begin(9600);
  33.   Serial.print("IP: ");
  34.   Serial.println(WiFly.ip());
  35.   pinMode(LATCH_PIN_1, OUTPUT);
  36.   pinMode(LATCH_PIN_2, OUTPUT);
  37.   pinMode(LATCH_PIN_3, OUTPUT);
  38.   pinMode(DATA_PIN, OUTPUT);
  39.   pinMode(CLOCK_PIN, OUTPUT);        
  40.  
  41.   server.begin();
  42. }
  43.  
  44. void loop() {
  45.   Client client = server.available();
  46.   if (client) {
  47.     // an http request ends with a blank line
  48.     boolean current_line_is_blank = true;
  49.     while (client.connected()) {
  50.       if (client.available()) {
  51.         char c = client.read();
  52.  
  53.  //read char by char HTTP request
  54. if (readString.length() < 100) {
  55.  
  56. //store characters to string
  57. readString = readString + c;
  58. }      
  59.         if (c == '\n' && current_line_is_blank) {
  60. shstring1 = readString.substring(6,7);
  61. shstring2 = readString.substring(7,8);
  62. shstring3 = readString.substring(8,9);
  63.     Serial.println("this was the recognized number" );  
  64.     Serial.println("digit 1=");
  65.     Serial.println(shstring1);
  66.     Serial.println("digit 2=");
  67.     Serial.println(shstring2);
  68.     Serial.println("digit 3=");
  69.     Serial.println(shstring3);    
  70.  
  71.           // send a standard http response header
  72.           client.println("HTTP/1.1 200 OK");
  73.           client.println("Content-Type: text/html");
  74.           client.println();
  75.  
  76.           client.print("received_via_url=");
  77.           client.print("digit 1=");
  78.           client.print(shstring1);
  79.           client.print("digit 2=");
  80.           client.print(shstring2);
  81.           client.print("digit 3=");
  82.           client.print(shstring3);
  83.  
  84. //controlling of shiftout for three digits
  85. if (shstring1.equals("0")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00111111);digitalWrite(LATCH_PIN_1, HIGH);}
  86. if (shstring1.equals("1")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000110);digitalWrite(LATCH_PIN_1, HIGH);}
  87. if (shstring1.equals("2")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01011011);digitalWrite(LATCH_PIN_1, HIGH);}
  88. if (shstring1.equals("3")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01001111);digitalWrite(LATCH_PIN_1, HIGH);}
  89. if (shstring1.equals("4")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01100110);digitalWrite(LATCH_PIN_1, HIGH);}
  90. if (shstring1.equals("5")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101101);digitalWrite(LATCH_PIN_1, HIGH);}
  91. if (shstring1.equals("6")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111101);digitalWrite(LATCH_PIN_1, HIGH);}
  92. if (shstring1.equals("7")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000111);digitalWrite(LATCH_PIN_1, HIGH);}
  93. if (shstring1.equals("8")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111111);digitalWrite(LATCH_PIN_1, HIGH);}
  94. if (shstring1.equals("9")){digitalWrite(LATCH_PIN_1, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101111);digitalWrite(LATCH_PIN_1, HIGH);}
  95.  
  96. if (shstring2.equals("0")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00111111);digitalWrite(LATCH_PIN_2, HIGH);}
  97. if (shstring2.equals("1")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000110);digitalWrite(LATCH_PIN_2, HIGH);}
  98. if (shstring2.equals("2")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01011011);digitalWrite(LATCH_PIN_2, HIGH);}
  99. if (shstring2.equals("3")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01001111);digitalWrite(LATCH_PIN_2, HIGH);}
  100. if (shstring2.equals("4")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01100110);digitalWrite(LATCH_PIN_2, HIGH);}
  101. if (shstring2.equals("5")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101101);digitalWrite(LATCH_PIN_2, HIGH);}
  102. if (shstring2.equals("6")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111101);digitalWrite(LATCH_PIN_2, HIGH);}
  103. if (shstring2.equals("7")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000111);digitalWrite(LATCH_PIN_2, HIGH);}
  104. if (shstring2.equals("8")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111111);digitalWrite(LATCH_PIN_2, HIGH);}
  105. if (shstring2.equals("9")){digitalWrite(LATCH_PIN_2, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101111);digitalWrite(LATCH_PIN_2, HIGH);}
  106.  
  107. if (shstring3.equals("0")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00111111);digitalWrite(LATCH_PIN_3, HIGH);}
  108. if (shstring3.equals("1")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000110);digitalWrite(LATCH_PIN_3, HIGH);}
  109. if (shstring3.equals("2")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01011011);digitalWrite(LATCH_PIN_3, HIGH);}
  110. if (shstring3.equals("3")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01001111);digitalWrite(LATCH_PIN_3, HIGH);}
  111. if (shstring3.equals("4")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01100110);digitalWrite(LATCH_PIN_3, HIGH);}
  112. if (shstring3.equals("5")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101101);digitalWrite(LATCH_PIN_3, HIGH);}
  113. if (shstring3.equals("6")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111101);digitalWrite(LATCH_PIN_3, HIGH);}
  114. if (shstring3.equals("7")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B00000111);digitalWrite(LATCH_PIN_3, HIGH);}
  115. if (shstring3.equals("8")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01111111);digitalWrite(LATCH_PIN_3, HIGH);}
  116. if (shstring3.equals("9")){digitalWrite(LATCH_PIN_3, LOW);shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, B01101111);digitalWrite(LATCH_PIN_3, HIGH);}
  117.  
  118.           break;
  119.         }
  120.         if (c == '\n') {
  121.           // we're starting a new line
  122.           current_line_is_blank = true;
  123.         } else if (c != '\r') {
  124.           // we've gotten a character on the current line
  125.           current_line_is_blank = false;
  126.         }
  127.       }
  128.     }
  129.     // give the web browser time to receive the data
  130. readString="";
  131. delay(100);
  132.     client.stop();
  133.   }
  134. }

Share