Can't make connection between Arduino mega 2560 wifi esp8266 and esp8266

I am trying make the connection, but nothing comes of it. Within 5 days I try to make the simplest communication. It seems like this is happening through Serial3 on Arduino and Serial on ESP8266, but I can’t. The last attempt was to connect Arduino Mega to Arduino UNO via RX TX, transfer the data there, and transfer UNO via SoftSerial to Serial3 (to ESP) and accept to ESP via Serial, but this also does not work.

Sorry for my english ;c

add the sketches as text. and please the ATmega2560 sketch, not the Uno attempt.

one error I see is that the char array filled with readBytes in esp8266 is not terminated with zero. the atoi doesn't know where the string ends.

use int l = readBytes(digits, sizeof(digits)-1);
digits[l] = 0;

the readBytes will wait 1 second after the last character received, because it doesn't know it was the last.

the DIP switches want last long if you switch them many times. the board is good if you put a firmware in esp8266 and use a coresponding library in ATmega.
or if you want to have own sketch in esp8266, upload it OTA with ArduinoOTA library.

Waaait

mega

//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27,16,2);
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);

char digits[5];
int buzzer = 8;
int GASA0 = A0;
int gasvalue;

void setup() {
  Serial3.begin(9600); 
  mySerial.begin(9600);
  Serial.begin(9600);
  pinMode(buzzer, OUTPUT);
  delay(5000);
}

void loop() {
  int analogSensor = analogRead(GASA0);
  int gasvalue = (analogSensor - 50) / 10;
  itoa(gasvalue, digits, 10);
  Serial.write(digits);
  Serial3.write(digits); 
  if (gasvalue >= 20)
  {
    SendTextMessage();
    tone(buzzer, 6500, 3000);
  }
  else
  {
    noTone(buzzer);
  }
  delay(1500);
}

void SendTextMessage()
{
  mySerial.println("AT+CMGF=1");    //To send SMS in Text Mode
  delay(1000);
  mySerial.println("AT+CMGS=\"+num\"\r");  // change to the phone number you using
  delay(1000);
  mySerial.println("Attention! Gas level exceeded!");//the content of the message
  delay(200);
  mySerial.println((char)26);//the stopping character
  delay(2000);
}

esp

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#ifndef STASSID
#define STASSID "GasLeaker"
#define STAPSK  "11111111"
#endif
char digits[15];
int gasvalue;

const char* ssid = STASSID;
const char* password = STAPSK;

const char* host = "dvornick.ru";
const int httpsPort = 443;

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "90 65 a2 43 d1 fb c5 03 28 5f 93 50 34 55 1b ed a4 9c b4 2d";


void get_request(int x) {
  WiFiClientSecure client;
  Serial.print("connecting to ");
  Serial.println(host);

  Serial.printf("Using fingerprint '%s'\n", fingerprint);
  client.setFingerprint(fingerprint);

  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
  String url = "url";
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.println("reply was:");
  Serial.println("==========");
  Serial.println(line);
  Serial.println("==========");
  Serial.println("closing connection");
}

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



void loop() {
  while (Serial.available()) {
    Serial.readBytes(digits, 10);
    gasvalue = atoi(digits);
    Serial.print(gasvalue);
    get_request(gasvalue);
  }}

add the 0

btw

itoa(gasvalue, digits, 10);
Serial.write(digits);
Serial3.write(digits);

is same as

Serial.print(gasvalue);
Serial3.print(gasvalue);

Ok. Tenks. And i found a misprint: Serial3.begin(9600);
It must be looks like Serial3.begin(115200);, but it actually don't work ;c

Trying to make the simplest communication between Arduino and ESP. Why is this not working?

Dvornick:
Trying to make the simplest communication between Arduino and ESP. Why is this not working?

do you have the DIP switches in the right configuration?