In order to connect our Things to Internet, we need to select some Internet communication protocol so that different Internet device can talk with each other. Some commonly used IoT protocols are HTTP, MQTT etc.
In questa lezione, impareremo a utilizzare il protocollo HTTP e a realizzare un semplice server web HTTP con lo shield MEGA-IoT di Osoyoo e il microcontrollore MEGA2560 e a mostrare una pagina web “Hello World!” a un browser remoto. La pagina web mostra anche la tensione di ingresso del pin A0 rilevata dalle funzioni analogRead() di Arduino.
1).Collegare la scheda di estensione MEGA-IoT di OSOYOO alla scheda avanzata di OSOYOO per la scheda MEGA2560:
2)Usare il cappuccio del ponticello per collegare ESP8266 RX con A8, TX con A9)
Software open-source (IDE) | Scarica l’IDE qui: https://www.arduino.cc/en/Main/Software?setlang=en |
|
7 zip è un’utility gratuita che decomprime i file zip. | Scaricate gratuitamente 7zip qui: https://www.7-zip.org/ |
Passo 1 Installare l’ultimo IDE (se si dispone di una versione dell’IDE successiva alla 1.1.16, saltare questo passo).Scaricare l’IDE dahttps://www.arduino.cc/en/software, quindi installare il software.
Passo 2 OSOYOO MEGA-IoT estende il pin TX/RX al pin A9/A8 per impostazione predefinita. Quindi, nel codice dello sketch, è necessario utilizzare la porta seriale del software per comunicare con l’ESP8266 (impostare A9 come TX e A8 come RX nell’oggetto SoftwareSerial): https://osoyoo.com/driver/WiFiEsp-master.zip
Passo 3 Aprire l’IDE ->Sketch ->Include Library ->Add ,Zip library per caricare i file zip di cui sopra in Arduino IDE.
Passo 4 Dopo aver installato la libreria di cui sopra, scaricare il codice principale dal seguente link.Decomprimere il file zip lesson3.zip, si vedrà una cartella chiamata smarthome-lesson3.
https://osoyoo.com/driver/smarthome/smarthome-lesson3.zip
Fase 5 Al termine delle operazioni sopra descritte, collegare la scheda OSOYOO MEGA2560 al PC con un cavo USB.
Fase 6 Aprire l’IDE e scegliere il tipo di scheda e il tipo di porta corrispondenti al progetto.
Passo 7 IDE: Fare clic su file -> fare clic su Apri -> scegliere il codice “smarthome-lesson3”, caricare lo sketch sulla scheda.
Nota: nello schizzo sono presenti le seguenti linee:
char ssid[] = "******"; // your network SSID (name) char pass[] = "******"; // your network password
Sostituire ****** con l’SSID e la password wifi corretti, altrimenti il progetto non potrà connettersi a Internet.
Dopo aver caricato lo sketch nell’IDE, aprite il monitor seriale nell’angolo superiore destro dell’IDE e vedrete il seguente risultato:
Dal monitor seriale è possibile vedere l’indirizzo IP della scheda MEGA2560 nel cerchio di lettura (nell’immagine precedente, 192.168.50.102).
Quindi utilizzare il browser per visitare il sito web http://mega2560-ip-address (in above case, http://192.168.50.102), si vedrà il seguente risultato:
Il risultato mostra anche la tensione di ingresso del pin A0 attraverso la funzione analogRead(0) nella riga 94. analogRead() è in grado di leggere la tensione di ingresso da 0 a 5 V e di restituire un valore compreso tra 0 e 1023 che rappresenta la tensione.
Se si collega A0 a GND tramite un ponticello, l’ingresso analogico A0: mostra 0
Se colleghiamo A0 a 5 V tramite un ponticello, l’ingresso analogico A0: mostra 1023.
A volte, quando si esegue il progetto lesson3, l’ESP8266 potrebbe avere problemi di inizializzazione con i pin A8 e A9 del MEGA2560 e questo causa l’errore di inizializzazione di WifiESP che viene visualizzato nel monitor seriale come segue:
[WiFiEsp] Initializing ESP module [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] >>> TIMEOUT >>> [WiFiEsp] Cannot initialize ESP module
Per risolvere il problema, è necessario utilizzare il pin stabile D10, D11 per inizializzare l’ESP8266 e poi passare nuovamente ad A8, A9. Per fare ciò, procedere come indicato di seguito:
La riga 15 originale è :
SoftwareSerial softserial(A9, A8); // RX, TX
Modificare la riga precedente in:
SoftwareSerial softserial(10, 11); // RX, TX
Passo 3) Caricare lo sketch nell’IDE e l’inizializzazione passerà normalmente attraverso e mostrerà:
[WiFiEsp] Initilization successful - 1.5.4 SSI
DownLoad Url osoyoo.com
You must be logged in to post a comment.
I am getting this error on the serial monitor:
[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present
Does this mean that my ESP shield is bad?
Nevermind. I solved the problem. The diagram showing the jumper cap connection is not very descriptive.
Hi Cbales!
I’m running into the same problem but I am sure I have the jumpers right.
What did you end up doing exactly?
Do I need a third jumper on Tx0 and Rx0 or something?
Check if your hotspot name and password are correct
Nevermind Cbales:
I had some bent pins on the board.
This is the message I am getting – what could I be doing wrong. I checked to make sure the ssid and password are correct and the pins are entered correctly.
[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present
tienes que descargarte la carpeta que te dan de wifiESP-master.zip y adjuntarla a la librería . Y después , cambiar el código a :
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Use browser and OSOYOO MEGA-IoT extension shield to control LED from remote browser
* Tutorial URL http://osoyoo.com/?p=28284
* CopyRight http://www.osoyoo.com
*/
#include “WiFiEsp.h”
// Emulate softserial on pins 6/7 if not present
//#ifndef HAVE_HWSERIAL1
#include “SoftwareSerial.h”
SoftwareSerial softserial (10, 11); // RX, TX
//#endif
char ssid[] = “vodafone3418”; // your network SSID (name)
char pass[] = “RJSPSB7TVU78R8”; // your network password
int status = WL_IDLE_STATUS;
int reqCount = 0; // number of requests received
WiFiEspServer server(80);
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
softserial.begin(115200);
softserial.write(“AT+CIOBAUD=9600\r\n”);
softserial.write(“AT+RST\r\n”);
softserial.begin(9600);
// initialize ESP module
WiFi.init(&softserial);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print(“Attempting to connect to WPA SSID: “);
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println(“You’re connected to the network”);
printWifiStatus();
// start the web server on port 80
server.begin();
}
void loop()
{
// listen for incoming clients
WiFiEspClient client = server.available();
if (client) {
Serial.println(“New client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
Serial.println(“Sending response”);
// send a standard http response header
// use \r\n instead of many println statements to speedup data send
client.print(
“HTTP/1.1 200 OK\r\n”
“Content-Type: text/html\r\n”
“Connection: close\r\n” // the connection will be closed after completion of the response
“Refresh: 20\r\n” // refresh the page automatically every 20 sec
“\r\n”);
client.print(“\r\n”);
client.print(“\r\n”);
client.print(“Hello World!\r\n”);
client.print(“Requests received: “);
client.print(++reqCount);
client.print(“\r\n”);
client.print(“Analog input A0: “);
client.print(analogRead(0));
client.print(“\r\n”);
client.print(“\r\n”);
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection:
client.stop();
Serial.println(“Client disconnected”);
}
}
void printWifiStatus()
{
// print the SSID of the network you’re attached to
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());
// print your WiFi shield’s IP address
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print(“To see this page in action, open a browser to http://“);
Serial.println(ip);
Serial.println();
}
y ya esta . Y ademas quitar los jumpers y conectar lo de un cable macho hembra a los dos pines que te dice .
I’m running into the same problem. The pins are OK.
What shall I do ?
Hi gents
I get the same problem and jumpers seem to be ok
How did you solve yours?
The wifi module works when I do the troubleshooting steps for connecting to 10, 11. However, when I try to roll back to the A8, A9 it is not working.
can anyone help by showing a picture of how the cables should be connected to A8, A9 — do i use the pnp jumpers or do i use the regualr jumper cables. Please help.
I’ll reply you by email
Hi David,
I havent received the reply yet.
Thanks
have you sent it yet?
The mailbox prompts that the mail was delivered successfully
can someone help me!
Hi everyone i´m trying to start wifi ESP with the following code
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Use browser and OSOYOO MEGA-IoT extension shield to control LED from remote browser
* Tutorial URL http://osoyoo.com/?p=28284
* CopyRight http://www.osoyoo.com
*/
#include “WiFiEsp.h”
// Emulate softserial on pins 6/7 if not present
//#ifndef HAVE_HWSERIAL1
#include “SoftwareSerial.h”
SoftwareSerial softserial(10, 11); // RX, TX
//SoftwareSerial softserial(A9, A8); // RX, TX
//#endif
char ssid[] = “TEST”; // your network SSID (name)
char pass[] = “0001234”; // your network password
int status = WL_IDLE_STATUS;
int reqCount = 0; // number of requests received
WiFiEspServer server(80);
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
softserial.begin(115200);
softserial.write(“AT+CIOBAUD=9600\r\n”);
softserial.write(“AT+RST\r\n”);
softserial.begin(9600);
// initialize ESP module
WiFi.init(&softserial);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
// don’t continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print(“Attempting to connect to WPA SSID: “);
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
Serial.println(“You’re connected to the network”);
printWifiStatus();
// start the web server on port 80
server.begin();
}
void loop()
{
// listen for incoming clients
WiFiEspClient client = server.available();
if (client) {
Serial.println(“New client”);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
Serial.println(“Sending response”);
// send a standard http response header
// use \r\n instead of many println statements to speedup data send
client.print(
“HTTP/1.1 200 OK\r\n”
“Content-Type: text/html\r\n”
“Connection: close\r\n” // the connection will be closed after completion of the response
“Refresh: 20\r\n” // refresh the page automatically every 20 sec
“\r\n”);
client.print(“\r\n”);
client.print(“\r\n”);
client.print(“Hello World!\r\n”);
client.print(“Requests received: “);
client.print(++reqCount);
client.print(“\r\n”);
client.print(“Analog input A0: “);
client.print(analogRead(0));
client.print(“\r\n”);
client.print(“\r\n”);
break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(10);
// close the connection:
client.stop();
Serial.println(“Client disconnected”);
}
}
void printWifiStatus()
{
// print the SSID of the network you’re attached to
Serial.print(“SSID: “);
Serial.println(WiFi.SSID());
// print your WiFi shield’s IP address
IPAddress ip = WiFi.localIP();
Serial.print(“IP Address: “);
Serial.println(ip);
// print where to go in the browser
Serial.println();
Serial.print(“To see this page in action, open a browser to http://“);
Serial.println(ip);
Serial.println();
}
but the serial monitor shows me the following message:
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present