# A Real-Time Water Level and Discharge Monitoring Station: A Case Study of the Sakarya River
## 2 Materials and Methods
### Software and Connections  
The communication module of the River monitoring station has been programmed using an Arduino program that interacts with sensors, utilizing PHP get functions. Arduino is an open-source development platform with an open library that allows processing of both digital and analog signals [30]. The Arduino program takes advantage of its open libraries to handle the communication between the sensors and the River monitoring station, ensuring seamless integration and efficient data processing. The Arduino program has been employed for the communication module of the river monitoring station's integrated components (Figure 4). Figure 3: Cabin Design. Through the coding within the program, communication is established with the SIM800 modem, and values read from the sensor are converted and transmitted to the server through the RS485 converter. In the coding, essential libraries, sensor values, server information, and other variable parameters are initially defined. As seen in the Arduino interface in Figure 5, these variables include APN settings, GPRS username and password, SIM card PIN code, and server address. The values read from the sensor are processed via RS485 and displayed on the serial port. Then, a GPRS connection is established and sent to the server via HTTP POST request. This request is carried out by the server with PHP get functions (Figure 6). Additionally, when data transfer is successful, the device is put into deep sleep mode with ESP32 and energy is saved. The ESP32 remains in deep sleep mode for a defined duration (set to 1 s in the code), and it wakes up automatically when this time elapses, as managed by a programmed timer. Figure 4: Monitoring Station Integrated Components. Figure 5: Arduino Program Interface and Protocols. Coding and communication protocols were written on the sim 800 L GSM module and ESP32 microcontroller card used in the manufactured system and data flow was provided. After it was determined that the sensor installed in the system supported RS-485 and MODBUS protocols, MAX485 integration was used for data conversion. The Modbus RTU communication protocol functions used and the distance at which the sensor measures at address 520 specified in the user manual are calculated with the given formula and transferred to the server with RS-485 conversion integration. The request formulated using the Modbus protocol is as follows:  
\(\{0\times 01,0\times 03,0\times 02,0\times 08,0\times 00,0\times 01,0\times 04,0 \times 70\}\), and it consists of 8 bytes. This includes information such as the sensor's network address, the command to read the registers, and the starting address of the data to be read. Byte (0 \(\times\) 01): The sensor's network address. Byte (0 \(\times\) 03): The command to read the registers. And 4. Bytes (0 \(\times\) 02, 0 \(\times\) 08): The starting address of the data to be read (520). And 6. Bytes (0 \(\times\) 00, 0 \(\times\) 01): The amount of data to be read (1 register). And 8. Bytes (0 \(\times\) 04, 0 \(\times\) 70): Used for error checking. The read value is converted according to the reference values provided by the sensor manufacturer and transmitted to the server in centimeters. This conversion is carried out using the following formula:  
\[\text{distance}=(\text{float})\text{adc0}\times 0.013528\times 2.54;\]  
In this formula, distance represents the calculated distance and is given in centimeters (cm). adc0 denotes the raw measurement value obtained from the sensor, provided by the analog-to-digital converter (ADC). The factor 0.013528 is used to convert the ADC value into a distance measurement and is based on the reference values specified in the sensor's datasheet. Additionally, 2.54 is a multiplier that facilitates the conversion from inches to centimeters, indicating that 1 inch is equivalent to 2.54 cm. This formula ensures that the sensor data are accurately converted into a distance measurement in centimeters and is consistent with the conversion formula specified in the sensor's datasheet.