#include <OneWire.h>
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x10, 0x19, 0xEF // Enter your ethernet MAC address. You will find it behind your arduino board.
};
IPAddress ip(IPADRESİBURDA); // Set your IP address for Arduino Board
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
OneWire ds(2); // on pin 10 (a 4.7K resistor is necessary)
void setup(void) {
// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
if ( !ds.search(addr)) {
Serial.println();
ds.reset_search();
delay(5000);
return;
}
for( i = 0; i < 8; i++) {
Serial.write(' ');
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println();
// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44); // start conversion, use ds.write(0x44,1) with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(" ");
}
Serial.println();
// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Sıcaklık Değeri = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
int req_index = 0;
int REQ_BUF_SZ = 1024;
char HTTP_req[REQ_BUF_SZ];
char ajax_inputs[15] = "ajax_inputs";
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
if (StrContains(HTTP_req, ajax_inputs)) {
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
client.print("Server Oda Sıcaklığı = ");
client.print(celsius);
client.print("\r\n");
}
else{
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html lang='en'>");
client.println("<head>");
client.println("<meta charset=\"UTF-8\"></meta>");
client.println("<title>Sıcaklık Kontrol Sistemi</title>");
client.println("<script>");
client.println("function SendText(){");
client.println("nocache = '&nocache=' + Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() { ");
client.println("if (request.readyState == XMLHttpRequest.DONE) {");
client.println("var paragraph = document.getElementById('response');");
client.println("paragraph.innerHTML = 'Server Oda Sıcaklığı = ' + celsius;}}");
client.println("request.open('GET', 'ajax_inputs' + nocache, true);");
client.println("request.send(null);}");
client.println("</script>");
client.println("</head>");
client.println("<body onload='setInterval(function(){SendText()},4000)'>");
client.print("<p style='text-align: center;'> </p>");
client.print("<p style='text-align: center;'><span style='font-size: x-large;'><strong>BAŞLIK</strong></span></p>");
client.print("<p id='response' style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'>Server Oda Sıcaklığı = ");
client.println(celsius);
client.print("</strong></span><h style='text-align: center;'><span style='color: #0000ff;'><strong style='font-size: large;'><sup>o</sup>C</strong></span></h></p>");
client.print("<p style='text-align: center;'> </p>");
client.print("<p style='text-align: center;'> </p>");
client.print("<p style='text-align: center;'> ");
client.println("</body>");
// Date and Time script
client.println("</html>");
}
req_index = 0;
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(5);
// close the connection:
client.stop();
// Serial.println("client disconnected");
}
}
#define htons(x) ( ((( x )&0xFF)<<8) | ((( x )&0xFF00)>>8) ) Arduniodan anlayan yardımcı olabilir mi?
0
●76
- 26-05-2022, 08:46:59Aşağıdaki kod ile ardunio ide izleme ekranından sıcaklık verisi geliyor. Fakat IP'e bağlanmak istediğimde bağlanmıyor ve tarayıcıdan ip girmek isteyince ardunio ide'deki veri akışıda duruyor nedendir?