-->
Page 1 of 1

Am I interfacing with Arduino correctly here?

PostPosted: Sat Mar 12, 2016 11:06 pm
by polishdude20
I've got an ESP8266-07 model (07 because it has support for the external antenna) hooked up to my Arduino. The ESP is on it's own breakout board which includes logic level shifters and a voltage regulator. So I've basically got four connections to my ESP other than all the GPIO pins: TX,RX,Vin, and GND.

My program basically sends over temperature and moisture data to Thingspeak every minute or so. The way I'm going about this is with the use of the SoftwareSerial library. I've pasted my wifiSend function at the end of this post.
Basically I send over a command using softSerial.print (softSerial is the name I gave my softwareSerial function when I "begin" it). After I send the command, I also print the same command to the serial monitor (for debugging purposes). After that, I have an if statement that goes: if(softSerial.find(okKey) then print a confirmation message in the serial monitor. My okKey is "OK".

Now, my problem is that sometimes, the commands just fail. If it doesn't find "OK" the function then tries to retry the process of sending the data and finding "OK"again. I'm thinking that it fails sometimes because the find method might just not be timed up with the data I'm receiving from the ESP. I'm not sure how "find" works in detail and I'm not sure if this is the correct way to communicate with the ESP. I've even tried putting in a delay of 500 ms after sending a command to give the ESP some time to send back OK but that doesn't seem to help at all. Is there something I'm missing or a better way of doing this?


Code for my wifiSend function to send data:

Code: Select allint wifiSend(){
  int tries = 0;
  int sendTries = 0;

 
 
  //All the next lines print the various commands to send data. If there is no response back, the while loop restarts and
  //a message is printed in the serial monitor stating what failed.
  while (true){

    if (tries == 3) {
     Serial.println("TOO MANY TRIES TO SEND, RESETTING UNIT");
    return 0;
      }
   
    tries ++;

   
    Serial.print(">>>>>>>>>>>>SENDING DATA TRY:");
    Serial.println(tries);

    float temp = measureTemp();
    float light = measureLight();
    float moist = SoilMoisture();

   

    Serial.println(startTCPKey);
    softSerial.print(startTCPKey);
    delay(500);

      if (softSerial.find(okKey)){
        Serial.println("TCP CONNECTED OK");
        Serial.println(dataLenKey);
        softSerial.print(dataLenKey);
        delay(500);
     
        if (softSerial.find(okKey)){
          Serial.println("CIPSEND RECEIVED OK");
          Serial.print(getUpdateKey);
          Serial.print(apiKey);
          Serial.print(field1Key);
          Serial.print(temp);
          Serial.print(field2Key);
          Serial.println(moist);
          softSerial.print(getUpdateKey);
          softSerial.print(apiKey);
          softSerial.print(field1Key);
          softSerial.print(temp);
          softSerial.print(field2Key);
          softSerial.println(moist);

          delay(500);

          while (softSerial.find(sendOkKey) != 1){
            sendTries = 1;

            if (sendTries == 20){
              Serial.print("FAILED TO SEND DATA, 20 TRIES");
              break;
            }
           
            softSerial.println();
            delay(200);
          }

           
              Serial.println("DATA SEND OK");
              softSerial.print("AT+CIPCLOSE");
              //Serial.println("TURNING OFF ESP");
              delay(200);
             // digitalWrite(espEnablePin,LOW); //Turns off power to ESP
              break;
           

   
   
   }
   else{
            Serial.println("CIPSEND FAILED RETRYING...");
            continue;
            }
 
  }
  else{
          Serial.println("TCP FAILED TO CONNECT RETRYING...");
          continue;
        }
 
    }

return 1;
 
}



Main code where all my stuff is defined:

Code: Select all//Program that utilizes the ESP8266-12 Wifi board to send temperature data to thingspeak
//Lots of the code is mainly for serial debugging to check for errors and fine tune the delays
//Lots of the .find methods are time sensitive so delays might need to be tweaked if there are problems

#include <SoftwareSerial.h>
#include "LowPower.h"

//Various ESP8266 commands, these might change for different firmwares/versions of the board.
#define atKey "AT\r\n"
#define setModeKey "AT+CWMODE=1\r\n"
#define connectWifiKey "AT+CWJAP=\"LanBeforeTime\",\"adamadam\"\r\n" //first quotes: SSID name, second quotes: SSID password
#define okKey "OK"
#define setCMKey "AT+CIPMUX=0\r\n"
#define startTCPKey "AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n"
#define dataLenKey "AT+CIPSEND=80\r\n" //change the number to the length of the data you're sending or more.
#define getUpdateKey "GET /update?key=" //first part "get" of keyword
#define apiKey "7SIJZ63RD612UQZG" //write api key from thingspeak
#define field1Key "&field1=" //change field number as needed or add more.
#define field2Key "&field2="
#define field3Key "&field3="
#define field4Key "&field4="
#define sendOkKey "SEND OK"
#define atCloseKey "AT+CIPCLOSE"


#define moisture_input 2
#define divider_top 2
#define divider_bottom 3



//Use either of these variables in the sleep() function. To use minutes, convert to seconds.
const int minutesToSleep = 15;
const int secondsToSleep = 10;

SoftwareSerial softSerial(10, 11); //RX , TX ,connect TX of esp8266 to RX of arduino and vice-versa

int espEnablePin = 4; //Pin connected to NPN transistor to turn on and off ESP during sleep
int lm35Pin = 0; //Pin for temperature probe
int photoPin = 1; //Pin for photo resistor

void setup() {

  pinMode(espEnablePin,OUTPUT);
  Serial.begin(9600);
  softSerial.begin(9600);

}

void loop() {

  resetESP(); //turns ESP off and back on to reset it
  wifiSetup(); //sets modes and connects to network
 
  while (wifiSend() == 1){   //measures temperature and send data to thingspeak, if wifiSend succeeds, return 1 and go to sleep
    //sleep(); //puts Arduino into power down sleep mode, draws about 19mA
    delay(60000);
   
  }


 
}













You can see i've included the low power library but at this point in time I'm not using it.