Go Down

Topic: Sparkfun esp8266 WiFi shield “locks up?” (Read 143 times) previous topic - next topic

jstamp

Hello all,
I'm having trouble with my sparkfun WiFi shield. When I first connect it everything seems to work- I use both a custom program and the example shield demo from the sparkfun library. Then, after a couple of iterations of adjusting code (things like changing a delay from 500 to 1000- not big changes) the shield appears to get stuck. It's still connected to my WiFi (I use my phone as a hotspot and I can see the number of devices connected) and the blue light is solid. Even uploading a blank sketch doesn't seem to disconnect. I also tried to change the WiFi ssid and password to something "wrong" to force it to be unable to connect but no luck. I've tried power cycling the arduino but when it comes back on the shield still hooks up to my WiFi (even if the sketch no longer has the right id/password) but won't respond to commands from the arduino (the demo sketch gives me "no esp8266 present " when I try to run it).
I thought maybe I had cooked one of these, so I bought a second one. Soldered the headers, and it worked great for a while then got stuck again.
Even going back to the unaltered demo code (all I do is change ssid and password to match my device) doesn't work. Still can't get the board to talk to the shield.
I'm guessing it's something to do with uploading a sketch while it's connected or some similar "order of operations " problem and I'm hoping someone knows the secret to getting back in touch with the shield (stick your tongue out while uploading? Only upload during a full moon?)
I can post code if necessary, but because it's the demo and it works then it doesn't I'm guessing it's jot the code.

pert

Here's information on the shield for anyone not familiar with it:
https://learn.sparkfun.com/tutorials/esp8266-wifi-shield-hookup-guide/all

It's still connected to my WiFi (I use my phone as a hotspot and I can see the number of devices connected) and the blue light is solid. Even uploading a blank sketch doesn't seem to disconnect.
The ESP8266 stores your WiFi configuration to non-volatile memory so this is a common source of confusion.


I also tried to change the WiFi ssid and password to something "wrong" to force it to be unable to connect but no luck. I've tried power cycling the arduino but when it comes back on the shield still hooks up to my WiFi (even if the sketch no longer has the right id/password) but won't respond to commands from the arduino (the demo sketch gives me "no esp8266 present " when I try to run it).
The reason why changing the SSID and password isn't having the expected result is probably because the communication between the Arduino and the ESP8266 is failing.

