Trying to print data through Serial and connecting to WiFi

Hi, I'm trying to Serial.print() data from the TFRobot Mini after connecting to the internet. My intention is to send it to MongoDB later. I'm using a Sparkfun RedBoard and WiFi ESP8266 Shield. Most of the shield code is from an example that comes with the library. Here's the code:

/************************************************************
ESP8266_Ping.h
SparkFun ESP8266 AT library - Ping Demo
Jim Lindblom @ SparkFun Electronics
Original Creation Date: July 16, 2015
https://github.com/sparkfun/SparkFun_ESP8266_AT_Arduino_Library

This example pings a destination server over and over and over.
It'll print the response time (in ms) to the serial monitor.

Development environment specifics:
  IDE: Arduino 1.6.5
  Hardware Platform: Arduino Uno
  ESP8266 WiFi Shield Version: 1.0

This code is released under the MIT license.

Distributed as-is; no warranty is given.
************************************************************/

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

// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "Xf123456";
const char myPSK[] = "11050927";
SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;

void setup(){
    Serial.begin(115200);
    TFmini.begin(mySerial);
  while (esp8266.begin() != true)
  {
    Serial.print("Error connecting to ESP8266.");
	delay(1000);
  }
  
  if (esp8266.status() <= 0)
  {
    Serial.print("Connected to Internet");
  }
  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);
  }

  // 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);

  if(TFmini.measure()){                      //Measure Distance and get signal strength
          distance = TFmini.getDistance();       //Get distance data
          strength = TFmini.getStrength();       //Get signal strength data
          Serial.print("Distance = ");
          Serial.print(distance);
          Serial.println("cm");
          //Serial.print("Strength = ");
          //Serial.println(strength);
          delay(100);
  delay(1000);
  }
}
void loop() {

  
  
}

What's the problem?

It won't print the distance data through the serial monitor. I'm not even sure its collecting the data.

what Arduino are you using - make sure that the pins you are using are supported by SoftwareSerial

try running the simple SoftwareSerialExample which reads characters from the serial line and displays on the serial monitor - this will check that you are receiving data (change Rx and Tx pins to suit your system).

Edit: note your Serial is running at 115200baud - I have found that softwareserial can corrupt Serial data at this baud rate

I'm using a RedBoard like I already stated. The pins are supported, I've tried to only use the sensor and it prints out everything through the monitor perfectly. When I try and implement the code for connecting to the wifi is when it fails.

Edit: When only running the sensor code the baud rate is at 115200 as well as the wifi connection code. The DFRobot uses pins 13 and 12 not 0 and 1

can you upload the code which worked and displayed data to the seerial monitor>
the following code is only executed once in setup()

if(TFmini.measure()){                      //Measure Distance and get signal strength
.....
}

should it be in the loop() function to be execute every time TFmini.measure() returns true?

Of course:

#include <DFRobot_TFmini.h>
#include <SoftwareSerial.h> 


SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;

void setup(){
    Serial.begin(115200);
    TFmini.begin(mySerial);

}
void loop(){
    if(TFmini.measure()){                      //Measure Distance and get signal strength
        distance = TFmini.getDistance();       //Get distance data
        strength = TFmini.getStrength();       //Get signal strength data
        Serial.print("Distance = ");
        Serial.print(distance);
        Serial.println("cm");
        //Serial.print("Strength = ");
        //Serial.println(strength);
        delay(100);
    }
    delay(100);
}

I'll try to put it in void loop()

It doesn't work with the distance sensor code inside void loop()

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

// Replace these two character strings with the name and
// password of your WiFi network.
const char mySSID[] = "Xf123456";
const char myPSK[] = "11050927";
SoftwareSerial mySerial(12, 13); // RX, TX

DFRobot_TFmini  TFmini;
uint16_t distance,strength;

void setup(){
    Serial.begin(115200);
    TFmini.begin(mySerial);
  while (esp8266.begin() != true)
  {
    Serial.print("Error connecting to ESP8266.");
	delay(1000);
  }
  
  if (esp8266.status() <= 0)
  {
    Serial.print("Connected to Internet");
  }
  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);
  }

  // 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 loop() {

  if(TFmini.measure()){                      //Measure Distance and get signal strength
          distance = TFmini.getDistance();       //Get distance data
          strength = TFmini.getStrength();       //Get signal strength data
          Serial.print("Distance = ");
          Serial.print(distance);
          Serial.println("cm");
          //Serial.print("Strength = ");
          //Serial.println(strength);
          delay(100);
  delay(1000);
  }
  
}

the original code of post #7 displays the distance data OK but when you add the code to support the ESP8266 as in post #8 distance is not displayed? does code of post #8 display the connection to the internet is OK?
It looks like the execution of the ESP8266 code is corrupting the distance data being read by SoftwareSerial. SoftwareSerial uses interrupts to detect the signal level changes in received serial data. It is very time critical and it is possible that the ESP8266 code is corrupting the timing and hence received data.
To confirm try commenting out the ESP8266 code in post #8 and running it - the distance should then be displayed.

what WiFi ESP8266 Shield are you using ? how is it connected to the RedBoard?

I would suggest trying the code of post #8 on an Arduino which supports hardware serial ports such as the Arduino Mega

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.