Advertisement
mudhita_triari

Mas Ghiffari

Apr 23rd, 2024
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <Wire.h>
  3. #include <HTTPClient.h>
  4. #include <ModbusMaster.h>
  5. #include <SoftwareSerial.h>
  6.  
  7. #define MAX485_RE_NEG  2
  8. #define MAX485_DE      4
  9. #define SSERIAL_RX_PIN 16
  10. #define SSERIAL_TX_PIN 17
  11.  
  12. #define NUM_SENSORS     2
  13. #define TEMP_SENSOR     0
  14. #define HUM_SENSOR      1
  15.  
  16. #define BUZZER_PIN      26
  17.  
  18. #define UPPER_THRESHOLD 28.0
  19. #define LOWER_THRESHOLD 20.0
  20. #define HYSTERESIS      0.3
  21.  
  22. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  23. ModbusMaster node;
  24.  
  25. float sensorData[NUM_SENSORS];
  26. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  27.  
  28. const char* ssid = "Khansa";
  29. const char* password = "ghiffari";
  30. const char* serverName = "https://bootcampindobot.com/index-ghiffari-esp32.php";
  31. String apiKeyValue = "Td45OOuqjpGxNplQ6PK5JHv55GUi6Pkyv4lAzNSvKqEf08HvITj4XXfZl8fAwBg4";
  32.  
  33. const char* sensorName = "SHT20";
  34. const char* sensorLocation = "Kamar";
  35.  
  36. enum BuzzerStatus { OFF, ON };
  37. enum TempStatus { Normal, Panas, Dingin };
  38.  
  39. BuzzerStatus buzzerStatus = OFF;
  40. TempStatus tempStatus = Normal;
  41.  
  42. void preTransmission() {
  43.   digitalWrite(MAX485_RE_NEG, 1);
  44.   digitalWrite(MAX485_DE, 1);
  45. }
  46.  
  47. void postTransmission() {
  48.   digitalWrite(MAX485_RE_NEG, 0);
  49.   digitalWrite(MAX485_DE, 0);
  50. }
  51.  
  52. String buzzerStatusToString(BuzzerStatus status) {
  53.   switch (status) {
  54.     case OFF: return "OFF";
  55.     case ON: return "ON";
  56.   }
  57. }
  58.  
  59. String tempStatusToString(TempStatus status) {
  60.   switch (status) {
  61.     case Normal: return "Normal";
  62.     case Panas: return "Panas";
  63.     case Dingin: return "Dingin";
  64.   }
  65. }
  66.  
  67. void setup() {
  68.   pinMode(MAX485_RE_NEG, OUTPUT);
  69.   pinMode(MAX485_DE, OUTPUT);
  70.   digitalWrite(MAX485_RE_NEG, 0);
  71.   digitalWrite(MAX485_DE, 0);
  72.  
  73.   Serial.begin(9600);
  74.   WiFi.begin(ssid, password);
  75.   WiFi.setSleep(false);
  76.   pinMode(BUZZER_PIN, OUTPUT);
  77.  
  78.   while (WiFi.status() != WL_CONNECTED) {
  79.     delay(500);
  80.     Serial.print(".");
  81.   }
  82.  
  83.   Serial.println("");
  84.   Serial.print("Connected to WiFi network with IP Address: ");
  85.   Serial.println(WiFi.localIP());
  86.  
  87.   RS485Serial.begin(9600);
  88.   node.begin(1, RS485Serial);
  89.   node.preTransmission(preTransmission);
  90.   node.postTransmission(postTransmission);
  91. }
  92.  
  93. void loop() {
  94.   for (int i = 0; i < NUM_SENSORS; i++) {
  95.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  96.  
  97.     if (result == node.ku8MBSuccess) {
  98.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00);
  99.     } else {
  100.       Serial.println("Failed to read from sensor " + String(i));
  101.       delay(1000);
  102.       return;
  103.     }
  104.   }
  105.  
  106.   if (sensorData[TEMP_SENSOR] > (UPPER_THRESHOLD + HYSTERESIS)) {
  107.     digitalWrite(BUZZER_PIN, HIGH);
  108.     buzzerStatus = ON;
  109.     tempStatus = Panas;
  110.   } else if (sensorData[TEMP_SENSOR] < (LOWER_THRESHOLD - HYSTERESIS)) {
  111.     digitalWrite(BUZZER_PIN, HIGH);
  112.     buzzerStatus = ON;
  113.     tempStatus = Dingin;
  114.   } else {
  115.     digitalWrite(BUZZER_PIN, LOW);
  116.     buzzerStatus = OFF;
  117.     tempStatus = Normal;
  118.   }
  119.  
  120.   if (WiFi.status() == WL_CONNECTED) {
  121.     HTTPClient http;
  122.     http.begin(serverName);
  123.     http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  124.  
  125.     String httpRequestData = "api_key=" + String(apiKeyValue) + "&sensor=" + String(sensorName) + "&location=" + String(sensorLocation) +
  126.                              "&temperature=" + String(sensorData[TEMP_SENSOR]) + "&humidity=" + String(sensorData[HUM_SENSOR]) + "&buzz_Status=" + buzzerStatusToString(buzzerStatus) + "&temp_Status=" + tempStatusToString(tempStatus);
  127.    
  128.     Serial.print("httpRequestData: ");
  129.     Serial.println(httpRequestData);
  130.  
  131.     int httpResponseCode = http.POST(httpRequestData);
  132.  
  133.     if (httpResponseCode > 0) {
  134.       Serial.print("HTTP Response code: ");
  135.       Serial.println(httpResponseCode);
  136.     } else {
  137.       Serial.print("Error code: ");
  138.       Serial.println(httpResponseCode);
  139.     }
  140.  
  141.     http.end();
  142.   } else {
  143.     Serial.println("WiFi Disconnected");
  144.   }
  145.  
  146.   delay(10000);
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement