Sketch resetting unexpectedly


i have sketch try esp8266 board arduino uno. supplied external power uno prevent lack of juice. following output on monitor serial window (showing reset , garbage characters). don´t know happening here, might problem softwareserial library? otherwise doing expecting!!!!  advice or insight appreciated.

quote
at+rst
module dosn't respond.
please reset.
init - esp8266 demo
at+rst
wifi - module ready
at+cwmode=1
at+cwjap="{myssid}","{myssid password}"
connected wifi...
temp: 24.89 c     presion: 970.86   humedad: 84%
--------------------------------1
-kr+‹�- esp8266 demo
at+rst
wifi - module ready
at+cwmode=1
at+cwjap="{myssid}","{myssid password}"
connected wifi...
temp: 24.89 c     presion: 970.86   humedad: 84%
--------------------------------1
--init - esp8266 demo
at+rst
module dosn't respond.
please reset.
code: [select]
// example adapted from
// http://zeflo.com/2014/esp8266-weather-display/
// in turn reworked from:
// http://www.seeedstudio.com/wiki/wifi_serial_transceiver_module

#include <arduinojson.h> //https://github.com/bblanchon/arduinojson
#include <softwareserial.h>

//#include <jsonparser.h> couldn't find one!!!! use arduinojson instead

#define ssid "{myssid}"
#define pass "{myssid password}"

// obtiene datos del clima
// localidad id: coatepec=3530531, xalapa=3526617
#define locationid "3530531"
//"188.226.224.148" //api.openweathermap.org
#define dst_ip "78.46.48.103"
softwareserial dbgserial(2,3);

// función para conectar wifi con la tarjeta esp8266
boolean connectwifi()
{
  serial.println("at+cwmode=1");
  string cmd="at+cwjap=\"";
  cmd+=ssid;
  cmd+="\",\"";
  cmd+=pass;
  cmd+="\"";
  dbgserial.println(cmd);
  serial.println(cmd);
  delay(2000);
  if(dbgserial.find("ok")){
    dbgserial.println("ok, connected wifi.");
    return true;
  }else{
    dbgserial.println("can not connect wifi.");
    return false;
  }
}

void setup()
{
  // put setup code here, run once:
  serial.begin(9600);
  dbgserial.settimeout(5000);
  dbgserial.begin(9600); // debuging
  serial.println("init - esp8266 demo");
 
  // prueba que el módulo esté funcionando
  serial.println("at+rst");
  // reset , test if module redy
  dbgserial.println("at+rst");
  delay(1000);
  if(dbgserial.find("ready")) {
    serial.println("wifi - module ready");
  }else{
    serial.println("module dosn't respond.");
    serial.println("please reset.");
    while(1);
  }
  delay(1000);
 
  // intenta conectar el módulo al wifi
  boolean connected=false;
  for(int i=0;i<5;i++){
    if(connectwifi()){
      connected = true;
      serial.println("connected wifi...");
      break;
    }
  }
 
  if (!connected){
    serial.println("coudn't connect wifi.");
    while(1);
  }
  delay(5000);
  dbgserial.println("at+cipmux=0"); // set single connection mode 
}

void loop()
{
  // conceta con la pagina del servicio del estado del tiempo
  string cmd = "at+cipstart=\"tcp\",\"";
  cmd += dst_ip;
  cmd += "\",80";

  // envía el comando y espera por una respuesta, si hay algún error reintenta!
  dbgserial.println(cmd);
  if(dbgserial.find("error")) return;

  cmd = "get /data/2.5/weather?id=";
  cmd += locationid;
  cmd += " http/1.0\r\nhost: api.openweathermap.org\r\n\r\n";

  // wait prompt send data
  dbgserial.print("at+cipsend=");
  dbgserial.println(cmd.length());
  if(!dbgserial.find(">"))
  {
    dbgserial.println("at+cipclose");
    serial.println("connection timeout");
    delay(1000);
    return;
  }
  dbgserial.print(cmd);

  // recorre el puerto serial hasta encontrar el segmento "main":{
  while (!dbgserial.find("\"main\":{")){}

  // lee el bloque hasta encontrar el corchete de cierre: "}"
  unsigned int = 0; //timeout counter
  int n = 1; // char counter
  char json[100]="{";
  while (i<60000)
  {
    if(dbgserial.available())
    {
      char c = dbgserial.read();
      json[n]=c;
      if(c=='}') break;
      n++;
      i=0;
    }
    i++;
  }
 
  //
  // step 1: reserve memory space
  //
  staticjsonbuffer<200> jsonbuffer;
 
  //
  // step 2: deserialize json string
  //
  jsonobject& root = jsonbuffer.parseobject(json);
 
  if (!root.success())
  {
    serial.println("parseobject() failed");
    return;
  }

  //
  // step 3: retrieve values
  //
  double temp = root["temp"];
  double presion = root["pressure"];
  byte humedad = root["humidity"];
  temp -= 273.15; // kelvin degree celsius
 
  // muestra los resultados de la consulta web
  serial.print ("temp: ");
  serial.print (temp);
  serial.print (" c ");
  serial.print ("    presion: ");
  serial.print (presion);
  serial.print ("   humedad: ");
  serial.print (humedad);
  serial.println ("%");
  serial.println ("--------------------------------1");
  delay(1000);
  serial.println ("--------------------------------2");
}

