Seriale tra Arduino e ESP8266

Salve a Tutti,
ho un modulo ESP8266 e vorrei utilizzarlo per ottenere data e ora in tempo reale dal Web. Ho scaricato ed installato la libreria "NTPtimeESP.h", ho riprogrammato il firmware dell'ESP con il seguente codice:

/*
   This sketch shows an example of sending a reading to data.sparkfun.com once per day.

   It uses the Sparkfun testing stream so the only customizing required is the WiFi SSID and password.

   The Harringay Maker Space
   License: Apache License v2
*/
#include "NTPtimeESP.h"

NTPtime NTP("it.pool.ntp.org");   // Choose server pool as required
char *ssid      = "MyWiFi";               // Set you WiFi SSID
char *password  = "MyPsw";               // Set you WiFi password

bool status;
byte currentHour;
byte currentMinute;
byte currentSecond;
int currentYear;
byte currentMonth;
byte currentDay;
byte currentDayofWeek;

unsigned long lastTimeMillis = 0;
unsigned long lastSerialBegin = 0;

/*
 * The structure contains following fields:
 * struct strDateTime
{
  byte hour;
  byte minute;
  byte second;
  int year;
  byte month;
  byte day;
  byte dayofWeek;
  boolean valid;
};
 */
strDateTime dateTime;

