0% found this document useful (0 votes)
6 views1 page

Smart SCADA System Code for STM32

Uploaded by

singhbijenda254
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Smart SCADA System Code for STM32

Uploaded by

singhbijenda254
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Smart SCADA Hybrid System (STM32 Blue Pill +

GSM SIM7670 + Sensors)


This document contains the complete Arduino IDE and STM32CubeIDE compatible code for your
hybrid Smart SCADA System. The SCADA HTTP URL field has been left blank for user
customization.

Arduino IDE Code (Smart_SCADA_Arduino.ino)


#include #include HardwareSerial gsm(USART1); STM32RTC& rtc =
STM32RTC::getInstance(); #define VR_PHASE PA0 #define VY_PHASE PA1 #define VB_PHASE
PA2 #define IR_PHASE PA3 #define IY_PHASE PA4 #define IB_PHASE PA5 #define
PRESSURE_SENSOR PA6 #define ULTRA_TRIG PB0 #define ULTRA_ECHO PB1 #define RELAY1 PB5
#define RELAY2 PB6 #define FAULT_LED PB7 float vR, vY, vB, iR, iY, iB, pressure,
waterLevel; char buffer[300]; void setup() { [Link](115200); [Link](9600);
[Link](); pinMode(RELAY1, OUTPUT); pinMode(RELAY2, OUTPUT); pinMode(FAULT_LED,
OUTPUT); pinMode(ULTRA_TRIG, OUTPUT); pinMode(ULTRA_ECHO, INPUT); } float
readVoltage(int pin) { return (analogRead(pin) * 3.3 / 4095.0) * (415.0 / 3.3); }
float readCurrent(int pin) { return (analogRead(pin) * 3.3 / 4095.0) * 10.0; } float
readPressure() { float sensorValue = analogRead(PRESSURE_SENSOR) * 3.3 / 4095.0;
return (sensorValue / 5.0) * 10.0; } float readWaterLevel() {
digitalWrite(ULTRA_TRIG, LOW); delayMicroseconds(2); digitalWrite(ULTRA_TRIG, HIGH);
delayMicroseconds(10); digitalWrite(ULTRA_TRIG, LOW); long duration =
pulseIn(ULTRA_ECHO, HIGH); float distance = duration * 0.034 / 2; return
constrain(100 - (distance / 2), 0, 100); } void sendDataToSCADA() { vR =
readVoltage(VR_PHASE); vY = readVoltage(VY_PHASE); vB = readVoltage(VB_PHASE); iR =
readCurrent(IR_PHASE); iY = readCurrent(IY_PHASE); iB = readCurrent(IB_PHASE);
pressure = readPressure(); waterLevel = readWaterLevel(); uint8_t hour =
[Link](); uint8_t minute = [Link](); uint8_t second =
[Link](); snprintf(buffer, sizeof(buffer), "{ \"Device_ID\": \"SCADA_001\",
\"Voltage\": {\"R\": %.2f, \"Y\": %.2f, \"B\": %.2f}, " "\"Current\": {\"R\": %.2f,
\"Y\": %.2f, \"B\": %.2f}, " "\"Pressure\": %.2f, \"Water_Level\": %.2f,
\"RTC_Time\": \"%02d:%02d:%02d\" }", vR, vY, vB, iR, iY, iB, pressure, waterLevel,
hour, minute, second); [Link]("AT+HTTPINIT"); delay(100);
[Link]("AT+HTTPPARA=\"URL\",\"\""); // Leave SCADA URL blank delay(100);
[Link]("AT+HTTPDATA="); [Link](strlen(buffer)); delay(100);
[Link](buffer); delay(100); [Link]("AT+HTTPACTION=1"); delay(5000);
[Link]("AT+HTTPTERM"); } void loop() { sendDataToSCADA(); delay(15000); }

STM32CubeIDE Code (Smart_SCADA_STM32.c)


#include "main.h" #include "stdio.h" #include "string.h" ADC_HandleTypeDef hadc1;
UART_HandleTypeDef huart1; RTC_HandleTypeDef hrtc; char txBuffer[512]; float
Read_ADC(uint32_t channel) { ADC_ChannelConfTypeDef sConfig = {0}; [Link] =
channel; [Link] = 1; [Link] = ADC_SAMPLETIME_1CYCLE_5;
HAL_ADC_ConfigChannel(&hadc1;, &sConfig;); HAL_ADC_Start(&hadc1;);
HAL_ADC_PollForConversion(&hadc1;, HAL_MAX_DELAY); uint32_t raw =
HAL_ADC_GetValue(&hadc1;); HAL_ADC_Stop(&hadc1;); return ((float)raw * 3.3f /
4095.0f); } void GSM_SendData(void) { float vR = Read_ADC(ADC_CHANNEL_0); float vY =
Read_ADC(ADC_CHANNEL_1); float vB = Read_ADC(ADC_CHANNEL_2); float iR =
Read_ADC(ADC_CHANNEL_3); float iY = Read_ADC(ADC_CHANNEL_4); float iB =
Read_ADC(ADC_CHANNEL_5); float pressure = Read_ADC(ADC_CHANNEL_6); float water =
Read_ADC(ADC_CHANNEL_7); RTC_TimeTypeDef sTime; HAL_RTC_GetTime(&hrtc;, &sTime;,
RTC_FORMAT_BIN); sprintf(txBuffer, "{ \"Device_ID\": \"SCADA_001\", " "\"Voltage\":
{\"R\": %.2f, \"Y\": %.2f, \"B\": %.2f}, " "\"Current\": {\"R\": %.2f, \"Y\": %.2f,
\"B\": %.2f}, " "\"Pressure\": %.2f, \"Water_Level\": %.2f, " "\"RTC_Time\":
\"%02d:%02d:%02d\" }", vR, vY, vB, iR, iY, iB, pressure, water, [Link],
[Link], [Link]); char cmd[128]; sprintf(cmd,
"AT+HTTPPARA=\"URL\",\"\"\r\n"); HAL_UART_Transmit(&huart1;, (uint8_t *)cmd,
strlen(cmd), 100); HAL_UART_Transmit(&huart1;, (uint8_t *)txBuffer,
strlen(txBuffer), 100); }

You might also like