mikroavr

ethernet_deteksi_220vac

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