there is a sign of communication from arduino to esp8266, and blynk to esp, and esp to arduino. But the problem is, when the arduino sends data to esp8266 the data is sent but not been uploaded to the blynk.
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6aPlgCd6b"
#define BLYNK_TEMPLATE_NAME "NostraDamn"
#define BLYNK_AUTH_TOKEN "yGAX5dneuEsM59HUnztaIbyEsAcrR7Ey"
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
const char *ssid = "";
const char *password = "";
const char auth[] = "yGAX5dneuEsM59HUnztaIbyEsAcrR7Ey";
#define arduinoSerialRx D7
#define arduinoSerialTx D6
SoftwareSerial arduinoSerial(arduinoSerialRx, arduinoSerialTx);
bool servoControlEnabled = false;
void setup() {
Serial.begin(9600);
arduinoSerial.begin(9600); // SoftwareSerial for communication with the Arduino Nano
connectWiFi();
Blynk.config(BLYNK_AUTH_TOKEN);
while (Blynk.connect() == false) {
Serial.println("Failed to connect to Blynk. Retrying...");
delay(1000);
}
Serial.println("Connected to Blynk");
}
void loop() {
Blynk.run();
readSerialData();
}
void connectWiFi() {
WiFi.begin(ssid, password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(1000);
Serial.println("Connecting to WiFi...");
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("Connected to WiFi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("Failed to connect to WiFi");
}
}
void readSerialData() {
while (arduinoSerial.available() > 0) {
String receivedData = arduinoSerial.readStringUntil('\n');
if (receivedData.length() > 0) {
Serial.println("Received Data from Arduino Nano: " + receivedData);
processReceivedData(receivedData);
}
}
delayMicroseconds(1000);
}
void processReceivedData(String data) {
int delimiterIndex = data.indexOf(',');
if (delimiterIndex != -1) {
String dataType = data.substring(0, delimiterIndex);
String dataValue = data.substring(delimiterIndex + 1);
if (dataType.equals("DIST")) {
Serial.println("Received DIST data: " + dataValue);
Blynk.virtualWrite(V0, dataValue.toFloat());
Blynk.virtualWrite(V4, dataValue.toFloat()); // Send data to V4 for the graph
} else if (dataType.equals("TEMP")) {
Serial.println("Received TEMP data: " + dataValue);
Blynk.virtualWrite(V1, dataValue.toFloat());
} else if (dataType.equals("HUMID")) {
Serial.println("Received HUMID data: " + dataValue);
Blynk.virtualWrite(V2, dataValue.toFloat());
} else if (dataType.equals("SERVO")) {
if (servoControlEnabled) {
int servoPos = dataValue.toInt();
Serial.println("Received SERVO data: " + dataValue);
Blynk.virtualWrite(V3, servoPos);
}
}
}
}
BLYNK_WRITE(V3) {
Serial.println("Blynk.Cloud is writing something to V3");
sendToArduino("SERVO:" + String(param.asInt()));
}
// Function to send data to Arduino through SoftwareSerial
void sendToArduino(String data) {
Serial.println("Sending to Arduino: " + data); // Print for debugging
arduinoSerial.println(data);
}
The reason i was using two micro controllers because of the load of the sensors and for the esp8266 to focus on application (it was for the website before but later changed to the app.)
and this is the code for the arduino nano:
#include <Adafruit_AHT10.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Servo.h>
// Function declarations
void sendToESP8266(unsigned long timestamp, String data);
float measureDistance();
void displayOnLCD(String msg);
void calibrateServo();
const int LCD_ADDRESS = 0x27;
const int AHT10_ADDRESS = 0x38;
const int trigPin = 10;
const int echoPin = 11;
const int MAX_DISTANCE = 6; // Set the maximum distance in centimeters
const int MIN_DISTANCE = 4; // Set the minimum distance in centimeters
const int RX_PIN = 8; // Nano RX, ESP8266 TX
const int TX_PIN = 7; // Nano TX, ESP8266 RX
const int SERVO_PIN = 9; // Servo control pin
SoftwareSerial espSerial(RX_PIN, TX_PIN);
Servo servo;
Adafruit_AHT10 aht;
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);
void setup() {
Wire.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.attach(SERVO_PIN);
if (!aht.begin()) {
Serial.println("Failed to initialize AHT10 sensor!");
while (1);
}
lcd.begin();
lcd.backlight();
Serial.begin(9600);
espSerial.begin(9600);
Serial.println("Setup complete!");
calibrateServo(); // Call the calibration function after setup
}
void calibrateServo() {
// Set the initial position of the servo (adjust the angle as needed)
servo.write(180);
delay(500); // Allow time for the servo to reach its position
}
void loop() {
sensors_event_t humidity, temperature;
aht.getEvent(&humidity, &temperature);
unsigned long timestamp = millis();
// Send humidity and temperature data to ESP8266
sendToESP8266(timestamp, "HUMID:" + String(humidity.relative_humidity, 1) + ", TEMP:" + String(temperature.temperature, 1));
// Only measure distance and send
float dist = measureDistance();
sendToESP8266(timestamp, "DIST:" + String(dist));
displayOnLCD("T:" + String(temperature.temperature, 1) + " C");
delay(1000);
displayOnLCD("H:" + String(humidity.relative_humidity, 1) + " %");
delay(1000);
displayOnLCD("Dist:" + String(dist) + " cm");
delay(1000);
// Determine servo position based on distance
int servoPos = map(dist, MIN_DISTANCE, MAX_DISTANCE, 180, 0);
if (dist > MAX_DISTANCE) {
// If distance is greater than 6cm, set servo to 0
servoPos = 0;
} else if (dist <= MIN_DISTANCE) {
// If distance is less than or equal to 4cm, set servo to 180
servoPos = 180;
} else {
// If distance is between 4cm and 5.5cm, set servo to 90
servoPos = 90;
}
// Move the servo based on the calculated position
servo.write(servoPos);
// Send servo position to ESP8266
sendToESP8266(timestamp, "SERVO:" + String(servoPos));
// Delay to allow the servo to move
delay(1500);
// Handle servo position data from ESP8266
while (espSerial.available()) {
String receivedData = espSerial.readStringUntil('\n');
Serial.println("Received Data from ESP8266: " + receivedData);
int delimiterIndex = receivedData.indexOf(',');
if (delimiterIndex != -1) {
String dataType = receivedData.substring(0, delimiterIndex);
String dataValue = receivedData.substring(delimiterIndex + 1);
if (dataType.equals("SERVO")) {
int receivedServoPos = dataValue.toInt();
displayOnLCD("Servo Pos: " + String(receivedServoPos));
delay(2000); // Display for 2 seconds
servo.write(receivedServoPos);
}
}
}
}
float measureDistance() {
long startTime = 0, endTime = 0;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
while (digitalRead(echoPin) == LOW);
startTime = micros();
while (digitalRead(echoPin) == HIGH);
endTime = micros();
long duration = endTime - startTime;
return duration * 0.034 / 2;
}
void sendToESP8266(unsigned long timestamp, String data) {
Serial.println(data);
espSerial.println(String(timestamp) + "," + data);
}
void displayOnLCD(String msg) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(msg);
}
@Jay_Mark_Velarde Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.