Created
November 15, 2025 23:00
-
-
Save VladVanyuk/e678922acb554581ede7d673c91ddbee to your computer and use it in GitHub Desktop.
scada lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <DHT11.h> | |
| #include <Wire.h> | |
| #include <BME280I2C.h> | |
| // #include <ESP8266WiFi.h> | |
| // #include <MQTT.h> | |
| #define MOTOR_INA_PIN D5 | |
| #define MOTOR_INB_PIN D6 | |
| #define DHT_SENSOR_PIN D7 | |
| // const char ssid[] = "ssid"; | |
| // const char pass[] = "pass"; | |
| // WiFiClient net; | |
| // MQTTClient client; | |
| DHT11 dht11(D7); | |
| BME280I2C bme280; | |
| const uint32_t readDHT_timer = 5000; | |
| uint32_t lastTempRead = 0; | |
| const uint32_t readGY_timer = 3000; | |
| uint32_t lastGYRead = 0; | |
| uint32_t timeInited = 0; | |
| float temperature = 0; | |
| const float tempToStartMotors = 28.0; | |
| bool stateMotor = false; | |
| float printBME280Data(Stream* client) | |
| { | |
| float temp(NAN), hum(NAN), pres(NAN); | |
| BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); | |
| BME280::PresUnit presUnit(BME280::PresUnit_Pa); | |
| bme280.read(pres, temp, hum, tempUnit, presUnit); | |
| client->print("Temp: "); | |
| client->print(temp); | |
| client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F')); | |
| client->print("\t\tHumidity: "); | |
| client->print(hum); | |
| client->print("% RH"); | |
| client->print("\t\tPressure: "); | |
| client->print(pres); | |
| client->println("Pa"); | |
| return temp; | |
| } | |
| bool readTempHumidity() | |
| { | |
| int temperature = 0; | |
| int humidity = 0; | |
| // Attempt to read the temperature and humidity values from the DHT11 sensor. | |
| int result = dht11.readTemperatureHumidity(temperature, humidity); | |
| // Check the results of the readings. | |
| // If the reading is successful, print the temperature and humidity values. | |
| // If there are errors, print the appropriate error messages. | |
| if (result == 0) { | |
| Serial.print("Temperature: "); | |
| Serial.print(temperature); | |
| Serial.print(" °C\tHumidity: "); | |
| Serial.print(humidity); | |
| Serial.println(" %"); | |
| return true; | |
| } else { | |
| // Print error message based on the error code. | |
| Serial.println(DHT11::getErrorString(result)); | |
| return false; | |
| } | |
| } | |
| bool readTemp() | |
| { | |
| // Attempt to read the temperature value from the DHT11 sensor. | |
| int temperature = dht11.readTemperature(); | |
| // Check the result of the reading. | |
| // If there's no error, print the temperature value. | |
| // If there's an error, print the appropriate error message. | |
| if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT) { | |
| Serial.print("Temperature: "); | |
| Serial.print(temperature); | |
| Serial.println(" °C"); | |
| return true; | |
| } else { | |
| // Print error message based on the error code. | |
| Serial.println(DHT11::getErrorString(temperature)); | |
| return false; | |
| } | |
| } | |
| void setup() | |
| { | |
| Serial.begin(9600); | |
| delay(10); | |
| Serial.println(); | |
| Serial.println("Init started"); | |
| Serial.println("BME INIT"); | |
| Wire.begin(); //Join I2C bus | |
| bme280.begin(); | |
| switch(bme280.chipModel()) | |
| { | |
| case BME280::ChipModel_BME280: | |
| Serial.println("Found BME280 sensor! Success."); | |
| break; | |
| case BME280::ChipModel_BMP280: | |
| Serial.println("Found BMP280 sensor! No Humidity available."); | |
| break; | |
| default: | |
| Serial.println("Found UNKNOWN sensor! Error!"); | |
| } | |
| Serial.println("MOTOR INIT"); | |
| pinMode(MOTOR_INA_PIN,OUTPUT); | |
| pinMode(MOTOR_INB_PIN,OUTPUT); | |
| // Serial.println("WIFI"); | |
| // WiFi.begin(ssid, pass); | |
| // client.begin("public.cloud.shiftr.io", net); | |
| // client.onMessage(messageReceived); | |
| // connect(); | |
| timeInited = millis(); | |
| Serial.println("INIT ENDED"); | |
| } | |
| void loop() | |
| { | |
| uint32_t now = millis(); | |
| // client.loop(); | |
| // if (!client.connected()) { | |
| // connect(); | |
| // } | |
| // publish a message roughly every second. | |
| // if (millis() - lastMillis > 1000) { | |
| // lastMillis = millis(); | |
| // client.publish("/temp", temperature); | |
| // todo: OLYA add what to publish | |
| // } | |
| if(temperature > tempToStartMotors) | |
| { | |
| stateMotor = true; | |
| digitalWrite(MOTOR_INA_PIN,LOW); | |
| digitalWrite(MOTOR_INB_PIN,HIGH); | |
| } | |
| else | |
| { | |
| stateMotor = false; | |
| digitalWrite(MOTOR_INA_PIN,LOW); | |
| digitalWrite(MOTOR_INB_PIN,LOW); | |
| // client.publish("/motor", stateMotor); | |
| } | |
| if(now - lastTempRead > readDHT_timer) | |
| { | |
| readTempHumidity(); | |
| lastTempRead = now; | |
| // client.publish("/hum", temperature); | |
| } | |
| if(now - lastGYRead > readGY_timer) | |
| { | |
| temperature = printBME280Data(&Serial); | |
| lastGYRead = now; | |
| // client.publish("/temp", temperature); | |
| // client.publish("/pres", ???create new var for pres or make posting within function printBME280Data???); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment