daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Demo using DHCP and DNS to perform a web client request.
  2. // 2011-06-08 <jc@wippler.nl>
  3. //
  4. // License: GPLv2
  5.  
  6. #include <EtherCard.h>
  7. #include <OneWire.h>
  8. #include <DallasTemperature.h>
  9.  
  10.  
  11. // Data wire is plugged into port 2 on the Arduino
  12. #define ONE_WIRE_BUS 2
  13.  
  14. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  15. OneWire oneWire(ONE_WIRE_BUS);
  16.  
  17. // Pass our oneWire reference to Dallas Temperature.
  18. DallasTemperature sensors(&oneWire);
  19.  
  20. // assign a MAC address for the ethernet controller.
  21. // fill in your address here:
  22. const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  23.  
  24.  
  25. byte Ethernet::buffer[700];
  26.  
  27.  
  28. const char server[] PROGMEM = "deda.tmep.cz";   // domain.tmep.cz
  29. const char guid[] = "12345678";        // guid
  30.  
  31. unsigned long lastConnectionTime = 0;              // last time you connected to the server, in milliseconds
  32. const unsigned long postingInterval = 60L * 1000L; // delay between updates, in milliseconds
  33. // the "L" is needed to use long type numbers
  34.  
  35.  
  36. // called when the client request is complete
  37. static void my_callback (byte status, word off, word len) {
  38.   Serial.println(">>>");
  39.   Ethernet::buffer[off+300] = 0;
  40.   Serial.print((const char*) Ethernet::buffer + off);
  41.   Serial.println("...");
  42. }
  43.  
  44. void setup () {
  45.   Serial.begin(9600);
  46.   Serial.println(F("\n[webClient]"));
  47.  
  48.   // Change 'SS' to your Slave Select pin, if you arn't using the default pin
  49.   if (ether.begin(sizeof Ethernet::buffer, mac, SS) == 0)
  50.     Serial.println(F("Failed to access Ethernet controller"));
  51.   if (!ether.dhcpSetup())
  52.     Serial.println(F("DHCP failed"));
  53.  
  54.   ether.printIp("IP:  ", ether.myip);
  55.   ether.printIp("GW:  ", ether.gwip);
  56.   ether.printIp("DNS: ", ether.dnsip);
  57.  
  58. #if 1
  59.   // use DNS to resolve the website's IP address
  60.   if (!ether.dnsLookup(server))
  61.     Serial.println("DNS failed");
  62. #elif 2
  63.   // if website is a string containing an IP address instead of a domain name,
  64.   // then use it directly. Note: the string can not be in PROGMEM.
  65.   char websiteIP[] = "192.168.1.1";
  66.   ether.parseIp(ether.hisip, websiteIP);
  67. #else
  68.   // or provide a numeric IP address instead of a string
  69.   byte hisip[] = { 192,168,1,1 };
  70.   ether.copyIp(ether.hisip, hisip);
  71. #endif
  72.  
  73.   ether.printIp("SRV: ", ether.hisip);
  74.  
  75.   sensors.begin();
  76. }
  77.  
  78.  
  79. void loop () {
  80.   ether.packetLoop(ether.packetReceive());
  81.  
  82.   if (millis() - lastConnectionTime > postingInterval) {
  83.     lastConnectionTime = millis();
  84.    
  85.     send_values();
  86.   }
  87. }
  88.  
  89.  
  90.  
  91. void send_values(){
  92.   // request to all devices on the bus
  93.   Serial.print("Requesting temperatures...");
  94.   sensors.requestTemperatures(); // Send the command to get temperatures
  95.   Serial.println("DONE");
  96.   // After we got the temperatures, we can print them here.
  97.   // We use the function ByIndex, and as an example get the temperature from the first sensor only.
  98.   Serial.print("Temperature for the device 1 (index 0) is: ");
  99.   float t = sensors.getTempCByIndex(0); // Read temperature in "t" variable
  100.   if (t == -127.00) {                   // If you have connected it wrong, Dallas read this temperature! :)
  101.     Serial.println("Error! sensor is not connected (probably)");
  102.     return;
  103.   }
  104.   Serial.println(t);
  105. /*
  106.       client.print("GET /?");
  107.       client.print(guid);
  108.       client.print("=");
  109.       client.print(t);
  110.       client.println(" HTTP/1.1");
  111.       client.print("Host: ");
  112.       client.println(server);
  113.       client.println("User-Agent: arduino-ethernet");
  114.       client.println("Connection: close");
  115.       client.println();
  116. */
  117.   char str_temp[6];   /* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
  118.   dtostrf(t, 4, 2, str_temp);
  119.  
  120.   char value[50]; // string to be sent to the server, you can access it i.e. $_GET in PHP ...
  121.   // memset(value, 0, sizeof(value));
  122.   sprintf(value,"%s=%s", guid, str_temp);   // prepare the GET, all variables in one string
  123.   Serial.println(value);
  124.  
  125.   Serial.println();
  126.   Serial.print("<<< REQ ");
  127.   ether.browseUrl(PSTR("/?"), value, server, my_callback);
  128. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top