mikroavr

ethernet_deteksi_220vac_v2

Mar 1st, 2021
540
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "check_id.h"
  2. #include "DHT.h"
  3.  
  4. #define DHTTYPE DHT21  
  5. #define DHTPIN 33
  6.  
  7. DHT dht(DHTPIN, DHTTYPE);
  8.  
  9. #include <SPI.h>
  10. #include <Ethernet.h>
  11.  
  12. #define _pin_cs 5
  13. #define _pin_rst 15
  14. #define _pin_sensor 34
  15. #define _pin_limit 22
  16.  
  17. #include <HTTPClient.h>
  18.  
  19. byte _mac[] = {0xDE, 0x19, 0xCC, 0xEF, 0xFE, 0xED};
  20. byte _ip[] = {192, 168, 1, 20};
  21. byte _dns[] = {8, 8, 8, 8};
  22.  
  23. EthernetClient client;
  24.  
  25. const char server[] = "mainapi.sbumedan.co.id";
  26. //const char server[] = "google.com";
  27. const char resource[] = "/api/sensi";
  28. const int port = 80;
  29.  
  30. bool dt_sensor = 0;
  31. bool dt_limit = 0;
  32. float hum, temp;
  33.  
  34. unsigned long cur_time, old_time;
  35. unsigned long cur_time_sensor, old_time_sensor;
  36.  
  37. unsigned long update_time = 1500;
  38.  
  39. bool hold_dt_sensor = false;
  40. bool hold_dt_limit = false;
  41. String id;
  42.  
  43. void setup() {
  44.   // put your setup code here, to run once:
  45.   Serial.begin(115200);
  46.   delay(1000);
  47.  
  48.   id = check_id();
  49.  
  50.   pinMode(_pin_rst, OUTPUT);
  51.   pinMode(_pin_sensor, INPUT);
  52.   pinMode(_pin_limit, INPUT);
  53.  
  54.   digitalWrite(_pin_rst, HIGH);
  55.   delay(2000);
  56.   dht.begin();
  57.  
  58.   Ethernet.init(_pin_cs);
  59.   Ethernet.begin(_mac, _ip, _dns);
  60.  
  61. }
  62.  
  63. void loop() {
  64.   // put your main code here, to run repeatedly:
  65.  
  66.   cur_time_sensor = millis();
  67.   if (cur_time_sensor - old_time_sensor >= 1000) {
  68.     baca_sensor();
  69.     if(dt_sensor == true){
  70.       if(hold_dt_sensor == false){
  71.         //push_server();
  72.         hold_dt_sensor = true;
  73.       }
  74.     }else{
  75.       hold_dt_sensor = false;
  76.     }
  77. //------------------------------------
  78.     if(dt_limit == true){
  79.       if(hold_dt_limit == false){
  80.         push_server();
  81.         hold_dt_limit = true;
  82.       }
  83.     }else{
  84.       hold_dt_limit = false;
  85.     }
  86. //------------------------------------
  87.     old_time_sensor = cur_time_sensor;
  88.   }
  89.  
  90.   cur_time = millis();
  91.   if (cur_time - old_time >= update_time) {
  92.     push_server();
  93.     old_time = cur_time;
  94.     update_time = 5*60*1000; //update setiap 5 menit;
  95.   }
  96.   delay(2000);
  97. }
  98.  
  99. void baca_sensor() {
  100.   dt_sensor = digitalRead(_pin_sensor);
  101.   dt_limit = digitalRead(_pin_limit);
  102.   hum = dht.readHumidity();
  103.   temp = dht.readTemperature();
  104.  
  105.   Serial.print("suhu: ");
  106.   Serial.println(temp);
  107.  
  108. //  /Serial.print("data Sensor: ");
  109. //  /Serial.println(dt_sensor);
  110. //  /Serial.print("data limit: ");
  111. //  /Serial.println(dt_sensor);
  112.  
  113. }
  114.  
  115. void push_server() {
  116.   if (init_eth(_mac, _ip, _dns, _pin_cs )) {
  117.     Serial.println("push to server..");
  118.     if (client.connect(server, port)) {
  119.       Serial.println("connected to server");
  120.      
  121.       String httpRequestData = "pln=" + String(dt_sensor) + "&suhu =" + String(temp,2)+
  122.                                "&limit=" +String(dt_limit)+"&lokasi=" + id;
  123.       Serial.print("dt_http_post: ");
  124.       Serial.println(httpRequestData);
  125.  
  126.       client.print(String("POST ") + resource + " HTTP/1.1\r\n");
  127.       client.print(String("Host: ") + server + "\r\n");
  128.       client.println("Connection: close");
  129.       client.println("Content-Type: application/x-www-form-urlencoded");
  130.       client.print("Content-Length: ");
  131.       client.println(httpRequestData.length());
  132.       client.println();
  133.       client.println(httpRequestData);
  134.       while (client.connected()) {
  135.           if (client.available()) {
  136.             Serial.write(client.read());
  137.           }
  138.         }
  139.     } else {
  140.       Serial.println("disconnected to server");
  141.     }
  142.   }
  143. }
  144.  
  145. bool init_eth(uint8_t mac[5], uint8_t ip[3], uint8_t dns[3], uint8_t pin_cs) {
  146.   bool sts_eth;
  147.   Ethernet.init(pin_cs);
  148.  
  149.   while (Ethernet.hardwareStatus() == EthernetNoHardware) {
  150.     Serial.println(F("Ethernet shield was not found."));
  151.     delay(1000);
  152.     ESP.restart();
  153.   }
  154.   if (Ethernet.hardwareStatus() == EthernetW5500) {
  155.     Serial.println(F("W5500 Ethernet controller detected."));
  156.   }
  157.  
  158.   if (Ethernet.linkStatus() == LinkON) {
  159.     Serial.println(F("Link status: On"));
  160.     sts_eth = true;
  161.   }
  162.  
  163.   if (Ethernet.linkStatus() == LinkOFF) {
  164.     Serial.println(F("Link status: Off"));
  165.     sts_eth = false;
  166.   }
  167.  
  168.   Serial.print("  DHCP assigned IP ");
  169.   Serial.println(Ethernet.localIP());
  170.   return sts_eth;
  171. }
RAW Paste Data