SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By tzifudzi
#193585
I want to connect my Arduino MEGA 2560 to the Sparkfun ESP8266 Thing so they can commuicate. This page: https://learn.sparkfun.com/tutorials/es ... e-overview contains information on the Sparkfun Thing pins. I want to use I2C to communicate between the two devices as I want to send sensor readings from the Arduino Mega to the internet using the Sparkfun Thing. I need to connect them together because I need to connect many sensors which will need many pins, and not to mention memory space. My question is how am I to set up the schematics and write the code?

Also, will I need any level shifting from the Mega to the Sparkfun Thing?
#193596
Since the Mega 2560 is a 5 volt device and the ESP8266 is a 3.3 volt device you DO need levelshifting.

But why must you use I2C to communicate between them? Why not serial? The Mega 2560 has plenty of them in hardware (4).
#193597
And before anyone suggests code will you first narrow down the meaning of "send sensor readings ... to the internet "? How much? How often? To where?

Have you tried storing data to the internet with an ESP8266 Thing in the first place? My guess is you are jumping into too big of a project right now. Start in small baby steps, one foot behind the other.
#193598
I agree with Valen... I had the same question in mind when I read your post, why I2C and not serial between the Mega and Thing. I currently have a Mega / ESP8266 module connected via serial and use the AT command set to send / retrieve data to and from ThingSpeak. The Thing is just a more featured version of the ESP8266 module so I would think you could do the same.

You'll need a voltage divider on your TX from the Mega to the RX on the Thing.

For AT commands, I'm using this library: https://github.com/itead/ITEADLIB_Arduino_WeeESP8266
By tzifudzi
#193605
After considering your suggestions and doing further research I have realized serial is probably the best way to go. Thanks Valen and Exeng.
Would this be the right way to set up the serial connections? http://i.imgur.com/PPp7JdD.png
Valen wrote:Have you tried storing data to the internet with an ESP8266 Thing in the first place?
I have. All I need to do is to make sure the Sparkfun ESP8266 can receive decimal sensor readings from my Mega.
Valen wrote:Have you tried storing data to the internet with an ESP8266 Thing in the first place?
I have successfully. I set up a local server on my computer and I am able to send and receive commands. I am rather good in code, but not so much in electronics.
Valen wrote:My guess is you are jumping into too big of a project right now.
You are right. But there is not turning back now as it is my final year project for computer science that I have already started. I also figured this is the best way for me to learn electronics so I wish to continue with this challenging project as I learn.

P.S. Sorry for not asking my question clearly the first time. I'm new to the forum.
#193606
You already responded as I was formulating the following. So it is probably moot since you chose serial UART communication anyway. But it might be of interest to others that have a similar desire.

The thing (no pun intended) about I2C is also that it is quite one-directional. The master initiates communication when it desires so. The slave cannot. The slave can only respond to it with returning a (not-)acknowledge bit at the end of an address-byte, or later data byte. You'll have to figure out which of the two, Mega 2560 or the ESP8266 acts as the master and which as the slave. (although multiple masters are possible with I2C, but uncommonly used so few examples) If the slave needs to send data back whenever it pleases it, like a message "stop!, my buffers are full" or "stop!, no Wifi/internet-connection" then it will have a harder time making that clear to the master. The master must then realize that something is up and request the status of the slave. This will resort to forcing data to the slave (a sort of polling) , break the session if it not-acknowledged (do a renewed start-condition,) and asking a "why not?" status command. Whereas with serial communication the ESP8266 could just send "stop!, because of reason XYZ" at will. Less latency involved I think. And I2C on the ESP8266 is bitbanged in software which might put caps on the data-rate if it has to do other things.
#193613
Since you want to send a lot of data, make sure to test if this voltage divider solution survives high data rates. At high frequencies the resistance of the divider might round off the edges of the pulses. (not enough time to charge pin-input and wires/breadboard capacitance) This can lead to data-corruption. If you have access to an oscilloscope I suggest you look how this turns out.

Here is a Microchip article that covers many solutions: https://www.newark.com/pdfs/techarticle ... sBrchr.pdf

Personally I had good luck with Sparkfun's older levelshifting article (now links through to logic level converter board hookup-guide) which showed a reverse biased diode between a 5 volt output and 3.3 volt input, the latter side being pulled up to 3.3 volt supply with a resistor. Just like the image at the botom of this link: http://electronics.stackexchange.com/qu ... -5v/102640
By tzifudzi
#193614
So I am using the circuit as in the image http://i.imgur.com/PPp7JdD.png. However I am failing to send a word from the ESP8266 Thing to the Arduino Mega. I also noticed that the Arduino Mega RX LED does not even blink when I open the Serial Monitor, but the TX LED does. And also, when I open the Serial Monitor with the ESP8266 Thing turned on, Serial Monitor shows a blank page with nothing on it.

Could it have something to do with needing to reset the DTR pin?

Here is my code:
Code: Select all
//Sender Code - Sparkfun ESP8266
void setup() {
  Serial.begin(9600);
}

void loop() {
    Serial.write("Hello");
    //delay(1000);
}
Code: Select all
//Receiver Code - Arduino Mega

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  if (Serial1.available()) {
        Serial.write("Serial connected");    
        Serial.write(Serial1.read());
  } else {
        Serial.write("No serial connected");    
  }
  delay(1000);
}
#193620
Well, the ESP 8266 is connected to pins D0 and D1 of the Arduino Mega 2560 as per your supplied image. That is "Serial", which is also the serial port that goes to the serial monitor of the Arduino IDE. Serial monitor won't show what the ESP8266 is sending since it is listening to the Mega2560 TX(0) pin, not the RX(0) pin which is connected to the ESP 8266 TX pin. In other words: ear listening to ear and mouth talking to mouth. The latter could even lead to a short and damage the pins when they have opposing output levels. (so don't send anything from the serial monitor in the current circuit!)

Your receiver code is listening to "Serial1", which is 19 (RX1) and 18 (TX1). So it won't receive anything from that as those pins are left unconnected. The mega is however sending responses (back to the ESP8266 and serialmonitor/PC) every second when it figures it didn't receive anything. (which is not the same as "no serial connected") Which accounts for the TX led lighting up.

So merely a case of getting your ports and wires in a twist. ;)
By tzifudzi
#193623
Valen wrote:So merely a case of getting your ports and wires in a twist. ;)
Oh my word its working now! I guess it was a novices mistake. I can now successfully communicate using the serial.

Thank you Valen, your comments were well explained and extremely helpful.