I'm guessing it's something to do with uploading a sketch while it's connected
The only way that would be a problem is if you have the switch set to the "HW" position. The reason is that the Uno and Mega also use the same pins used for the HW connection to the ESP8266 for uploading. In the "HW" position, you can also have problems when you use Serial.print(), etc. in your sketch (or a library you're using does it) because the serial communication between the Arduino board and your computer on pins 0 and 1 can interfere with the communication between the Arduino and the ESP8266 on the same pins.


Please post your full sketch. If possible, you should always post code directly in the forum thread as text using code tags (</> button on the toolbar). This will make it easy for anyone to look at it, which will increase the likelihood of you getting help. If the sketch is longer than the forum will allow then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

Please always do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.


Please tell us which setting you have the switch on the shield set to.

jstamp

#2
Jan 18, 2019, 09:47 pm Last Edit: Jan 18, 2019, 10:09 pm by jstamp Reason: Add URL to library
I did leave the switch at SW (I was aware of the issue).

Here's my code:  It works sometimes, doesn't work other times (if you're confused about the purpose, right now I'm building the chunk of code that posts numbers.  I'm eventually going to add routines to grab that data from sensors so that I'm posting actual changing data, but I don't want to do that until I can reliably post).

What's funny about this code is it only works SOMETIMES- I turned the device on and it took right off this afternoon.  Didn't work yesterday afternoon, no change and it worked for a while today.  Now it doesn't work (I'm getting "error connecting to the server" so I know it's making it to the "postData" function before failing.

Code: [Select]

#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>


// WiFi Network Definitions
const char mySSID[] = "Josh";
const char myPSK[] = "12345678";

const boolean useWifi=true;

const char destServer[] = "api.pushingbox.com";

const String startHttpRequest = "GET /pushingbox?devid=v2CA84BF46480EF0";

const String endHttpRequest = " HTTP/1.1\n"
                           "Host: api.pushingbox.com\n"
                           "Connection: close\n\n";
const String labelStatus= "&status=";
const String labelTemp= "&temp=";
const String labelVac="&vacuum=";
const String labelTank="&tank=";

unsigned long timeNow = 0;
long period = 60000;
int minuteCounter=0;


ESP8266Server server = ESP8266Server(80);

const String htmlHeader = "HTTP/1.1 200 OK\r\n"
                          "Content-Type: text/html\r\n"
                          "Connection: close\r\n\r\n"
                          "<!DOCTYPE HTML>\r\n"
                          "<html>\r\n";

                          
void setup()
{

  Serial.begin(9600);
 if (useWifi) {
  Serial.println("Using Wifi");
  initializeESP8266();
  connectESP8266();
  displayConnectInfo();
 }
 else
  Serial.println("Wifi disabled for debugging");
  int temp=37;
  int vacuum=22;
  int tank=0;
  postData("Startup", temp, vacuum, tank);


  

}

void loop()
{

if (millis() > timeNow + period) {
  timeNow=millis();
  Serial.print("Minute Counter= ");
  Serial.println(minuteCounter);
  
  if (minuteCounter >13) {
    int temp=35;
    int vacuum=26;
    int tank=0;
    postData("Periodic", temp, vacuum, tank);
    minuteCounter =0;
  }
  else {
    minuteCounter=minuteCounter+1;
  }
}
  
}

void initializeESP8266()
{
  // esp8266.begin() verifies that the ESP8266 is operational
  // and sets it up for the rest of the sketch.
  // It returns either true or false -- indicating whether
  // communication was successul or not.
  // true
  int test = esp8266.begin();
  if (test != true)
  {
    Serial.println(F("Error talking to ESP8266."));
    errorLoop(test);
  }
  Serial.println(F("ESP8266 Shield Present"));
}

void connectESP8266()
{
  // The ESP8266 can be set to one of three modes:
  //  1 - ESP8266_MODE_STA - Station only
  //  2 - ESP8266_MODE_AP - Access point only
  //  3 - ESP8266_MODE_STAAP - Station/AP combo
  // Use esp8266.getMode() to check which mode it's in:
  int retVal = esp8266.getMode();
  if (retVal != ESP8266_MODE_STA)
  { // If it's not in station mode.
    // Use esp8266.setMode([mode]) to set it to a specified
    // mode.
    retVal = esp8266.setMode(ESP8266_MODE_STA);
    if (retVal < 0)
    {
      Serial.println(F("Error setting mode."));
      errorLoop(retVal);
    }
  }
  Serial.println(F("Mode set to station"));

  // esp8266.status() indicates the ESP8266's WiFi connect
  // status.
  // A return value of 1 indicates the device is already
  // connected. 0 indicates disconnected. (Negative values
  // equate to communication errors.)
  retVal = esp8266.status();
  if (retVal <= 0)
  {
    Serial.print(F("Connecting to "));
    Serial.println(mySSID);
    // esp8266.connect([ssid], [psk]) connects the ESP8266
    // to a network.
    // On success the connect function returns a value >0
    // On fail, the function will either return:
    //  -1: TIMEOUT - The library has a set 30s timeout
    //  -3: FAIL - Couldn't connect to network.
    retVal = esp8266.connect(mySSID, myPSK);
    if (retVal < 0)
    {
      Serial.println(F("Error connecting"));
      errorLoop(retVal);
    }
  }
}

void displayConnectInfo()
{
  char connectedSSID[24];
  memset(connectedSSID, 0, 24);
  // esp8266.getAP() can be used to check which AP the
  // ESP8266 is connected to. It returns an error code.
  // The connected AP is returned by reference as a parameter.
  int retVal = esp8266.getAP(connectedSSID);
  if (retVal > 0)
  {
    Serial.print(F("Connected to: "));
    Serial.println(connectedSSID);
    Serial.println();
  }

  // esp8266.localIP returns an IPAddress variable with the
  // ESP8266's current local IP address.
  IPAddress myIP = esp8266.localIP();
  Serial.print(F("My IP: ")); Serial.println(myIP);
}

void postData(String myStatus, int temp, int vacuum, int tank)
{
  // To use the ESP8266 as a TCP client, use the
  // ESP8266Client class. First, create an object:

  ESP8266Client client;
 if (useWifi){
  // ESP8266Client connect([server], [port]) is used to
  // connect to a server (const char * or IPAddress) on
  // a specified port.
  // Returns: 1 on success, 2 on already connected,
  // negative on fail (-1=TIMEOUT, -3=FAIL).
  int retVal = client.connect(destServer, 80);
  if (retVal <= 0)
  {
    Serial.println(F("Failed to connect to server."));
    return;
  }
 }

  // print and write can be used to send data to a connected
  // client connection.



String httpRequest=startHttpRequest+labelStatus+myStatus+labelTemp+temp+labelVac+vacuum+labelTank+tank+endHttpRequest;
  
  Serial.println(httpRequest);
  Serial.println("Number of Characters: ");
  
  if (useWifi) {
  Serial.print(client.available());
  client.print(httpRequest);

  // available() will return the number of characters
  // currently in the receive buffer.
  while (client.available())
    Serial.write(client.read()); // read() gets the FIFO char
  
  // connected() is a boolean return value - 1 if the
  // connection is active, 0 if it's closed.
  if (client.connected())
    client.stop(); // stop() closes a TCP connection.
  }
}



// errorLoop prints an error code, then loops forever.
void errorLoop(int error)
{
  Serial.print(F("Error: ")); Serial.println(error);
  Serial.println(F("Looping forever."));
  for (;;)
    ;
}


The library I'm using came from the makers of the shield: https://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library/blob/master/src/SparkFunESP8266WiFi.h

Go Up