void setup() {
  delay(500);
  Serial.begin(115200);
  Serial.println("Connected");
  status = false;
  WiFi.mode(WIFI_STA);
  WiFi.begin (ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  status = true;
}

void loop() {
  if (!Serial) {
    Serial.begin(115200);
  }
  // first parameter: Time zone in floating point (for India); second parameter: 1 for European summer time; 2 for US daylight saving time; 0 for no DST adjustment; (contributed by viewwer, not tested by me)
  dateTime = NTP.getNTPtime(1.0, 0);

  // check dateTime.valid before using the returned time
  // Use "setSendInterval" or "setRecvTimeout" if required
  if (dateTime.valid){
    currentHour = dateTime.hour;
    currentMinute = dateTime.minute;
    currentSecond = dateTime.second;
    currentYear = dateTime.year;
    currentMonth = dateTime.month;
    currentDay = dateTime.day;
    currentDayofWeek = dateTime.dayofWeek;
    lastTimeMillis = millis();
  }

  if (Serial.available()) {
    String s = Serial.readString();
    int i = s.toInt();

    switch (i) {
      case 0: {                     //check status
        Serial.print(millis()-lastTimeMillis < 10000);
        break;
      }
      case 1: {                     //hour
        Serial.print(currentHour);
        break;
      }
      case 2: {                     //minute
        Serial.print(currentMinute);
        break;
      }
      case 3: {                     //second
        Serial.print(currentSecond);
        break;
      }
      case 4: {                     //day
        Serial.print(currentDay);
        break;
      }
      case 5: {                     //month
        Serial.print(currentMonth);
        break;
      }
      case 6: {                     //year
        Serial.print(currentYear);
        break;
      }
      case 7: {                     //day of the week
        int ris = currentDayofWeek-1;
        if (ris <= 0)
          ris += 7;
        Serial.print(ris);
        break;
      }
      case 8: {                     //change network ssid (name)          Send "8newNetworkName;" E.G. 8casaFilosi;
        s.substring(1, s.lastIndexOf(';')).toCharArray(ssid, sizeof(s)-2);
        Serial.print("New ssid: ");
        Serial.print(ssid);
        break;
      }
      case 9: {                     //change network password             Send "9newPsw;"   E.G. 9pippo2019;
        s.substring(1, s.lastIndexOf(';')).toCharArray(password, sizeof(s)-2);
        Serial.print("New password: ");
        Serial.print(password);
        break;
      }
      default: {
        
      }
    } 
  } else if (millis()-lastSerialBegin >= 2000) {             //Ho aggiunto questo else per cercare di risolvere il problema, ma senza successo.
    Serial.begin(115200);
    lastSerialBegin = millis();
    delay(100);
  }

  if (WiFi.status() != WL_CONNECTED) {
    status = false;
    WiFi.mode(WIFI_STA);
    WiFi.begin (ssid, password);
  }
  
}

.
in modo che Arduino possa inviare richieste specifiche tramite Serial all'ESP ed esso possa rispondere.
I collegamenti che ho effettuato per programmare l'ESP sono i seguenti:

 ESP ARDUINO
 
 TX TX
 CH_PD +3.3V
 RST +3.3V
 VCC +3.3V
 GND GND
 GPIO2 none
 GPIO0 GND
 RX RX
GND e RST di Arduino collegati.

.
Tutto ciò ha funzionato abbastanza bene, nel senso che l'ESP ha risposto:

Erasing 0x80000 bytes starting at 0x00000000
Uploading 315472 bytes from C:\Users\Tobia\AppData\Local\Temp\arduino_build_9065/ProvaNTPtimeESP.ino.bin to flash at 0x00000000
................................................................................ [ 25% ]
................................................................................ [ 51% ]
................................................................................ [ 77% ]
.....................................................................            [ 100% ]

.
Aprendo il seriale dall'IDE subito dopo l'upload, l'ESP risponde correttamente a tutti i comandi.
Mettendo però l'ESP come softwareSerial e invertendo TX e RX, non riesco a far comunicare Arduino e lo stesso. Il codice di Arduino (copiato da qualche parte):

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

SoftwareSerial mySerial(8, 9); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(115200);
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

Chiedo se qualcuno ha qualche idea e/o si è già imbattuto in questo problema.

Grazie in anticipo per l'aiuto.

Forse ti faccio una domanda cretina, ma hai tolto il ponticello tra ground e reset?

Darno2:
Forse ti faccio una domanda cretina, ma hai tolto il ponticello tra ground e reset?

Sì, l'ho tolto.

Ieri sera, mentre continuavo a fare prove, ho notato che se non tolgo corrente all'ESP dal momento in cui effettuo l'upload del firmware, riesce a comunicare con Arduino. Se invece, dopo l'upload, tolgo l'alimentazione, non comunicano più.

Come alimenti l'ESP8266? Se lo fai dalla linea dei 3V di Arduino semplicemente non ce la fa a meno che hai un clone con la linea potenziata.

Sì: lo alimento col pin +3.3V di Arduino.
Fornisce troppa poca corrente?

Si.

Ma Arduino non è in grado di fornire 800mA tra il +3.3V e Gnd?
Se sì, quanta corrente assorbe l'ESP?

da datasheet possiamo leggere che l'esp assorbe 80mA

e l'arduino (presumo arduino UNO essendo che non hai specificato, oppure non ho letto devo lo specificavi) da specifiche si legge DC Current for 3.3V Pin --> 50 mA

dire che non riesce ad alimentarlo al meglio...

non prendere per esatte le mie info... però mi baso sulle specifiche tecniche fornite... :slight_smile:

MD

MatteoDerrico:
da datasheet possiamo leggere che l'esp assorbe 80mA

... quello è l'assorbimento a "riposo" ... in trasmissione il ESP8266 assorbe anche più di 200mA mente, come hai giustamente indicato, il limite massimo del pin 3.3V di Arduino, è di 50mA.

Vi ricordo che in Megatopic c'è un thread di oltre 180 pagine interamente dedicato al ESP8266. Nella prima pagina si trovano una serie di link a guide e tutorials (fondamentale quella di SukkoPera) che dovreste leggere con attenzione!

Guglielmo

Alcuni cloni hanno una linea a 3.3V potenziata in grado di alimentare un ESP8266 ma sono pochi.

Scusate se non l'ho detto prima: sto usando Arduino Due, con un condensatore da 100microF tra +3.3V e GND. Mi sembrava sufficiente.
Ad ogni modo, sono riuscito a risolvere (non so esattamente come) il problema. A quanto pare era una configurazione di pin sbagliata.