Arduino Mega and Wifi 8266 shield issue

I have a mega and mounted the shield on the mega and it doesn't work. I mounted the same shield on an UNO and it works great.

I don't believe it is the code as i have been through it many times, and it is basically the same as the UNO. So i think the mega must be using a pin the shield uses.

This is really wearing me out. I have worked on this for 3 days. Initially it was a conflict with the wifi shield and the LCD using pins 11 and 12 and maybe 7. The wifi (on the UNO) would work great when the lcd was not connected. So i purchased the mega thinking my problems would be over as i have more pins and could relocate them. Now the wifi won't work on the mega and there are no pins connected to it.

libraries i am using:
#include <LiquidCrystal.h>

#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>

Any suggestions?

Thanks in advance.

Jim

Read the documentation ESP8266 WiFi Shield Hookup Guide - SparkFun Learn

I have read it... Guess I am just missing something..and I am not using any pins on the wifi shield.. I am new to arduino so I guess I am just missing something.. Againg, the shield works great on the uno.

You can't use D9 for software serial RX on a Mega. It works on the Uno. If you can select the RX/TX lines for the SoftwareSerial.begin() function call, jumper D9 to D11. It should work there.

Hi SurferTom,

Thanks for the response, but I am a bit too new to arduino to completely understand your answer. I know I can assign pins but other than the code below I am not setting any pins.

LiquidCrystal lcd(30, 32, 34, 36, 38, 40);

Do you mean set a pin in the wifi library?

Jim

The wifi library for the ESP8266 uses software serial. The Mega can't use certain pins for the RX line. See the Limitation section here.

edit: Do you see the UART switch on the shield? Is it set to SW? If so, it expects to use software serial and pins D8 and D9. The Mega can't use either of those pins for the RX line.

Ahh.. yes, it is set to SW or pin 8 and 9... i switched it to HW .. but it didn't seem to help. I wonder if the library is selecting a pin and that is the issue.

maybe something like this is the answer. I reassign the rx and tx pins?

The SparkFun library expects software serial on D8 and D9. This is defined in SparkFunESP8266WiFi.h.

/////////////////////
// Pin Definitions //
/////////////////////
#define ESP8266_SW_RX 9 // ESP8266 UART0 RXI goes to Arduino pin 9
#define ESP8266_SW_TX 8 // ESP8266 UART0 TXO goes to Arduino pin 8

// and a few lines later
static SoftwareSerial swSerial(ESP8266_SW_TX, ESP8266_SW_RX);

The mega can't use D9 for the RX pin! You must change that define for the RX pin and jumper the D9 pin to that new pin. I used D11.

edit: I might have those backwards. It may be D8 is the RX pin on the Arduino (TX on the shield).

Hi surferTim, really appreciate you effort to help on this. I have spent countless hours on it. And I feel i am getting pretty close at this point, because of your help.

Changing the pins on the "..wifi.h" file didn't help. I tried various combinations of pins but no success. But i think it should have, so i have to believe maybe there is something wrong with the code or something I am not seeing. I tried the switch on the wifi shield at SW and HW.. The HW seemed to bring up a lot of errors about failure to communicate with programmer.

BTW, everything else, works.. ie the LCD, temperature and Barometric Pressure, and the voltmeter on the 9v battery i have that i am using in place of a 12v battery. I do have a voltage divider on the breadboard, so i am not taking in too many volts.

At one time i tested it on the uno with everything hooked up but the LCD and it worked. so, i have to think there might be still something going on with the LCD and the WIFI.

I have 'xxx' out the wifi and passwords.. etc.. for privacy. they are filled in correctly in actual code.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h> 
#include <SparkFunESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>

//////////////////////////////
// WiFi Network Definitions //
///////////////////////////

const char mySSID[] = "xxxxx";
const char myPSK[] = "xxxxx";
const char* myserver = "www.xxxxx.com";


// Global variables
unsigned long startTime;
float onehour;  //holds the value of pressure
unsigned long lastonehour; //tracks the millis since updated
float onehourvalue;  //holds the value of pressure
unsigned long fourhour; 
unsigned long eighthour; 
unsigned long twofourhour; 
unsigned long mytime;
float currentpressure;
boolean gotonetwork = false;
// vars for battery readings
int analogInput = 0;
float vout = 0.0;
float vin = 0.0;
float R1 = 220;
float R2 = 10;
int value = 0;
// end vars for batt readings
// end global vars

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(30, 32, 34, 36, 38, 40);
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

