Advertisement
Guest User

Arduino web server connection

a guest
Dec 2nd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. //zoomkat 9-22-12
  2. //simple client test
  3. //for use with IDE 1.0.1
  4. //with DNS, DHCP, and Host
  5. //open serial monitor and send an e to test
  6. //for use with W5100 based ethernet shields
  7. //remove SD card if inserted
  8.  
  9. #include <SPI.h>
  10. #include <Ethernet.h>
  11.  
  12. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  13.  
  14. char serverName[] = "web.comporium.net"; // zoomkat's test web page server
  15. EthernetClient client;
  16.  
  17. //////////////////////
  18.  
  19. void setup(){
  20.  
  21. if (Ethernet.begin(mac) == 0) {
  22. Serial.println("Failed to configure Ethernet using DHCP");
  23. // no point in carrying on, so do nothing forevermore:
  24. while(true);
  25. }
  26.  
  27. Serial.begin(9600);
  28. Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  29. Serial.println("Send an e in serial monitor to test"); // what to do to test
  30. }
  31.  
  32. void loop(){
  33. // check for serial input
  34. if (Serial.available() > 0) //if something in serial buffer
  35. {
  36. byte inChar; // sets inChar as a byte
  37. inChar = Serial.read(); //gets byte from buffer
  38. if(inChar == 'e') // checks to see byte is an e
  39. {
  40. sendGET(); // call sendGET function below when byte is an e
  41. }
  42. }
  43. }
  44.  
  45. //////////////////////////
  46.  
  47. void sendGET() //client function to send/receive GET request data.
  48. {
  49. if (client.connect(serverName, 80)) { //starts client connection, checks for connection
  50. Serial.println("connected");
  51. client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
  52. client.println("Host: web.comporium.net");
  53. client.println(); //end of get request
  54. }
  55. else {
  56. Serial.println("connection failed"); //error message if no client connect
  57. Serial.println();
  58. }
  59.  
  60. while(client.connected() && !client.available()) delay(1); //waits for data
  61. while (client.connected() || client.available()) { //connected or data available
  62. char c = client.read(); //gets byte from ethernet buffer
  63. Serial.print(c); //prints byte to serial monitor
  64. }
  65.  
  66. Serial.println();
  67. Serial.println("disconnecting.");
  68. Serial.println("==================");
  69. Serial.println();
  70. client.stop(); //stop client
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement