This shows you the differences between two versions of the page.
pm:prj2022:imacovei:home_assist_station [2022/05/27 23:17] ionut.iordache3010 [Software Design] |
pm:prj2022:imacovei:home_assist_station [2022/05/27 23:20] (current) ionut.iordache3010 [Software Design] |
||
---|---|---|---|
Line 90: | Line 90: | ||
* In functia loop() am citit datele venite de la Arduino caracter cu caracter si am construit valorile initiale citite de senzori. In continuare realizam de fiecare data conectarea catre server cu asteptarea aferenta inainte de a incepe o noua transmisie catre server. Functia getValue() prelua stringul meu, separatorul care am ales sa fie virgula, si indexul pentru valoarea pe care o doream din string, valoare aflata intre 2 virgule consecutive. | * In functia loop() am citit datele venite de la Arduino caracter cu caracter si am construit valorile initiale citite de senzori. In continuare realizam de fiecare data conectarea catre server cu asteptarea aferenta inainte de a incepe o noua transmisie catre server. Functia getValue() prelua stringul meu, separatorul care am ales sa fie virgula, si indexul pentru valoarea pe care o doream din string, valoare aflata intre 2 virgule consecutive. | ||
</note> | </note> | ||
+ | |||
+ | <code> | ||
+ | // Arduino Side Code | ||
+ | |||
#include <SD.h> // need to include the SD library | #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 <TMRpcm.h> // also need to include this library (used to create instance of TMRpcm) | ||
Line 220: | Line 224: | ||
} | } | ||
+ | </code> | ||
+ | |||
<code> | <code> | ||
- | //Arduino Side | + | // 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> | </code> | ||
===== Rezultate Obţinute ===== | ===== Rezultate Obţinute ===== |