// wifi functions  --------------------------------------------------
void connectESP8266(){
   Serial.println("connect called");
   int retVal = esp8266.status();
  if (retVal <= 0)  {
   Serial.print("Connecting to ");
   Serial.println(mySSID);
   retVal = esp8266.connect(mySSID, myPSK);
   Serial.println("Error connecting - trying again!");
   Serial.println(retVal);
   }
   if (retVal > 0){
      gotonetwork=true;
      displayConnectInfo();
   }
}

void displayConnectInfo(){
  char connectedSSID[24];
  memset(connectedSSID, 0, 24);
  int retVal = esp8266.getAP(connectedSSID);
  if (retVal > 0) {
    Serial.print("Connected to: ");
    Serial.println(connectedSSID);
    IPAddress myIP = esp8266.localIP();
    Serial.print("My IP: "); 
    Serial.println(myIP);
    delay(2000);
    clientDemo();
  }
}

void clientDemo(){
  ESP8266Client client;
  int retVal = client.connect(myserver, 80);
  delay(4000);
  if (retVal <= 0)  {
    Serial.println("Failed to connect to server.");
    //return;
  }
  if (retVal > 0) {
    Serial.println("Connected to server.");
  }
  
  Serial.println("Making HTTP CALL: ");
  client.print(String("GET ") + "/cgi-bin/temperature.php?apples=69" + " HTTP/1.1\r\n" +
               "Host: " + "www.xxxx.com" + "\r\n" + 
               "Connection: keep-alive\r\n\r\n");
  
  Serial.println("http request has been made");
  
 
  while (client.available()){
    Serial.write(client.read()); // read() gets the FIFO char
  }


  if (client.connected()){
    client.stop(); // stop() closes a TCP connection.
  }
}


// end wifi functions --------------------------------------------


void setup() {

  
  Serial.begin(9600); 
  
 lcd.begin(16, 2);
 lcd.print("Monitor    ");
  
  if(!bmp.begin()) {
   
   Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!");
   //while(1);
 }

// wifi setup  =========================================

  esp8266.begin();
  connectESP8266();
  
  

// end wifi setup  ====================================

  startTime =  millis();
  lastonehour = startTime;

  // voltmeter setup =========================
   pinMode(analogInput, INPUT);
  // end voltmeter setup ---------------
   
 
}

void loop() {
 
 
  sensors_event_t event;
  bmp.getEvent(&event);

   if (event.pressure){
    Serial.print("Pressure: ");
    Serial.print(event.pressure);
    Serial.println(" hPa");

     if (millis() < 4000){
       Serial.println(millis());
          currentpressure = event.pressure;
          Serial.println(event.pressure);
     }
      
   
   
    float temperature;
    bmp.getTemperature(&temperature);
    temperature = (temperature * 9/5) + 32;
    Serial.print("Temp: ");
    Serial.print(temperature);
    Serial.println(" F");

   
    
    lcd.setCursor(0, 0);
    lcd.print("                ");
    lcd.setCursor(0, 0);
    lcd.print("Temp: ");
    lcd.print(temperature);

    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    lcd.print("Pres: ");
    lcd.print(event.pressure);
    //lcd.print(" hPa");
  
    //String mystring = String(temperature + " myst " + event.pressure);
    //Serial.print(mystring);
  } else {
    Serial.println("Sensor error");
  }
  
  
  
  delay(8000);
  mytime = millis() - lastonehour;

  if(mytime >= 3600000){
    onehourvalue = currentpressure;
    currentpressure = event.pressure;
    lastonehour = millis();
  }
  
 lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.setCursor(0, 1);
    lcd.print("1 hr: ");
    
    if(onehourvalue > currentpressure){
      lcd.print(onehourvalue);
      lcd.print(" dn ");
    }else{
      lcd.print(onehourvalue);
      lcd.print(" up ");
    }
  // voltmeter loop stuff
   value = analogRead(analogInput);
   Serial.print("volts into arduino:");
   Serial.println(value);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 
   if (vin<11) {
   //vin=0.0;//statement to quash undesired reading !
    lcd.setCursor(0,1);
    lcd.print("Batts are LOW!");
    delay(5000);
   }
   lcd.setCursor(0, 1);
   lcd.print("Boat DC = ");
    lcd.print(vin);
    delay(5000);
  // end voltmeter stuff
 
}