-->
Page 1 of 1

Esp8266 01 message to arduino uno

PostPosted: Wed Aug 10, 2016 2:23 am
by elpejo
hi everyone, sorry if my english is too bad.
I have this setup code, in my ESP8266 - 01, it saves my WIFI config in the EEPROM.
This ESP8266 it's connected to arduino one. And i want to send messages with the url of the IP of my ESP8266, but after config my SSID and password WiFi i can't. Can anyone help me?
Pd: i want to send pin13 for example, to activate the pin13 of arduino.

Thanks if anyone can help me. i am a newbe, and this is for my university tesis

ESP8266 - 01 web ssid - pass config code:
Code: Select all
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>

String pral = "<html>"
"<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>"
"<title>WIFI CONFIG</title> <style type='text/css'> body,td,th { color: #036; } body { background-color: #999; } </style> </head>"
"<body> "
"<h1>WIFI CONF</h1><br>"
"<form action='config' method='get' target='pantalla'>"
"<fieldset align='left' style='border-style:solid; border-color:#336666; width:200px; height:180px; padding:10px; margin: 5px;'>"
"<legend><strong>Configurar WI-FI</strong></legend>"
"SSID: <br> <input name='ssid' type='text' size='15'/> <br><br>"
"PASSWORD: <br> <input name='pass' type='password' size='15'/> <br><br>"
"<input type='submit' value='Comprobar conexion' />"
"</fieldset>"
"</form>"
"<iframe id='pantalla' name='pantalla' src='' width=900px height=400px frameborder='0' scrolling='no'></iframe>"
"</body>"
"</html>";

ESP8266WebServer server(80);

char ssid[20];
char pass[20];
String ssid_leido;
String pass_leido;
int ssid_tamano = 0;
int pass_tamano = 0;

String arregla_simbolos(String a) {
 a.replace("%C3%A1", "á");
 a.replace("%C3%A9", "é");
 a.replace("%C3%A", "i");
 a.replace("%C3%B3", "ó");
 a.replace("%C3%BA", "ú");
 a.replace("%21", "!");
 a.replace("%23", "#");
 a.replace("%24", "$");
 a.replace("%25", "%");
 a.replace("%26", "&");
 a.replace("%27", "/");
 a.replace("%28", "(");
 a.replace("%29", ")");
 a.replace("%3D", "=");
 a.replace("%3F", "?");
 a.replace("%27", "'");
 a.replace("%C2%BF", "¿");
 a.replace("%C2%A1", "¡");
 a.replace("%C3%B1", "ñ");
 a.replace("%C3%91", "Ñ");
 a.replace("+", " ");
 a.replace("%2B", "+");
 a.replace("%22", "\"");
 return a;
}

//**** CONFIGURACION WIFI  *******
void wifi_conf() {
 int cuenta = 0;

 String getssid = server.arg("ssid"); //Recibimos los valores que envia por GET el formulario web
 String getpass = server.arg("pass");

 getssid = arregla_simbolos(getssid); //Reemplazamos los simbolos que aparecen cun UTF8 por el simbolo correcto
 getpass = arregla_simbolos(getpass);

 ssid_tamano = getssid.length() + 1;  //Calculamos la cantidad de caracteres que tiene el ssid y la clave
 pass_tamano = getpass.length() + 1;

 getssid.toCharArray(ssid, ssid_tamano); //Transformamos el string en un char array ya que es lo que nos pide WIFI.begin()
 getpass.toCharArray(pass, pass_tamano);

 Serial.println(ssid);     //para depuracion
 Serial.println(pass);

 WiFi.begin(ssid, pass);     //Intentamos conectar
 while (WiFi.status() != WL_CONNECTED)
 {
  delay(500);
  Serial.print(".");
  cuenta++;
  if (cuenta > 20) {
   graba(70, "noconfigurado");
   server.send(200, "text/html", String("<h2>No se pudo realizar la conexion<br>no se guardaron los datos.</h2>"));
   return;
  }
 }
 Serial.print(WiFi.localIP());
 graba(70, "configurado");
 graba(1, getssid);
 graba(30, getpass);
 server.send(200, "text/html", String("<h2>Conexion exitosa a: "
  + getssid + "<br> El pass ingresado es: " + getpass + "<br>Datos correctamente guardados."));

}


//*******  G R A B A R  EN LA  E E P R O M  ***********
void graba(int addr, String a) {
 int tamano = (a.length() + 1);
 Serial.print(tamano);
 char inchar[30];    //'30' Tamaño maximo del string
 a.toCharArray(inchar, tamano);
 EEPROM.write(addr, tamano);
 for (int i = 0; i < tamano; i++) {
  addr++;
  EEPROM.write(addr, inchar[i]);
 }
 EEPROM.commit();
}

