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 5
- #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
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- float sensorData[NUM_SENSORS];
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- // Replace with your SSID and Password
- const char* ssid = "Snellen_Rating";
- const char* password = "Injatama_892";
- const char* serverName = "https://bootcampindobot.com/index.php";
- // Keep this API Key value to be compatible with the PHP code provided in the project page.
- // If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
- String apiKeyValue = "tPmAT5Ab3j7F9";
- String sensorName = "ModbusMaster";
- String sensorLocation = "My Room";
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- Serial.begin(9600);
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- // Connect to WiFi
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- WiFi.setSleep(false);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- }
- void loop() {
- //Check WiFi connection status
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- // Your Domain name with URL path or IP address with path
- http.begin(serverName);
- // Specify content-type header
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- 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.00F);
- } else {
- delay(1000);
- return;
- }
- }
- // Prepare your HTTP POST request data
- String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName + "&location=" + sensorLocation +
- "&value1=" + String(sensorData[TEMP_SENSOR]) + "&value2=" + String(sensorData[HUM_SENSOR]);
- Serial.print("httpRequestData: ");
- Serial.println(httpRequestData);
- // Send HTTP POST request
- int httpResponseCode = http.POST(httpRequestData);
- if (httpResponseCode > 0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- }
- else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- // Free resources
- http.end();
- }
- else {
- Serial.println("WiFi Disconnected");
- }
- //Send an HTTP POST request every hour
- delay(10000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement