110 lines
2.7 KiB
C++
110 lines
2.7 KiB
C++
/*
|
|
Repeating Web client
|
|
|
|
Circuit:
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
|
* Sensor mit 4,7K Ohm auf Data <->5V auf pin 9
|
|
*/
|
|
|
|
#include <SPI.h>
|
|
#include <Ethernet.h>
|
|
#include <EthernetUdp.h>
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
#define host "10.188.17.6"
|
|
#define port 1234
|
|
|
|
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
|
|
|
// initialize the library instance:
|
|
EthernetClient client;
|
|
OneWire oneWire(9);
|
|
EthernetUDP Udp;
|
|
|
|
// Pass our oneWire reference to Dallas Temperature.
|
|
DallasTemperature sensors(&oneWire);
|
|
char server[] = host;
|
|
|
|
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
|
const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds
|
|
// the "L" is needed to use long type numbers
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
while (!Serial);
|
|
|
|
|
|
// give the ethernet module time to boot up:
|
|
delay(1000);
|
|
// start the Ethernet connection using a fixed IP address and DNS server:
|
|
Ethernet.begin(mac);
|
|
// print the Ethernet board/shield's IP address:
|
|
Serial.print("My IP address: ");
|
|
Serial.println(Ethernet.localIP());
|
|
|
|
sensors.begin();
|
|
Udp.begin(1234);
|
|
}
|
|
|
|
void loop() {
|
|
// if there's incoming data from the net connection.
|
|
// send it out the serial port. This is for debugging
|
|
// purposes only:
|
|
if (client.available()) {
|
|
char c = client.read();
|
|
Serial.write(c);
|
|
}
|
|
|
|
// if ten seconds have passed since your last connection,
|
|
// then connect again and send data:
|
|
if (millis() - lastConnectionTime > postingInterval) {
|
|
httpRequest();
|
|
delay(10);
|
|
} else {
|
|
delay(20);
|
|
}
|
|
}
|
|
|
|
void httpRequest() {
|
|
Serial.println("Sending");
|
|
//client.stop();
|
|
sensors.requestTemperatures();
|
|
|
|
Udp.beginPacket(server, port);
|
|
int temp = sensors.getTempCByIndex(0)*100;
|
|
Serial.println(temp);
|
|
char out[] = {
|
|
(char) ((temp & 0XFF000000) >> 24) ,
|
|
(char) ((temp & 0X00FF0000) >> 16),
|
|
(char) ((temp & 0X0000FF00) >> 8),
|
|
(char) (temp & 0X000000FF)
|
|
};
|
|
Udp.write(out , 4);
|
|
Udp.endPacket();
|
|
Serial.println(out[0], HEX);
|
|
Serial.println(out[1], HEX);
|
|
Serial.println(out[2], HEX);
|
|
Serial.println(out[3], HEX);
|
|
|
|
lastConnectionTime = millis();
|
|
/*if (client.connect(server, 80)) {
|
|
Serial.println("connecting...");
|
|
// send the HTTP GET request:
|
|
client.print("POST /temp/");
|
|
client.print(sensors.getTempCByIndex(0));
|
|
client.println("HTTP/1.1");
|
|
client.print("Host: ");
|
|
client.print(host);
|
|
client.println("User-Agent: BOT");
|
|
client.println("Connection: close");
|
|
|
|
client.println();
|
|
client.println();
|
|
|
|
lastConnectionTime = millis();
|
|
} else {
|
|
Serial.println("connection failed");
|
|
}*/
|
|
}
|