Advertisement
mudhita_triari

1

Apr 19th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 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 5
  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. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  17. ModbusMaster node;
  18.  
  19. float sensorData[NUM_SENSORS];
  20. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  21.  
  22. // Replace with your SSID and Password
  23. const char* ssid = "Snellen_Rating";
  24. const char* password = "Injatama_892";
  25.  
  26. const char* serverName = "https://bootcampindobot.com/index.php";
  27.  
  28. // Keep this API Key value to be compatible with the PHP code provided in the project page.
  29. // If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
  30. String apiKeyValue = "tPmAT5Ab3j7F9";
  31.  
  32. String sensorName = "ModbusMaster";
  33. String sensorLocation = "My Room";
  34.  
  35. void preTransmission() {
  36. digitalWrite(MAX485_RE_NEG, 1);
  37. digitalWrite(MAX485_DE, 1);
  38. }
  39.  
  40. void postTransmission() {
  41. digitalWrite(MAX485_RE_NEG, 0);
  42. digitalWrite(MAX485_DE, 0);
  43. }
  44.  
  45. void setup() {
  46. pinMode(MAX485_RE_NEG, OUTPUT);
  47. pinMode(MAX485_DE, OUTPUT);
  48. digitalWrite(MAX485_RE_NEG, 0);
  49. digitalWrite(MAX485_DE, 0);
  50.  
  51. Serial.begin(9600);
  52. RS485Serial.begin(9600);
  53.  
  54. node.begin(1, RS485Serial);
  55. node.preTransmission(preTransmission);
  56. node.postTransmission(postTransmission);
  57.  
  58. // Connect to WiFi
  59. Serial.print("Connecting to ");
  60. Serial.println(ssid);
  61. WiFi.begin(ssid, password);
  62. WiFi.setSleep(false);
  63.  
  64. while (WiFi.status() != WL_CONNECTED) {
  65. delay(500);
  66. Serial.print(".");
  67. }
  68.  
  69. Serial.println("");
  70. Serial.print("Connected to WiFi network with IP Address: ");
  71. Serial.println(WiFi.localIP());
  72. }
  73.  
  74. void loop() {
  75. //Check WiFi connection status
  76. if (WiFi.status() == WL_CONNECTED) {
  77. HTTPClient http;
  78.  
  79. // Your Domain name with URL path or IP address with path
  80. http.begin(serverName);
  81.  
  82. // Specify content-type header
  83. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  84.  
  85. for (int i = 0; i < NUM_SENSORS; i++) {
  86. uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  87.  
  88. if (result == node.ku8MBSuccess) {
  89. sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  90. } else {
  91. delay(1000);
  92. return;
  93. }
  94. }
  95.  
  96. // Prepare your HTTP POST request data
  97. String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName + "&location=" + sensorLocation +
  98. "&value1=" + String(sensorData[TEMP_SENSOR]) + "&value2=" + String(sensorData[HUM_SENSOR]);
  99.  
  100. Serial.print("httpRequestData: ");
  101. Serial.println(httpRequestData);
  102.  
  103. // Send HTTP POST request
  104. int httpResponseCode = http.POST(httpRequestData);
  105.  
  106. if (httpResponseCode > 0) {
  107. Serial.print("HTTP Response code: ");
  108. Serial.println(httpResponseCode);
  109. }
  110. else {
  111. Serial.print("Error code: ");
  112. Serial.println(httpResponseCode);
  113. }
  114. // Free resources
  115. http.end();
  116. }
  117. else {
  118. Serial.println("WiFi Disconnected");
  119. }
  120. //Send an HTTP POST request every hour
  121. delay(10000);
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement