Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Demo using DHCP and DNS to perform a web client request.
- // 2011-06-08 <jc@wippler.nl>
- //
- // License: GPLv2
- #include <EtherCard.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Data wire is plugged into port 2 on the Arduino
- #define ONE_WIRE_BUS 2
- // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
- OneWire oneWire(ONE_WIRE_BUS);
- // Pass our oneWire reference to Dallas Temperature.
- DallasTemperature sensors(&oneWire);
- // assign a MAC address for the ethernet controller.
- // fill in your address here:
- const byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- byte Ethernet::buffer[700];
- const char server[] PROGMEM = "deda.tmep.cz"; // domain.tmep.cz
- const char guid[] = "12345678"; // guid
- unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
- const unsigned long postingInterval = 60L * 1000L; // delay between updates, in milliseconds
- // the "L" is needed to use long type numbers
- // called when the client request is complete
- static void my_callback (byte status, word off, word len) {
- Serial.println(">>>");
- Ethernet::buffer[off+300] = 0;
- Serial.print((const char*) Ethernet::buffer + off);
- Serial.println("...");
- }
- void setup () {
- Serial.begin(9600);
- Serial.println(F("\n[webClient]"));
- // Change 'SS' to your Slave Select pin, if you arn't using the default pin
- if (ether.begin(sizeof Ethernet::buffer, mac, SS) == 0)
- Serial.println(F("Failed to access Ethernet controller"));
- if (!ether.dhcpSetup())
- Serial.println(F("DHCP failed"));
- ether.printIp("IP: ", ether.myip);
- ether.printIp("GW: ", ether.gwip);
- ether.printIp("DNS: ", ether.dnsip);
- #if 1
- // use DNS to resolve the website's IP address
- if (!ether.dnsLookup(server))
- Serial.println("DNS failed");
- #elif 2
- // if website is a string containing an IP address instead of a domain name,
- // then use it directly. Note: the string can not be in PROGMEM.
- char websiteIP[] = "192.168.1.1";
- ether.parseIp(ether.hisip, websiteIP);
- #else
- // or provide a numeric IP address instead of a string
- byte hisip[] = { 192,168,1,1 };
- ether.copyIp(ether.hisip, hisip);
- #endif
- ether.printIp("SRV: ", ether.hisip);
- sensors.begin();
- }
- void loop () {
- ether.packetLoop(ether.packetReceive());
- if (millis() - lastConnectionTime > postingInterval) {
- lastConnectionTime = millis();
- send_values();
- }
- }
- void send_values(){
- // request to all devices on the bus
- Serial.print("Requesting temperatures...");
- sensors.requestTemperatures(); // Send the command to get temperatures
- Serial.println("DONE");
- // After we got the temperatures, we can print them here.
- // We use the function ByIndex, and as an example get the temperature from the first sensor only.
- Serial.print("Temperature for the device 1 (index 0) is: ");
- float t = sensors.getTempCByIndex(0); // Read temperature in "t" variable
- if (t == -127.00) { // If you have connected it wrong, Dallas read this temperature! :)
- Serial.println("Error! sensor is not connected (probably)");
- return;
- }
- Serial.println(t);
- /*
- client.print("GET /?");
- client.print(guid);
- client.print("=");
- client.print(t);
- client.println(" HTTP/1.1");
- client.print("Host: ");
- client.println(server);
- client.println("User-Agent: arduino-ethernet");
- client.println("Connection: close");
- client.println();
- */
- char str_temp[6]; /* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
- dtostrf(t, 4, 2, str_temp);
- char value[50]; // string to be sent to the server, you can access it i.e. $_GET in PHP ...
- // memset(value, 0, sizeof(value));
- sprintf(value,"%s=%s", guid, str_temp); // prepare the GET, all variables in one string
- Serial.println(value);
- Serial.println();
- Serial.print("<<< REQ ");
- ether.browseUrl(PSTR("/?"), value, server, my_callback);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement