Arduino Uno - ESP8266 Connection problem

Hi people,
Im Trying to connect cayenne With Arduino Uno with ESP8266 wifi Connection.

First of all i tested if ESP8266 connects wifi with arduino to wifi; if i connnect RX to RX and TX to TX At commands works and connects to wifi

Than i Created New Device - Selected Arduino uno As board And Selected As board Connection as Arduino ESP8266 Wifi. Uploaded the Sketch and got error ‘Serial1’ was not declared in this scope

i researched the forum and know that it happens because arduino uno does not use Serial 1-2 etc. Than used instead of #define EspSerial Serial1 - #define EspSerial Serial;

The code did not give me error but this time im getting following error:

AT
[1529] ESP is not responding

Than i tried to add softwareserial library of arduino and added code
SoftwareSerial mySerial(0, 1); // RX, TX

Same error like Above…

Anyone have solution ideas?

p.s :

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x5b
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x31
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x30
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x35
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x37
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x35
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x5d
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x20
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x46
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x61

Anyone can help me please
Regards

first, remove esp8266 TX and RX pin from the Arduino. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries). on your cayenne dashboard add a new device and get your MQTT credential. add them to below code and your wifi credentials. upload the code to your Arduino(make sure you don’t have any pins connected to RX and TX). once uploaded disconnect Arduino from your pc. connect esp8266 RX to TX of the Arduino through a voltage divider and TX of esp8266 to RX of arduino. Reconnect your arduino to pc. Open serial monitor with baud rate set to 115200. you will see something like this:

AT
ATE0
AT+CIPMUX=1
AT+CWMODE?
AT+CWJAP="Wifi","Pass"
[8095] Connected to WiFi
[8095] Connecting to mqtt.mydevices.com:1883
AT+CIPSTART=1,"TCP","mqtt.mydevices.com",1883
AT+CIPSEND=1,40

Code:

/*
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected via a hardware serial to an Arduino.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
   folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries) to compile this example.
2. Connect the ESP8266 as a shield to your Arduino. This example uses the Serial1 hardware serial pins available on the Mega. You can also try 
   using a software serial, though it may be less stable.
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

NOTE: This code requires ESP8266 firmware version 1.0.0 (AT v0.22) or later.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial

ESP8266 wifi(&EspSerial);

void setup()
{
  //Serial.begin(9600);
  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(10);

  Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
  Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Thank you,
as you said i uploaded code without connecting the pins and than connected after upload. I successfully connected to Cayenne.

But i did not use voltage divider. What u mean by it? do you mean Using Resistances or logic level connectors?

Please add a resistance voltage divider.

1 Like

It will “work” without the voltage divider but the ESP is 3.3v logic and Arduino is 5v logic. I have an ESP that I use specifically for testing and have never had a problem with it, but it could shorten the life of the device. Long story short, if you are just testing something you should be fine but if you are making a project definitely do it the right way.

1 Like

thanks for the reply,
so if i use Nodemcu for the same project do i need to use voltage divider or logic level converter?

If you mean using a NodeMCU as a replacement for the Arduino/ESP8266, then no. If you are using it as a replacement for just the ESP8266 then yes.

1 Like

if you are using Nodemcu, then you dont need ardiuno and esp8266. Because Nodemcu is a development board which has esp866 inbuilt and you can connect it directly to cayenne Adding a New Device using MQTT

tx wih tx or tx with rx?

Uno---------------------------Esp8266
Rx-----------------------------Tx
Tx------voltage divider----Rx

Thanks I already did. When I open serial monitor I recieve a message statint ESP not responging. Which could be the reason for this? Do I have to buy another one?

you have powered your esp8266 from uno 3.3v and used a voltage divider ?
use this code

/*
This sketch connects to the Cayenne server using an ESP8266 WiFi module as a shield connected via a hardware serial to an Arduino.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. Install the ESP8266SerialLibrary.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library) from the Cayenne extras/libraries
   folder (e.g. My Documents\Arduino\libraries\CayenneMQTT\extras\libraries) to compile this example.
2. Connect the ESP8266 as a shield to your Arduino. This example uses the Serial hardware serial pins available on the UNO. 
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
4. Set the network name and password.
5. Compile and upload the sketch.
6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.

NOTE: This code requires ESP8266 firmware version 1.0.0 (AT v0.22) or later.
*/

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
//#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTESP8266Shield.h>

// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";


char username[] = "";
char password[] = "";
char clientID[] = "";

// Set ESP8266 Serial object. In this example we use the Serial1 hardware serial which is available on boards like the Arduino Mega.
#define EspSerial Serial

ESP8266 wifi(&EspSerial);

void setup()
{
  delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(115200);
  delay(10);
    Cayenne.begin(username, password, clientID, wifi, ssid, wifiPassword);
}

void loop()
{
  Cayenne.loop();
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

In my first attempts I did not use voltage divider now I have it connected as you describe in the picture image

Sorry that is an old image i used as reference to show voltage divider circuit. you need to connect Tx to Rx and Rx to Tx.

thank you I will try and let you know. I already ordered a Mega and another ESP01 that is supposed to arrive today to have it in another aquarium. How is the connection with the mega?

mega---------------------------esp8266
RX1-----------------------------TX
TX1-----voltage divider------Rx