This shows you the differences between two versions of the page.
pm:prj2022:imacovei:home_assist_station [2022/05/27 23:10] ionut.iordache3010 [Software Design] |
pm:prj2022:imacovei:home_assist_station [2022/05/27 23:20] (current) ionut.iordache3010 [Software Design] |
||
---|---|---|---|
Line 91: | Line 91: | ||
</note> | </note> | ||
- | ''Code Text'' | + | <code> |
+ | // Arduino Side Code | ||
+ | |||
+ | #include <SD.h> // need to include the SD library | ||
+ | #include <TMRpcm.h> // also need to include this library (used to create instance of TMRpcm) | ||
+ | #include <SPI.h> // and this one too for SPI protocol | ||
+ | #include "DHT.h" // temperature and humidity sensor library | ||
+ | #include <Wire.h> // wire library | ||
+ | #include <LiquidCrystal_I2C.h> // liquid crystal with I2C library | ||
+ | #include <SoftwareSerial.h> // used to transmit data to ESP8266 | ||
+ | |||
+ | #define ultrasoundTrigger 4 // pin 4 used as output with ultrasonic sensor | ||
+ | #define ultrasoundEcho 2 // pin 2 used as input with ultrasonic sensor | ||
+ | #define smokeA0 A0 // pin A0 used as input with air-quality gas sensor | ||
+ | #define DHTPIN A1 // pin A1 used as input with temp&hum(DHT11) sensor | ||
+ | #define DHTTYPE DHT11 // sensor type = DHT11 | ||
+ | #define SD_ChipSelectPin 10 // pin 10 used as Chip Select - CS with SD module | ||
+ | #define SPEAKER_PIN 9 // pin 9 used as speaker | ||
+ | |||
+ | long duration; // variable for the duration of sound wave travel | ||
+ | float ultrasoundDistance; // variable for the distance measurement | ||
+ | String strArduinoToESP; // string to transmit from Arduino to ESP8266 NODEMCU | ||
+ | |||
+ | DHT dht(DHTPIN, DHTTYPE); // initialize DHT11 sensor | ||
+ | LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display | ||
+ | TMRpcm tmrpcm; // used by SD card to play sound | ||
+ | SoftwareSerial espSerial(5, 6); // RX & TX on pins 5 & 6 | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | espSerial.begin(9600);//TODO | ||
+ | |||
+ | // Ultrasonic sensor logic | ||
+ | // Serial.println("Ultrasonic Sensor HC-SR04 Test"); | ||
+ | pinMode(ultrasoundTrigger, OUTPUT); // Sets the trigPin as an OUTPUT | ||
+ | pinMode(ultrasoundEcho, INPUT); // Sets the echoPin as an INPUT | ||
+ | |||
+ | // Smoke sensor logic | ||
+ | //Serial.println(F("Smoke sensor test!")); | ||
+ | pinMode(smokeA0, INPUT); | ||
+ | |||
+ | // DHT11 sensor logic | ||
+ | //Serial.println(F("DHT11 sensor test!")); | ||
+ | dht.begin(); | ||
+ | |||
+ | // LCD&I2C logic | ||
+ | lcd.init(); // initialize the lcd | ||
+ | lcd.backlight(); // Turn on backlight | ||
+ | |||
+ | // SDcard logic | ||
+ | tmrpcm.speakerPin = SPEAKER_PIN; // speaker ouput pin 9 | ||
+ | if(!SD.begin(SD_ChipSelectPin)) { // check connection and return proper message | ||
+ | // Serial.println("SD fail"); | ||
+ | return; | ||
+ | } | ||
+ | tmrpcm.setVolume(6); // set output volume on pin selected | ||
+ | tmrpcm.play("test.wav"); // play sound from file | ||
+ | delay(22000); | ||
+ | |||
+ | // Welcome message | ||
+ | lcd.setCursor(0, 0); // set cursor at top left | ||
+ | lcd.print(" Welcome Home "); // print string | ||
+ | lcd.setCursor(0, 1); // set cursor at top left | ||
+ | lcd.print(" SIR "); // print string | ||
+ | delay(3000); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | delay(1500); // process to repeat every 3s (1.5s + 1.5s underneath) | ||
+ | // Serial.println("========="); | ||
+ | // Read from smoke sensor | ||
+ | int smokeVal = analogRead(smokeA0); | ||
+ | // Serial.print("Pin A0 smoke: "); | ||
+ | // Serial.print(smokeVal); | ||
+ | // Serial.println(" ppm"); | ||
+ | |||
+ | // Read from temp&hum(DHT11) sensor | ||
+ | float h = dht.readHumidity(); | ||
+ | float t = dht.readTemperature(); | ||
+ | // Serial.print(F("Humidity: ")); | ||
+ | // Serial.print(h); | ||
+ | // Serial.print(F(" % | Temperature: ")); | ||
+ | // Serial.print(t); | ||
+ | // Serial.println(F(" C")); | ||
+ | |||
+ | // Clears the trigPin condition | ||
+ | digitalWrite(ultrasoundTrigger, LOW); | ||
+ | delayMicroseconds(2); | ||
+ | // Sets the trigPin HIGH (ACTIVE) for 10 microseconds | ||
+ | digitalWrite(ultrasoundTrigger, HIGH); | ||
+ | delayMicroseconds(10); | ||
+ | digitalWrite(ultrasoundTrigger, LOW); | ||
+ | // Reads the echoPin, returns the sound wave travel time in microseconds | ||
+ | duration = pulseIn(ultrasoundEcho, HIGH); | ||
+ | delay(1000); | ||
+ | // Calculating the distance | ||
+ | ultrasoundDistance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back) | ||
+ | // Displays the distance on the Serial Monitor | ||
+ | // Serial.print("Distance: "); | ||
+ | // Serial.print(distance); | ||
+ | // Serial.println(" cm"); | ||
+ | |||
+ | // Print on LCD | ||
+ | lcd.setCursor(0, 0); // set cursor at top left | ||
+ | lcd.print("Hum: "); // print string | ||
+ | lcd.print(h); // humidity value | ||
+ | lcd.print(" % "); // unit | ||
+ | // --- | ||
+ | lcd.setCursor(0, 1); // set cursor at left space on 2nd row | ||
+ | lcd.print("Temp: "); // print string | ||
+ | lcd.print(t); // temperature value | ||
+ | lcd.print(" C "); // unit | ||
+ | // --- | ||
+ | delay(1500); | ||
+ | lcd.setCursor(0, 0); // set cursor at top left | ||
+ | lcd.print("Gas val: "); // print string | ||
+ | lcd.print(smokeVal); // gas value | ||
+ | lcd.print(" ppm"); // unit | ||
+ | // --- | ||
+ | lcd.setCursor(0, 1); // set cursor at left space on 2nd row | ||
+ | lcd.print("Dist: "); // print string | ||
+ | lcd.print(ultrasoundDistance); // distance value | ||
+ | lcd.print(" cm"); // unit | ||
+ | |||
+ | // Send data to ESP as a string | ||
+ | strArduinoToESP = strArduinoToESP + smokeVal + "," + h + "," + t + "," + ultrasoundDistance; //TODO am h si t ca float si in ex sunt int-uri | ||
+ | // Serial.println(strArduinoToESP); | ||
+ | espSerial.println(strArduinoToESP); // transmit string to ESP8266 | ||
+ | |||
+ | strArduinoToESP = ""; // reinitialize string value | ||
+ | |||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | // ESP8266 Side Code | ||
+ | |||
+ | #define BLYNK_TEMPLATE_ID "TMPL3SY-JM6J" | ||
+ | #define BLYNK_DEVICE_NAME "Blynk LED" | ||
+ | #define BLYNK_AUTH_TOKEN "RkiSi9tnBfPHmic1gaBwiC9upodMfHy1" | ||
+ | |||
+ | #define BLYNK_FIRMWARE_VERSION "0.1.0" | ||
+ | |||
+ | #define BLYNK_PRINT Serial | ||
+ | //#define BLYNK_DEBUG | ||
+ | |||
+ | #define APP_DEBUG | ||
+ | |||
+ | // Uncomment your board, or configure a custom board in Settings.h | ||
+ | //#define USE_SPARKFUN_BLYNK_BOARD | ||
+ | //#define USE_NODE_MCU_BOARD | ||
+ | //#define USE_WITTY_CLOUD_BOARD | ||
+ | //#define USE_WEMOS_D1_MINI | ||
+ | |||
+ | #include "BlynkEdgent.h" | ||
+ | #include <SoftwareSerial.h> | ||
+ | |||
+ | String strArduinoToESP; // string received from Arduino to ESP8266 NODEMCU | ||
+ | int smokeVal; | ||
+ | float humVal, tempVal, ultrasoundVal; | ||
+ | char rdata; // received character | ||
+ | |||
+ | const int motorspeed_pin = D1; | ||
+ | const int DIRA = D2; | ||
+ | const int DIRB = D7; | ||
+ | |||
+ | BLYNK_WRITE(V0) | ||
+ | { | ||
+ | int pinValue = param.asInt(); | ||
+ | digitalWrite(D5, pinValue); | ||
+ | } | ||
+ | |||
+ | BLYNK_WRITE(V5) | ||
+ | { | ||
+ | int pinValue = param.asInt(); | ||
+ | digitalWrite(D6, pinValue); | ||
+ | } | ||
+ | |||
+ | BLYNK_WRITE(V7) | ||
+ | { | ||
+ | int pinValue = param.asInt(); | ||
+ | if (pinValue == 1) { | ||
+ | digitalWrite(motorspeed_pin, HIGH); | ||
+ | digitalWrite(DIRA, HIGH); | ||
+ | digitalWrite(DIRB, LOW); | ||
+ | } else if (pinValue == 0) { | ||
+ | digitalWrite(motorspeed_pin, LOW); | ||
+ | digitalWrite(DIRA, LOW); | ||
+ | digitalWrite(DIRB, LOW); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void sendSensorsData() { | ||
+ | Blynk.virtualWrite(V1, smokeVal); | ||
+ | delay(200); | ||
+ | Blynk.virtualWrite(V2, humVal); | ||
+ | delay(200); | ||
+ | Blynk.virtualWrite(V3, tempVal); | ||
+ | delay(200); | ||
+ | Blynk.virtualWrite(V4, ultrasoundVal); | ||
+ | delay(200); | ||
+ | } | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | delay(100); | ||
+ | pinMode(D5, OUTPUT); | ||
+ | pinMode(D6, OUTPUT); | ||
+ | pinMode(motorspeed_pin, OUTPUT); | ||
+ | pinMode(DIRA, OUTPUT); | ||
+ | pinMode(DIRB, OUTPUT); | ||
+ | |||
+ | BlynkEdgent.begin(); | ||
+ | delay(2000); | ||
+ | edgentTimer.setInterval(1000L, sendSensorsData); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | if (Serial.available() > 0 ) { | ||
+ | rdata = Serial.read(); | ||
+ | strArduinoToESP = strArduinoToESP + rdata; | ||
+ | // Serial.print(rdata); | ||
+ | if ( rdata == '\n') { | ||
+ | // Serial.println(strArduinoToESP); | ||
+ | |||
+ | String l = getValue(strArduinoToESP, ',', 0); | ||
+ | String m = getValue(strArduinoToESP, ',', 1); | ||
+ | String n = getValue(strArduinoToESP, ',', 2); | ||
+ | String p = getValue(strArduinoToESP, ',', 3); | ||
+ | |||
+ | smokeVal = l.toInt(); | ||
+ | humVal = m.toFloat(); | ||
+ | tempVal = n.toFloat(); | ||
+ | ultrasoundVal = p.toFloat(); | ||
+ | |||
+ | // Serial.print("val: "); | ||
+ | // Serial.print(smokeVal); | ||
+ | // Serial.print(" | val: "); | ||
+ | // Serial.print(humVal); | ||
+ | // Serial.print(" | val: "); | ||
+ | // Serial.print(tempVal); | ||
+ | // Serial.print(" | val: "); | ||
+ | // Serial.print(ultrasoundVal); | ||
+ | // Serial.print(" | val: "); | ||
+ | // Serial.println(" |"); | ||
+ | |||
+ | BlynkEdgent.run(); | ||
+ | delay(1000); | ||
+ | edgentTimer.run(); // Initiates SimpleTimer | ||
+ | delay(1000); | ||
+ | |||
+ | strArduinoToESP = ""; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | String getValue(String data, char separator, int index) | ||
+ | { | ||
+ | int found = 0; | ||
+ | int strIndex[] = { 0, -1 }; | ||
+ | int maxIndex = data.length() - 1; | ||
+ | |||
+ | for (int i = 0; i <= maxIndex && found <= index; i++) { | ||
+ | if (data.charAt(i) == separator || i == maxIndex) { | ||
+ | found++; | ||
+ | strIndex[0] = strIndex[1] + 1; | ||
+ | strIndex[1] = (i == maxIndex) ? i+1 : i; | ||
+ | } | ||
+ | } | ||
+ | return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; | ||
+ | } | ||
+ | </code> | ||
===== Rezultate Obţinute ===== | ===== Rezultate Obţinute ===== | ||