Hi everyone, I connected arduino with the esp01 board via serial.
I have tried them all but I can't.
I want to pass the temperature and humidity values to the wifi card connected to the home network which I can then read from my browser
Below I report the code to be put on both Arduino and esp01.
The problem appears to be data synchronization between the two cards.
Thank you all
ciao a tutti, ho collegato arduino con la scheda esp01 via seriale.
le ho provate tutte ma non riesco.
voglio passare i valori della temperature e della umidita' alla schedina wifi connessa ala rete domestica che poi posso rilevare dal mio browser
Di seguito riporto il codice da mettere sia su Arduino che su esp01.
Il problema sembra la sincronizzazione dei dati tra le due schede.
Grazie a tutti
ESP01:
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "123456";
WiFiServer server(80);
String line;
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
digitalWrite(LED_BUILTIN, LOW);
server.begin();
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage(String toWrite) {
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F("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: 5\r\n" // refresh the page automatically every 5 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>");
htmlPage += toWrite;
htmlPage += F("</html>"
"\r\n");
return htmlPage;
}
void loop() {
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client) {
while (client.connected()) {
if (client.available()) {
while (line.length() < 1) {
line = Serial.readString();
Serial.print("Read");
}
if (line.length() >= 1) {
client.println(prepareHtmlPage(line));
Serial.print("Read");
break;
}
}
}
while (client.available()) {
// but first, let client finish its request
// that's diplomatic compliance to protocols
// (and otherwise some clients may complain, like curl)
// (that is an example, prefer using a proper webserver library)
client.read();
}
// close the connection:
client.stop();
}
}
ARDUINO:
float umidita = 45;
float temperatura = 24.7;
bool flag = true;
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.readString() == "Read") {
flag = true;
}
if (flag) {
String comunicazione = String(umidita) + "$" + String(temperatura) + "$" + "25/12/2021 16:38" + "\n";
Serial.print(comunicazione);
flag = false;
}
}
What is the Arduino board?
How is the ESP connected to the mystery Arduino board?
You seem to be reading from the ESP serial port before you know if anything is available in the serial input buffer. The serial input basics tutorial has robust methods for reading and parsing serial input.
Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum. Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
Please go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
I would suggest that you get a NodeMCU board or Wemos D1 board instead of using an Arduino and ESP-01. It would make your life much easier. One board to do it all.
maybe i succeeded, the only drawback is that i have to turn on esp-01 after arduino, in my opinion they are not synchronized.
I state that Arduino is powered by the usb of the pc, while esp-01 by an external power supply (arduino does not give enough current to 3.3 volts) with gnd connected in common.
I send the code, if someone to vesse solutions ...
#include <ESP8266WiFi.h>
const char* ssid = "SSID";
const char* password = "123456789";
String valoriRicevuti = "In Attesa dei dati";
WiFiServer server(80);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(19200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println(" connected");
server.begin();
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage(String toWrite)
{
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F("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: 5\r\n" // refresh the page automatically every 5 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>"
"<h1>Dati di oggi:</h1>"
"<p>Rilevazioni comunicate: ");
htmlPage += toWrite;
htmlPage += F("</p>"
"</html>"
"\r\n");
return htmlPage;
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = Serial.readString();
if (line.length() >= 1)
{
valoriRicevuti = line;
}
client.println(prepareHtmlPage(valoriRicevuti));
break;
}
}
while (client.available()) {
// but first, let client finish its request
// that's diplomatic compliance to protocols
// (and otherwise some clients may complain, like curl)
// (that is an example, prefer using a proper webserver library)
client.read();
}
// close the connection:
client.stop();
}
}
Niente non riesco, aiuto.
vorrei trasmettere via seriale delle stringhe da arduino a esp-01 che poi me trasmette via http
il programma che ho scritto sopra funziona in parte, cioe' a volte su internet trovo la stringa doppia o tripla di quello che dovrebbe essere.
premetto che il tutto funziona perfettamente dal monitor della seriale, ma quando collego via seriale i due si presenta il problema.
Nothing I can not, help. I would like to transmit strings via serial line from arduino to esp-01 which it then transmits to me via http the program I wrote above works partially, ie sometimes on the internet I find the double or triple string of what it should be. I state that everything works perfectly from the serial monitor, but when I connect the two via serial, the problem arises.
#include <ESP8266WiFi.h>
#include <Arduino.h>
const char* ssid = "SSID";
const char* password = "123456789";
String valoriRicevuti = "In Attesa dei dati";
WiFiServer server(80);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
IPAddress ip(192, 168, 1, 77); //XXX sceglilo tu
IPAddress gateway(192, 168, 1, 1); //suppongo sia quello di default se no settalo tu
IPAddress subnet(255, 255, 255, 0); //Come sopra
WiFi.config(ip, gateway, subnet); //per indirizzo statico
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println(" connected");
server.begin();
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage(String toWrite)
{
String htmlPage;
htmlPage.reserve(1024); // prevent ram fragmentation
htmlPage = F("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: 5\r\n" // refresh the page automatically every 5 sec
"\r\n"
"<!DOCTYPE HTML>"
"<html>"
"<h1>Dati di oggi:</h1>"
"<p>Rilevazioni comunicate: ");
htmlPage += toWrite;
htmlPage += F("</p>"
"</html>"
"\r\n");
return htmlPage;
}
void loop()
{
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
WiFiClient client = server.available();
// wait for a client (web browser) to connect
if (client)
{
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = Serial.readString();
if (line.length() >= 1)
{
valoriRicevuti = line;
}
client.println(prepareHtmlPage(valoriRicevuti));
break;
}
}
while (client.available()) {
// but first, let client finish its request
// that's diplomatic compliance to protocols
// (and otherwise some clients may complain, like curl)
// (that is an example, prefer using a proper webserver library)
client.read();
}
// close the connection:
client.stop();
}
}
niente, non riesco proprio, ma ' possibile far comunicare arduino uno con Esp-01 tramite seriale?
ho collegato arduino rx -> tx Esp-01 e arduino tx <- Esp-01.
Arduino alimentato dal pc, Esp-01 collegato tramite un alimentatore esterno per i 3,3 volt
arduino riesce ad inviare le stringhe all Esp-01 mentre Arduino non riesce a ricevere nulla.
premetto che collegando solo arduino e utilizando il monitor seriale funziona tutto alla perfezione,
anche collegando solo Esp-01 al pc e utilizzando il monitor seriale funziona tutto alla perfezione.
Ma come si fa a inviare e ricevere stringhe tra i due ?
nothing, I just can't, but is it possible to make arduino uno communicate with Esp-01 via serial?
i connected arduino rx -> tx Esp-01 and arduino tx <- Esp-01.
Arduino powered by the pc, Esp-01 connected via an external power supply for 3.3 volts
arduino is able to send the strings to the Esp-01 while the Arduino is unable to receive anything.
I state that by connecting only Arduino and using the serial monitor everything works perfectly,
even connecting only Esp-01 to the pc and using the serial monitor everything works perfectly.
But how do you send and receive strings between the two?