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.