Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. /*
  2.   Web client
  3.  
  4.  This sketch connects to a website (http://www.google.com)
  5.  using an Arduino Wiznet Ethernet shield.
  6.  
  7.  Circuit:
  8.  * Ethernet shield attached to pins 10, 11, 12, 13
  9.  
  10.  created 18 Dec 2009
  11.  by David A. Mellis
  12.  modified 9 Apr 2012
  13.  by Tom Igoe, based on work by Adrian McEwen
  14.  
  15.  */
  16.  
  17. #include <SPI.h>
  18. #include <Ethernet.h>
  19.  
  20. // Enter a MAC address for your controller below.
  21. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  22. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEE, 0xFE, 0xED };
  23. // if you don't want to use DNS (and reduce your sketch size)
  24. // use the numeric IP instead of the name for the server:
  25. //IPAddress server(83.143.132.2);  // numeric IP for Google (no DNS)
  26. char server[] = "http://www.ewikuslugi.twojeuti.pl";    // name address for Google (using DNS)
  27.  
  28. // Set the static IP address to use if the DHCP fails to assign
  29. IPAddress ip(192,168,1,110);
  30.  
  31. // Initialize the Ethernet client library
  32. // with the IP address and port of the server
  33. // that you want to connect to (port 80 is default for HTTP):
  34. EthernetClient client;
  35.  
  36. void setup() {
  37.  // Open serial communications and wait for port to open:
  38.   Serial.begin(9600);
  39.    while (!Serial) {
  40.     ; // wait for serial port to connect. Needed for Leonardo only
  41.   }
  42.  
  43.   // start the Ethernet connection:
  44.   if (Ethernet.begin(mac) == 0) {
  45.     Serial.println("Failed to configure Ethernet using DHCP");
  46.     // no point in carrying on, so do nothing forevermore:
  47.     // try to congifure using IP address instead of DHCP:
  48.     Ethernet.begin(mac, ip);
  49.   }
  50.   // give the Ethernet shield a second to initialize:
  51.   delay(1000);
  52.   Serial.println("connecting...");
  53.  
  54.   // if you get a connection, report back via serial:
  55.  
  56. }
  57.  
  58. void loop()
  59. {
  60.   //if (client.connect(server, 80)) {
  61.     //Serial.println("connected");
  62.     // Make a HTTP request:
  63.   //  String data = "wiad=witam serdecznie" ;
  64.  //   if (client.connect("www.ewikuslugi.twojeuti.pl",80)) { // REPLACE WITH YOUR SERVER ADDRESS
  65. //      client.println("POST /mailuj.php HTTP/1.1");
  66. //      client.println("Host: www.ewikuslugi.twojeuti.pl"); // SERVER ADDRESS HERE TOO
  67. //      client.println("Content-Type: application/x-www-form-urlencoded");
  68. //      client.print("Content-Length: ");
  69. //      client.println(data.length());
  70. //      client.println();
  71. //      client.print(data);
  72.  //               Serial.println(data.length()+data);
  73. //  }
  74.  
  75. //  if (client.connected()) {
  76. //      client.stop();  // DISCONNECT FROM THE SERVER
  77. //  }
  78.  // }
  79.  // else {
  80.     // kf you didn't get a connection to the server:
  81.   //  Serial.println("connection failed");
  82.  // }
  83.  
  84.  
  85.  
  86.   postData();
  87.   //Get();
  88.  
  89.  
  90.   if (client.available()) {
  91.     char c = client.read();
  92.     Serial.print(c);
  93.   }
  94.  
  95.   // if the server's disconnected, stop the client:
  96.   if (!client.connected()) {
  97.     Serial.println();
  98.     Serial.println("disconnecting.");
  99.     client.stop();
  100.  
  101.     // do nothing forevermore:
  102.     while(true);
  103.   }
  104. }
  105.  
  106.  
  107. void postData() {
  108. // Combine yourdatacolumn header (yourdata=) with the data recorded from your arduino
  109. // (yourarduinodata) and package them into the String yourdata which is what will be
  110. // sent in your POST request
  111. String nag = "wiad=";
  112. String wart ="costam";
  113. String yourdata = nag + wart;
  114. // If there's a successful connection, send the HTTP POST request
  115. if (client.connect(server, 80)) {
  116. Serial.println("connecting...");
  117. // EDIT: The POST 'URL' to the location of your insert_mysql.php on your web-host
  118. client.println("POST /mailuj.php HTTP/1.1");
  119. // EDIT: 'Host' to match your domain
  120. client.println("Host: http://www.ewikuslugi.twojeuti.pl");
  121. client.println("User-Agent: Arduino/1.0");
  122. client.println("Connection: close");
  123. client.println("Content-Type: application/x-www-form-urlencoded;");
  124. client.print("Content-Length: ");
  125. client.println(yourdata.length());
  126. client.println();
  127. client.println(yourdata);
  128. Serial.println(yourdata.length());
  129. Serial.println();
  130. Serial.println(yourdata);
  131.  
  132. }
  133. else {
  134. // If you couldn't make a connection:
  135. Serial.println("Connection failed");
  136. Serial.println("Disconnecting.");
  137. client.stop();
  138. }
  139. }
  140.  
  141. void Get(){
  142.  if (client.connect(server, 80)) {
  143.     Serial.println("connected");
  144.     // Make a HTTP request:
  145.     client.println("GET /index.php HTTP/1.1");
  146.     client.println("Host: http://www.ewikuslugi.twojeuti.pl");
  147.     client.println("Connection: close");
  148.     client.println();
  149.   }
  150.   else {
  151.     // kf you didn't get a connection to the server:
  152.     Serial.println("connection failed");
  153.   }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement