bangnaga

Arduino Ethernet Shield Baca Sensor

May 27th, 2012
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <dht11.h>
  4. dht11 DHT11;
  5. #define DHT11PIN 2 // Digital 2
  6.  
  7. // LED pin
  8. int ledPin0 = 3; // Digital 3
  9. int ledPin1 = 4; // Digital 4
  10. int ledPin2 = 5; // Digital 5
  11. int ledPin3 = 6; // Digital 6
  12. int ledPin4 = 7; // Digital 7
  13.  
  14. // Suhu pin
  15. int SHPin0 = 0; // Analog 0
  16.  
  17. // IR pin
  18. int IRpin = 4; // Analog 4
  19.  
  20. // Speaker pin
  21. int SPKpin = 8; // Digital 8
  22.  
  23. // Satuan
  24. int status_kirim_data = 0;
  25. float distance;
  26. int ledPin[5] = {3,4,5,6,7};
  27. int i;
  28.  
  29. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  30. byte ip[] = { 192, 168, 20, 254 }; // ip jaringan
  31. byte gateway[] = { 192, 168, 0, 3 }; // internet access melalui router
  32. byte subnet[] = { 255, 255, 0, 0 }; //subnet mask
  33. EthernetServer server(80); //server port
  34. String url_metadata = String(100); //mengambil url metadata
  35.  
  36. void setup()
  37. {
  38. //Mengaktifkan Ethernet
  39. Ethernet.begin(mac, ip, gateway, subnet);
  40. server.begin();
  41. pinMode(ledPin0, OUTPUT);
  42. pinMode(ledPin1, OUTPUT);
  43. pinMode(ledPin2, OUTPUT);
  44. pinMode(ledPin3, OUTPUT);
  45. pinMode(ledPin4, OUTPUT);
  46. pinMode(SPKpin, OUTPUT);
  47.  
  48. //Membuka serial data print
  49. Serial.begin(9600);
  50. Kedip_On();
  51. delay(500);
  52. Kedip_Off();
  53. delay(500);
  54. //Delay Ethernet module untuk boot up:
  55. Serial.print("Booting Ethernet.. Silahkan Tunggu..");
  56. Serial.println();
  57. Kedip_On();
  58. delay(500);
  59. Kedip_Off();
  60. delay(500);
  61. Kedip_On();
  62. delay(500);
  63. Kedip_Off();
  64. delay(500);
  65. run_led();
  66. }
  67.  
  68. void loop()
  69. {
  70. //DHT11 Sensor
  71. int chk = DHT11.read(DHT11PIN);
  72. switch (chk)
  73. {
  74. case 0: //Serial.println("OK");
  75. Serial.print("H0, "); //KELEMBABAN ALAT
  76. Serial.print((float)DHT11.humidity, 1);
  77. Serial.println();
  78. Serial.print("T1, "); //SUHU ALAT
  79. Serial.print((float)DHT11.temperature, 1);
  80. Serial.println();
  81. break;
  82. }
  83.  
  84. //SENSOR SUHU 0
  85. float SUHU0=analogRead(SHPin0);
  86. float SH0=((5.00 * SUHU0 * 100)/1024);
  87. Serial.print("T0, "); //SUHU ETHERNET
  88. Serial.print(SH0,1);
  89. Serial.println();
  90.  
  91. //IR Sensor
  92. float volt = analogRead(IRpin);
  93. float volts = ((volt*5)/1024); // 5 volts = ((volt * 5)/1024), 3.3.volts = ((volt * 3.3)/1024)
  94. float distance = (65*pow(volts, -1.10))/10; // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
  95. Serial.print("D0, "); //JARAK SENSOR
  96. Serial.print(distance,1);
  97. Serial.println();
  98.  
  99. //STATUS DATA
  100. Serial.print("SD");
  101. Serial.print(status_kirim_data,1);
  102. Serial.println();
  103.  
  104. if (distance < 5)
  105. {
  106. if (status_kirim_data == 0)
  107. {
  108. status_kirim_data = 1;
  109. }
  110. else
  111. {
  112. Kedip_On();
  113. digitalWrite(SPKpin, HIGH);
  114. }
  115. }
  116. else
  117. {
  118. status_kirim_data = 0;
  119. }
  120.  
  121. // Membuat Koneksi Client
  122. EthernetClient client = server.available();
  123. if (client) {
  124. Serial.println("Menerima Request Data Dari Client");
  125. while (client.connected()) {
  126. if (client.available()) {
  127. char c = client.read();
  128. //membaca char per char dalam HTTP request
  129. if (url_metadata.length() < 100)
  130. {
  131. //menampilkan characters kedalam string
  132. url_metadata = url_metadata + c;
  133. }
  134. //Serial.println(url_metadata);
  135. if (url_metadata.indexOf("=") >= 0) {
  136. int Input_Pertama = (url_metadata.indexOf("=") + 1);
  137. int Pin_Pertama = int(url_metadata.charAt(Input_Pertama)) - 48;
  138. int Input_Kedua = (url_metadata.indexOf("=", Input_Pertama) + 1);
  139. int Pin_Kedua = int(url_metadata.charAt(Input_Kedua)) - 48;
  140. Serial.println(Pin_Pertama);
  141. Serial.println(Pin_Kedua);
  142. //if (Pin_Pertama == 2)
  143. //{
  144. //}
  145. Kedip_On();
  146. delay(100);
  147. Kedip_Off();
  148. }
  149. client.print(" D0"); //JARAK SENSOR
  150. client.println(distance,1);
  151. digitalWrite(ledPin0, HIGH);
  152. client.print(" T0"); //SUHU ETHERNET
  153. client.println(SH0,1);
  154. digitalWrite(ledPin1, HIGH);
  155.  
  156. switch (chk)
  157. {
  158. case 0: //Serial.println("OK");
  159.  
  160. client.print(" T1"); //SUHU ALAT ALAT
  161. client.println((float)DHT11.temperature, 1);
  162. digitalWrite(ledPin2, HIGH);
  163. client.print(" H0"); //KELEMBABAN ALAT
  164. client.println((float)DHT11.humidity, 1);
  165. digitalWrite(ledPin3, HIGH);
  166. break;
  167. }
  168. client.print(" SD"); //KELEMBABAN ALAT
  169. client.println((float)status_kirim_data, 1);
  170. client.println(" FR100"); //ID ALAT
  171. digitalWrite(ledPin4, HIGH);
  172.  
  173. //menutup client
  174. if (!client.connected()) {
  175. Serial.println("Memutuskan Koneksi Dengan Client");
  176. }
  177. client.stop();
  178. //}
  179. }
  180. }
  181. }
  182. else
  183. {
  184. Serial.println("Tidak Ada Request Data Dari Client");
  185.  
  186. if (distance > 5)
  187. {
  188. Kedip_Off();
  189. }
  190. }
  191. }
  192.  
  193. void Kedip_On()
  194. {
  195. digitalWrite(ledPin0, HIGH);
  196. digitalWrite(ledPin1, HIGH);
  197. digitalWrite(ledPin2, HIGH);
  198. digitalWrite(ledPin3, HIGH);
  199. digitalWrite(ledPin4, HIGH);
  200. }
  201.  
  202. void Kedip_Off()
  203. {
  204. digitalWrite(ledPin0, LOW);
  205. digitalWrite(ledPin1, LOW);
  206. digitalWrite(ledPin2, LOW);
  207. digitalWrite(ledPin3, LOW);
  208. digitalWrite(ledPin4, LOW);
  209. }
  210.  
  211. void run_led()
  212. {
  213. digitalWrite(ledPin0, HIGH);
  214. delay(200);
  215. //
  216. digitalWrite(ledPin0, LOW);
  217. digitalWrite(ledPin1, HIGH);
  218. delay(200);
  219. //
  220. digitalWrite(ledPin1, LOW);
  221. digitalWrite(ledPin2, HIGH);
  222. delay(200);
  223. //
  224. digitalWrite(ledPin2, LOW);
  225. digitalWrite(ledPin3, HIGH);
  226. delay(200);
  227. digitalWrite(ledPin3, LOW);
  228. digitalWrite(ledPin4, HIGH);
  229. delay(200);
  230. digitalWrite(ledPin4, LOW);
  231. delay(200);
  232. }
RAW Paste Data