Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #include <Dhcp.h>
  6. #include <Dns.h>
  7. #include <EthernetUdp.h>
  8. #include <Wire.h>
  9. #define ONE_WIRE_BUS 2
  10.  
  11.  
  12.  
  13.  
  14.  
  15. float teplota = 0;
  16. float ph=0;
  17. float zakalenie=0;
  18. int hlbka=0;
  19.  
  20.  
  21.  
  22. OneWire oneWire(ONE_WIRE_BUS);
  23.  
  24. // Pass our oneWire reference to Dallas Temperature sensor
  25. DallasTemperature sensors(&oneWire);
  26.  
  27.  
  28. #define Hostname "Arduino" //Meno v sieti
  29. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC adresa
  30. char server[] = "www.logaro.studenthosting.sk"; //adresa webservera
  31. IPAddress ip(10, 0, 0, 8); //IP adresa v lokalnej sieti
  32. EthernetClient client; //Spustenie ethernetu ako klienta
  33.  
  34.  
  35. int sensorPin = A1;
  36. float volt;
  37. float ntu;
  38. float val;
  39. const int analogInPin = A0;
  40. int sensorValue = 0;
  41. unsigned long int avgValue;
  42. float b;
  43. int buf[10],temp;
  44. float round_to_dp( float in_value, int decimal_place )
  45.  
  46.  
  47.  
  48. {
  49. float multiplier = powf( 10.0f, decimal_place );
  50. in_value = roundf( in_value * multiplier ) / multiplier;
  51. return in_value;
  52. }
  53.  
  54. const int trigPin = 3;
  55. const int echoPin = 4;
  56. // defines variables
  57. float duration;
  58. int distance;
  59.  
  60.  
  61. void setup() {
  62. // komunikácia cez sériovú linku rýchlosťou 112500 baud
  63. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  64. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  65. Serial.begin(9600);
  66. // Start up the library
  67. sensors.begin();
  68.  
  69. }
  70. void loop() {
  71.  
  72.  
  73.  
  74. // Clears the trigPin
  75. digitalWrite(trigPin, LOW);
  76. delayMicroseconds(2);
  77. // Sets the trigPin on HIGH state for 10 micro seconds
  78. digitalWrite(trigPin, HIGH);
  79. delayMicroseconds(10);
  80. digitalWrite(trigPin, LOW);
  81. // Reads the echoPin, returns the sound wave travel time in microseconds
  82. duration = pulseIn(echoPin, HIGH);
  83. // Calculating the distance
  84. distance= duration*0.034/2;
  85.  
  86.  
  87. sensors.requestTemperatures();
  88.  
  89. volt = 0;
  90. for(int i=0; i<800; i++)
  91. {
  92. volt += ((float)analogRead(sensorPin)/1023)*5;
  93. }
  94.  
  95. volt = volt/800;
  96. volt = round_to_dp(volt,1);
  97. if(volt < 2.5){
  98. ntu = 3000;
  99. }else{
  100. ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
  101.  
  102. }
  103. val = map(ntu, 1798,3000 , 0, 100);
  104. sensors.requestTemperatures();
  105.  
  106.  
  107. for(int i=0;i<10;i++)
  108. {
  109. buf[i]=analogRead(analogInPin);
  110. delay(10);
  111. }
  112. for(int i=0;i<9;i++)
  113. {
  114. for(int j=i+1;j<10;j++)
  115. {
  116. if(buf[i]>buf[j])
  117. {
  118. temp=buf[i];
  119. buf[i]=buf[j];
  120. buf[j]=temp;
  121. }
  122. }
  123. }
  124. avgValue=0;
  125. for(int i=2;i<8;i++)
  126. avgValue+=buf[i];
  127. float pHVol=(float)avgValue*5.0/1024/6;
  128. float phValue = -5.70 * pHVol + 21.34;
  129. // Serial.print("sensor = ");
  130. //Serial.println(phValue);
  131.  
  132.  
  133. hlbka=distance;
  134. ph=phValue;
  135. zakalenie=val;
  136. teplota=sensors.getTempCByIndex(0);
  137.  
  138. if (Ethernet.begin(mac) == 0) { //V prípade zlyhania vypíš
  139. Serial.println("Chyba konfiguracie cez DHCP"); //Vypis na seriovu linku
  140. Ethernet.begin(mac, ip); //Nastavenie IP a MAC adresy pre ethernet modul
  141. }
  142.  
  143. odosli_data();
  144. delay(1000);
  145.  
  146. Serial.println(teplota);
  147. Serial.println(ph);
  148. Serial.println(zakalenie);
  149. Serial.println(hlbka);
  150.  
  151.  
  152.  
  153. }// Koniec loop slučky
  154.  
  155.  
  156. void odosli_data() {
  157. if (client.connect(server, 80)) { // Ak pripojí na server, port 80
  158. Serial.println("Pripojenie uspesne na webserver"); //vypise na seriovu linku
  159. client.print("GET /water/arduino/zapisdata.php?teplota="); //Začiatok HTTP REQUEST --> client.print GET Metodou s oznacenim premennej, do ktorej sa zapíšu dáta
  160.  
  161.  
  162. client.print(teplota);
  163. client.print("&zakalenie=");
  164. client.print(zakalenie);
  165. client.print("&hlbka=");
  166. client.print(hlbka);
  167. client.print("&ph=");
  168. client.print(ph);
  169. client.println(" HTTP/1.1");
  170. client.println("Host: www.logaro.studenthosting.sk"); // Adresa hosta na ktorý, bol mierený REQUEST
  171. client.println("Connection: close");
  172. client.println(); //Zalomenie riadka
  173. client.stop(); // Ukoncenie pripojenia ethernet shieldu
  174. } else {
  175. Serial.println("Pripojenie zlyhalo...");
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement