fork download
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <Adafruit_BMP085_U.h>
  4. #include <WiFi.h>
  5.  
  6. const int outPin = 7; //Trigger
  7. const int inPin = 8; //Echo
  8. const int ledPin = 13; //Indikator Pakan
  9. const int relay = 12;
  10.  
  11. char ssid[] = "ISI DENGAN SSID WIFI";
  12. char pass[] = "ISI PASSWORD WIFI";
  13. int status = WL_IDLE_STATUS;
  14.  
  15. WiFiClient client;
  16. char server[] = "agnosthings.com";
  17.  
  18. Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);
  19.  
  20. void setup() {
  21. Serial.begin(9600);
  22. Wire.begin();
  23.  
  24. pinMode(outPin, OUTPUT_FAST);
  25. pinMode(outPin, OUTPUT_FAST);
  26. pinMode(ledPin, OUTPUT);
  27. pinMode(relay, OUTPUT);
  28.  
  29. if(!bmp.begin()){
  30. /* There was a problem detecting the BMP085 ... check your connections */
  31. Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
  32. while(1);
  33. }
  34.  
  35. while ( status != WL_CONNECTED) {
  36. Serial.print("Attempting to connect to WPA SSID: ");
  37. Serial.println(ssid);
  38. // Connect to WPA/WPA2 network:
  39. status = WiFi.begin(ssid, pass);
  40. }
  41. delay(10000);
  42. }
  43.  
  44. void loop(){
  45.  
  46. //Bagian Sensor Jarak
  47. long duration, inches, cm;
  48.  
  49. fastDigitalWrite(outPin, LOW);
  50. waitMicros(2);
  51. fastDigitalWrite(outPin, HIGH);
  52. waitMicros(10);
  53. fastDigitalWrite(outPin, LOW);
  54. duration = pulseIn(inPin, HIGH);
  55.  
  56. // convert the time into a distance
  57. //inches = microsecondsToInches(duration);
  58. cm = microsecondsToCentimeters(duration);
  59.  
  60. if (cm>10){
  61. digitalWrite(ledPin, HIGH);
  62. }else{
  63. digitalWrite(ledPin, LOW);
  64. }
  65. /* Serial.println(cm);
  66.   Serial.println();*/
  67.  
  68. /* Get a new sensor event */
  69. sensors_event_t event;
  70. bmp.getEvent(&event);
  71.  
  72. /* First we get the current temperature from the BMP085 */
  73. float temperature;
  74. bmp.getTemperature(&temperature);
  75.  
  76. String pakan = "";
  77. pakan+=String(int(cm))+ "."+String(getDecimal(cm));
  78.  
  79. String stringVal = "";
  80. stringVal+=String(int(temperature))+ "."+String(getDecimal(temperature));
  81.  
  82. String url_api = "ganti dengan url api dari agnosthings.com ?push=cel={tmpval},cm={value}";
  83. url_api.replace("{tmpval}", stringVal);
  84. url_api.replace("{value}", pakan);
  85. boolean requestStatus = httpRequest(url_api);
  86. if(requestStatus == true)
  87. Serial.println("Sending data success! Suhu " +stringVal +" Pakan " + pakan);
  88. else
  89. Serial.println("Sending data failed!");
  90. Serial.println();
  91.  
  92. // close any connection before send a new request.
  93. // This will free the socket on the Ethernet shield
  94. client.stop();
  95. if (int(temperature)<28){
  96. digitalWrite(relay, HIGH);
  97. }else{
  98. digitalWrite(relay, LOW);
  99. }
  100. delay(10000);
  101.  
  102. }
  103.  
  104. boolean httpRequest(String link) {
  105. // if you get a connection, report back via serial:
  106. Serial.println("Push data to server");
  107. if (client.connect(server, 80)) {
  108. // Make a HTTP request:
  109. client.println("GET " + link + " HTTP/1.0");
  110. client.println("Host: agnosthings.com");
  111. client.println();
  112. return true;
  113. } else {
  114. // You couldn't make the connection
  115. Serial.println("Connection Failed");
  116. client.stop();
  117. return false;
  118. }
  119. }
  120.  
  121. //function to extract decimal part of float
  122. long getDecimal(float val)
  123. {
  124. int intPart = int(val);
  125. long decPart = 1000*(val-intPart);
  126.  
  127. if(decPart>0)return(decPart);
  128. else if(decPart<0)return((-1)*decPart);
  129. else if(decPart=0)return(00);
  130. }
  131.  
  132. void waitMicros(int val){
  133. unsigned long a = micros();
  134. unsigned long b = micros();
  135. while((b-a) < val)
  136. {
  137. b = micros();
  138. if(a>b)
  139. {
  140. break;
  141. }
  142. }
  143. }
  144.  
  145. long microsecondsToInches(long microseconds){
  146. return microseconds / 74 / 2;
  147. }
  148.  
  149. long microsecondsToCentimeters(long microseconds){
  150. return microseconds / 29 / 2;
  151. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:18: fatal error: Wire.h: No such file or directory
compilation terminated.
stdout
Standard output is empty