Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <Wire.h>
- #include <HTTPClient.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #define MAX485_RE_NEG 2
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- #define BUZZER_PIN 26
- #define UPPER_THRESHOLD 28.0
- #define LOWER_THRESHOLD 20.0
- #define HYSTERESIS 0.3
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- float sensorData[NUM_SENSORS];
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- const char* ssid = "Khansa";
- const char* password = "ghiffari";
- const char* serverName = "https://bootcampindobot.com/index-ghiffari-esp32.php";
- String apiKeyValue = "Td45OOuqjpGxNplQ6PK5JHv55GUi6Pkyv4lAzNSvKqEf08HvITj4XXfZl8fAwBg4";
- const char* sensorName = "SHT20";
- const char* sensorLocation = "Kamar";
- enum BuzzerStatus { OFF, ON };
- enum TempStatus { Normal, Panas, Dingin };
- BuzzerStatus buzzerStatus = OFF;
- TempStatus tempStatus = Normal;
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- String buzzerStatusToString(BuzzerStatus status) {
- switch (status) {
- case OFF: return "OFF";
- case ON: return "ON";
- }
- }
- String tempStatusToString(TempStatus status) {
- switch (status) {
- case Normal: return "Normal";
- case Panas: return "Panas";
- case Dingin: return "Dingin";
- }
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- Serial.begin(9600);
- WiFi.begin(ssid, password);
- WiFi.setSleep(false);
- pinMode(BUZZER_PIN, OUTPUT);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- }
- void loop() {
- for (int i = 0; i < NUM_SENSORS; i++) {
- uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
- if (result == node.ku8MBSuccess) {
- sensorData[i] = float(node.getResponseBuffer(0) / 10.00);
- } else {
- Serial.println("Failed to read from sensor " + String(i));
- delay(1000);
- return;
- }
- }
- if (sensorData[TEMP_SENSOR] > (UPPER_THRESHOLD + HYSTERESIS)) {
- digitalWrite(BUZZER_PIN, HIGH);
- buzzerStatus = ON;
- tempStatus = Panas;
- } else if (sensorData[TEMP_SENSOR] < (LOWER_THRESHOLD - HYSTERESIS)) {
- digitalWrite(BUZZER_PIN, HIGH);
- buzzerStatus = ON;
- tempStatus = Dingin;
- } else {
- digitalWrite(BUZZER_PIN, LOW);
- buzzerStatus = OFF;
- tempStatus = Normal;
- }
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- http.begin(serverName);
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- String httpRequestData = "api_key=" + String(apiKeyValue) + "&sensor=" + String(sensorName) + "&location=" + String(sensorLocation) +
- "&temperature=" + String(sensorData[TEMP_SENSOR]) + "&humidity=" + String(sensorData[HUM_SENSOR]) + "&buzz_Status=" + buzzerStatusToString(buzzerStatus) + "&temp_Status=" + tempStatusToString(tempStatus);
- Serial.print("httpRequestData: ");
- Serial.println(httpRequestData);
- int httpResponseCode = http.POST(httpRequestData);
- if (httpResponseCode > 0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- } else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- http.end();
- } else {
- Serial.println("WiFi Disconnected");
- }
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement