ATtiny85 to ESP01

I want to connect ATtiny85 to ESP-01 and communicate between them using software serial. The problem is ATtiny85 can't send data to the ESP-01.

ATtiny85 uses SoftwareSerial while ESP-01 use the hardware serial both connected at 9600 baudrate

The schematic is shown in the attached image

Please post your full sketch(es).

If possible, you should always post code directly in the forum thread as text using code tags:

  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click the "Reply" button.
  • click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.

This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

boshkash1151's picture:

ESP sketch

#include <Arduino.h>

#include <FS.h>

#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <ArduinoOTA.h>
#include <ArduinoJson.h>          //https://github.com/bblanchon/ArduinoJson
#include <ESP8266Ping.h>
#include <ESP8266HTTPClient.h>
#include <base64.h>
#include "EEPROM.h"


// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void handleRequest(WiFiClient client);
void handleResponse(WiFiClient client, String response);
String readSerialResponse();



void connectWiFi() {

  WiFiManager wifiManager;
  wifiManager.setConfigPortalTimeout(180);
  if (!wifiManager.autoConnect("MY_NODE", "MY_PASSWORD")) {
    delay(3000);
    ESP.reset();
    delay(5000);
  }
}


void setup() {
  Serial.begin(9600);
  delay(10);
  connectWiFi();
  Serial.println("WiFi connected");
  server.begin();
  Serial.println("Server started");
  Serial.println(WiFi.localIP());
}

void loop() {
  //check if still connected to wifi
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("Not connected to the Wifi, will try to connect in 5 second");
    delay(5000);
    connectWiFi();
  }
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }
  handleRequest(client);
}

void handleRequest(WiFiClient client)
{
  String req = client.readStringUntil('\r');
  String requestBody;
  while (client.available()) {
    requestBody += (char)client.read();
  }
  String response = "";
  client.flush();
  if (req.indexOf("/alive") != -1)
  {
    response = sendAliveSignal();
  }
  else {
    Serial.println("invalid request");
    client.stop();
    return;
  }

  client.flush();
  handleResponse(client, response);

}

void handleResponse(WiFiClient client, String response)
{
  String s = response;
  client.print(s);
  delay(1);
}

String sendAliveSignal() {
  Serial.println("<alive>");
  return readSerialResponse();
}

#define START_CHAR '<'
#define END_CHAR '>'
String readSerialResponse() {
  String response = "";
  long start_timestamp = millis();
  //wait 2 seconds for serial response
  while (Serial.available() <= 0 && abs(millis() - start_timestamp) < 2000) {
    delay(10);
  }
  bool startCharFound = false;
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == END_CHAR) {
      break;
    }
    if (startCharFound)
      response += inChar;
    delay(2);
    if (inChar == START_CHAR) {
      startCharFound = true;
    }
  }
  response.trim();
  return response;
}

ATtiny85 sketch

#include <Arduino.h>
#include "SoftwareSerial.h"


void tryReadingSerial();
void handleCommand(String command);

#define SIGNAL_PIN 4
#define ESP_RESET_PIN 3
#define SERIAL_TX_PIN   0
#define SERIAL_RX_PIN   1
#define BLINK_PIN 2
SoftwareSerial espSoftwareSerial(SERIAL_RX_PIN, SERIAL_TX_PIN); //(RX,TX)
long esp_timeout_timestamp = 0;
long ESP_TIMEOUT_THRESHOLD = 10 * 60 * 1000; //10 MINUTES


void setup() {
  // put your setup code here, to run once:
  espSoftwareSerial.begin(9600);
  pinMode(SIGNAL_PIN, OUTPUT);
  pinMode(ESP_RESET_PIN, OUTPUT);
  pinMode(BLINK_PIN, OUTPUT);
  digitalWrite(SIGNAL_PIN, HIGH);
  digitalWrite(ESP_RESET_PIN, LOW);
  digitalWrite(BLINK_PIN, LOW);
}

void loop() {
  tryReadingSerial();
  if (millis() > esp_timeout_timestamp) { // reset the ESP
    if (millis() - esp_timeout_timestamp > ESP_TIMEOUT_THRESHOLD) { // only reset ESP if interval has passed and nothing received from ESP
      digitalWrite(ESP_RESET_PIN, HIGH);
      delay(10);
      digitalWrite(ESP_RESET_PIN, LOW);
    }
  }
  else { //overflow occured
    esp_timeout_timestamp = millis();
  }

}


#define START_CHAR '<'
#define END_CHAR '>'
void tryReadingSerial() {
  String command = "";
  bool startCharFound = false;
  while (espSoftwareSerial.available()) {
    digitalWrite(BLINK_PIN, HIGH);
    char inChar = (char)espSoftwareSerial.read();
    if (inChar == END_CHAR)
      break;
    if (startCharFound)
      command += inChar;
    if (inChar == START_CHAR)
      startCharFound = true;
    delay(2);
    digitalWrite(BLINK_PIN, LOW);
    delay(2);
  }
  command.trim();
  if (command != "") {
    handleCommand(command);
  }
}

void handleCommand(String command) {
  if (command == "alive") {
    esp_timeout_timestamp = millis();
    espSoftwareSerial.println("<Alive signal recieved...>");
  }
}

Did you get just the serial communication between the tiny85 and and ESP to work before adding all of the other function?

Have you looked at the techniques in the serial input basics tutorial?