you have 3 while loops looks strange : nothing happens onthe while condition.
i have reformatted code , pointed out while loops :

code: [select]
// example adapted from
// http://zeflo.com/2014/esp8266-weather-display/
// in turn reworked from:
// http://www.seeedstudio.com/wiki/wifi_serial_transceiver_module

#include <arduinojson.h> //https://github.com/bblanchon/arduinojson
#include <softwareserial.h>

//#include <jsonparser.h> couldn't find one!!!! use arduinojson instead
#define ssid "{myssid}"
#define pass "{myssid password}"

// obtiene datos del clima
// localidad id: coatepec=3530531, xalapa=3526617
#define locationid "3530531"
//"188.226.224.148" //api.openweathermap.org
#define dst_ip "78.46.48.103"
softwareserial dbgserial(2, 3);

// función para conectar wifi con la tarjeta esp8266
boolean connectwifi()
{
  serial.println("at+cwmode=1");
  string cmd = "at+cwjap=\"";
  cmd += ssid;
  cmd += "\",\"";
  cmd += pass;
  cmd += "\"";
  dbgserial.println(cmd);
  serial.println(cmd);
  delay(2000);
  if (dbgserial.find("ok"))
  {
    dbgserial.println("ok, connected wifi.");
    return true;
  }
  else
  {
    dbgserial.println("can not connect wifi.");
    return false;
  }
}

void setup()
{
  // put setup code here, run once:
  serial.begin(9600);
  dbgserial.settimeout(5000);
  dbgserial.begin(9600); // debuging
  serial.println("init - esp8266 demo");

  // prueba que el módulo esté funcionando
  serial.println("at+rst");
  // reset , test if module ready
  dbgserial.println("at+rst");
  delay(1000);
  if (dbgserial.find("ready"))
  {
    serial.println("wifi - module ready");
  }
  else
  {
    serial.println("module dosn't respond.");
    serial.println("please reset.");
    while (1); // < ------------------------- ********************* !!!!!!!!!!!!!!!!!!!! what, empty while loop ?
  }
  delay(1000);

  // intenta conectar el módulo al wifi
  boolean connected = false;
  (int = 0; < 5; i++)
  {
    if (connectwifi())
    {
      connected = true;
      serial.println("connected wifi...");
      break;
    }
  }

  if (!connected)
  {
    serial.println("coudn't connect wifi.");
    while (1); // < ------------------------- ********************* !!!!!!!!!!!!!!!!!!!! what, empty while loop ?
  }
  delay(5000);
  dbgserial.println("at+cipmux=0"); // set single connection mode
}

void loop()
{
  // conceta con la pagina del servicio del estado del tiempo
  string cmd = "at+cipstart=\"tcp\",\"";
  cmd += dst_ip;
  cmd += "\",80";

  // envía el comando y espera por una respuesta, si hay algún error reintenta!
  dbgserial.println(cmd);
  if (dbgserial.find("error")) return;

  cmd = "get /data/2.5/weather?id=";
  cmd += locationid;
  cmd += " http/1.0\r\nhost: api.openweathermap.org\r\n\r\n";

  // wait prompt send data
  dbgserial.print("at+cipsend=");
  dbgserial.println(cmd.length());
  if (!dbgserial.find(">"))
  {
    dbgserial.println("at+cipclose");
    serial.println("connection timeout");
    delay(1000);
    return;
  }
  dbgserial.print(cmd);

  // recorre el puerto serial hasta encontrar el segmento "main":{
  while (!dbgserial.find("\"main\":{")) {} // < ------------------------- ********************* what, empty while loop ?

  // lee el bloque hasta encontrar el corchete de cierre: "}"
  unsigned int = 0; //timeout counter
  int n = 1; // char counter
  char json[100] = "{";
 
  while (i < 60000) // < ------------------------- ********************* correct while structure
  {
    if (dbgserial.available())
    {
      char c = dbgserial.read();
      json[n] = c;
      if (c == '}') break;
      n++;
      = 0;
    }
    i++;
  }

  //
  // step 1: reserve memory space
  //
  staticjsonbuffer<200> jsonbuffer;

  //
  // step 2: deserialize json string
  //
  jsonobject& root = jsonbuffer.parseobject(json);

  if (!root.success())
  {
    serial.println("parseobject() failed");
    return;
  }

  //
  // step 3: retrieve values
  //
  double temp = root["temp"];
  double presion = root["pressure"];
  byte humedad = root["humidity"];
  temp -= 273.15; // kelvin degree celsius

  // muestra los resultados de la consulta web
  serial.print ("temp: ");
  serial.print (temp);
  serial.print (" c ");
  serial.print ("    presion: ");
  serial.print (presion);
  serial.print ("   humedad: ");
  serial.print (humedad);
  serial.println ("%");
  serial.println ("--------------------------------1");
  delay(1000);
  serial.println ("--------------------------------2");
}


Arduino Forum > Using Arduino > Programming Questions > Sketch resetting unexpectedly


arduino

Comments