Arduino Uno with SD card shield not recognizing esp8266

Hello,

I am attempting to use an Uno with an SD card shield that includes a RTCDS1307 timeclock to record data from multiple sensors, and then to send that data also to thingspeak.com over wifi. All of the devices work well separately, but when I put them together the timeclock and the esp8266-01 seem to be disrupting each other. I have tried moving the RX,TX pins between many different analog and digital pins because I found that sheild wouldn't work at all when the 8266 was plugged into A4 and A5, likely because those are also SCL and SDA pins. When I get rid of the shield codes the wifi works fine, and without the wifi codes the shield and timeclock work just fine. Does anybody know how to get these to work together or why they are conflicting? Thanks!

#include "RTClib.h"
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>

#define RX 3
#define TX 4

RTC_DS1307 rtc;
const int chipSelect = 10;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String AP = "***************";       // AP NAME
String PASS = "**********"; // AP PASSWORD
String API = "**********";   // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;

long a;

int datasend = 0;

SoftwareSerial esp8266(RX, TX);
static unsigned long wifidelay = millis( );

void setup() {

  Serial.begin(9600);


#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) 
    delay(10);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    // When time needs to be set on a new device, or after a power loss, the
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

  DateTime now = rtc.now();

  delay(3000);
}

void loop() {
  //log info to SD card
    valSensor = getSensorData();
      File dataFile = SD.open("datalog.txt", FILE_WRITE);
      if (dataFile) {
        DateTime now = rtc.now();
        String data = String (valSensor);
        String Date = String (now.year(), DEC) + "/" + String (now.month(), DEC) + "/" + String (now.day(), DEC) + ",";
        String Time = String (now.hour(), DEC) + ":" + String (now.minute(), DEC) + ":" + String (now.second(), DEC);
        dataFile.print(data);
        dataFile.print(Date);
        dataFile.println(Time);
        dataFile.close();
        datasend = 1; 
        Serial.print(data);
        Serial.print(";");
        Serial.print(Date);
        Serial.print(";");
        Serial.print(Time);
        Serial.print(";");
        delay(500);
      }
  // Send wifi data every 15 seconds if it has been launched and is working
  if ( millis( ) - wifidelay > 15000 )
  {
    if (found == true)
    {

      String getData = "GET /update?api_key=" + API + "&field1=" + String(valSensor);;
      sendCommand("AT+CIPMUX=1", 5, "OK");
      sendCommand("AT+CIPSTART=0,\"TCP\",\"" + HOST + "\"," + PORT, 15, "OK");
      sendCommand("AT+CIPSEND=0," + String(getData.length() + 4), 4, ">");
      esp8266.println(getData);

      delay(3000);
      countTrueCommand++;
      sendCommand("AT+CIPCLOSE=0", 5, "OK");
      //{
    if ( datasend == 1) {
      wifidelay = millis( );
      datasend = 0;
    }
  }
}


//sensor code to send to wifi and SD card
int getSensorData() {
  int lightsensor = analogRead(A0);
  Serial.print("light % = ");
  Serial.println(lightsensor, 1);
  return (lightsensor); // Replace with your own sensor code
}


//wifi connection status
void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while (countTimeCommand < (maxTime * 1))
  {
    esp8266.println(command);//at+cipsend
    if (esp8266.find(readReplay)) //ok
    {
      found = true;
      break;
    }

    countTimeCommand++;
  }

  if (found == true)
  {
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if (found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

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