//*******  L E E R   EN LA  E E P R O M    **************
String lee(int addr) {
 String nuevoString;
 int valor;
 int tamano = EEPROM.read(addr);
 for (int i = 0;i < tamano; i++) {
  addr++;
  valor = EEPROM.read(addr);
  nuevoString += (char)valor;
 }
 return nuevoString;
}

//*********  INTENTO DE CONEXION   *********************
void intento_conexion() {
 if (lee(70).equals("configurado")) {
  ssid_leido = lee(1);      //leemos ssid y password
  pass_leido = lee(30);

  Serial.println(ssid_leido);  //Para depuracion
  Serial.println(pass_leido);

  ssid_tamano = ssid_leido.length() + 1;  //Calculamos la cantidad de caracteres que tiene el ssid y la clave
  pass_tamano = pass_leido.length() + 1;

  ssid_leido.toCharArray(ssid, ssid_tamano); //Transf. el String en un char array ya que es lo que nos pide WiFi.begin()
  pass_leido.toCharArray(pass, pass_tamano);

  int cuenta = 0;
  WiFi.begin(ssid, pass);      //Intentamos conectar
  while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   cuenta++;
   if (cuenta > 20) {
    Serial.println("Fallo al conectar");
    return;
   }
  }
 }
 if (WiFi.status() == WL_CONNECTED) {
  Serial.print("Conexion exitosa a: ");
  Serial.println(ssid);
  Serial.println(WiFi.localIP());
 }
}


//*****  S E T U P  **************
void setup() {
 Serial.begin(115200);
 EEPROM.begin(4096);
 WiFi.softAP("xxxxxxx");      //Nombre que se mostrara en las redes wifi

 server.on("/", []() {
  server.send(200, "text/html", pral);
 });
 server.on("/config", wifi_conf);
 server.begin();
 Serial.println("Webserver iniciado...");

 Serial.println(lee(70));
 Serial.println(lee(1));
 Serial.println(lee(30));
 intento_conexion();
}


//*****   L O O P   **************
void loop() {
 server.handleClient();
 delay(2000);
}




Arduino Pin Code:
Code: Select all#include <SoftwareSerial.h>
 
#define DEBUG true
 
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(9600);
  esp8266.begin(9600); // your esp's baud rate might be different
 
  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);
 
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
 
  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
}
 
void loop()
{
  if(esp8266.available()) // check if the esp is sending a message
  {
 
   
    if(esp8266.find("+IPD,"))
    {
     delay(1000); // wait for the serial buffer to fill up (read all the serial data)
     // get the connection id so that we can then disconnect
     int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
         
     esp8266.find("pin="); // advance cursor to "pin="
     
     int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
     pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
     
     digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin   
     
     // make close command
     String closeCommand = "AT+CIPCLOSE=";
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     
     sendData(closeCommand,1000,DEBUG); // close connection
    }
  }
}
 
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
   
    esp8266.print(command); // send the read character to the esp8266
   
    long int time = millis();
   
    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
       
        // The esp has data so display its output to the serial window
        char c = esp8266.read(); // read the next character.
        response+=c;
      } 
    }
   
    if(debug)
    {
      Serial.print(response);
    }
   
    return response;
}


Re: Esp8266 01 message to arduino uno

PostPosted: Wed Aug 10, 2016 11:26 am
by martinayotte
You seems to have reprogrammed the ESP with a custom Arduino sketch and on Arduino side you seems to still using AT commands.

When uploading a Arduino sketch, the original AT Firmware is erase and overwritten by the new sketch, so the ESP won't respond to AT commands anymore.

You will have to define your own new protocol for communication between Arduino and ESP, and implement new codes on both sides.

Re: Esp8266 01 message to arduino uno

PostPosted: Wed Aug 10, 2016 12:42 pm
by elpejo
martinayotte wrote:You seems to have reprogrammed the ESP with a custom Arduino sketch and on Arduino side you seems to still using AT commands.

When uploading a Arduino sketch, the original AT Firmware is erase and overwritten by the new sketch, so the ESP won't respond to AT commands anymore.

You will have to define your own new protocol for communication between Arduino and ESP, and implement new codes on both sides.
martinayotte wrote:You seems to have reprogrammed the ESP with a custom Arduino sketch and on Arduino side you seems to still using AT commands.

When uploading a Arduino sketch, the original AT Firmware is erase and overwritten by the new sketch, so the ESP won't respond to AT commands anymore.

You will have to define your own new protocol for communication between Arduino and ESP, and implement new codes on both sides.



Thanks a lot, now i understand what is happening. I'll search in interntet how i can define a new protocol of communication. If you havo some example o anyone have some example, I'll be very grateful