ESP8266 with Arduino

Hey guys, I just learned about IoT, and I’m trying a project using the ESP8266-01 and ArduinoUno R3 modules. This is the scheme that I use :

flash-mode-660x3301

I have a problem when I want to upload the code from Arduino IDE to the ESP module.
This is the code that I use :

/*
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[] = "arduino";
char wifiPassword[] = "joking77";

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

// 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(9600);
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");
}

When I try to upload it, it displays an error message. This is the message:

Arduino: 1.8.9 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, ck, 26
MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

<command-line>:0:9: error: expected unqualified-id before numeric constant

C:\Users\Bagus Ramadhan\Documents\Arduino\as\as.ino:35:1: note: in expansion of macro 
'ESP8266'

ESP8266 wifi(&EspSerial);

^

C:\Users\Bagus Ramadhan\Documents\Arduino\as\as.ino: In function 'void setup()':

as:46:47: error: 'wifi' was not declared in this scope

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

                                           ^

exit status 1
'wifi' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I intend to try turning on the Arduino LED on pin 13 using cayenne, but I have a problem

Can you help me? What should I do? I was a little dizzy and confused because this problem had not been resolved :sweat_smile:

Have you followed the steps provided in the code?

I did it, and when I tried to compile again, a different error message appeared. this is the error
message :

Arduino: 1.8.9 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Disabled, ck, 26 
MHz, 40MHz, DOUT (compatible), 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

In file included from C:\Users\Bagus Ramadhan\Documents\Arduino\8266\8266.ino:21:0:

C:\Users\Bagus 
Ramadhan\Documents\Arduino\libraries\CayenneMQTT\src/CayenneMQTTESP8266Shield.h:36:2: 
error: #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.

#error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting.

^

exit status 1
Error compiling for board Generic ESP8266 Module.

This report would have more information with
"Show verbose output during compilation"
 option enabled in File -> Preferences.

looks like this :

You need to select arduono uno board

OH Thank’s! Finally I know where my fault is, I use a different library, in my case (CayenneMQTTESP8266Shield.h). Then I look for it in the community and it refers here. :

One more question.I don’t know where to start to write the code , i want to try to make the LED on Arduino turn on. can you give an example of the code? :sweat_smile:

in cayenne, there are two main functions.

  1. To receive data.

    CAYENNE_IN(1)
    {
    int x = getValue.asInt(); //receives data from cayenne on channel 1 and is stored in x.
    }

  2. To send data.

    CAYENNE_OUT(2)
    {
    Cayenne.virtualWrite(2, 100); //this sends value 100 on channel 2
    }

Thanks it works! Thank you guys :grin:
*Pss sorry for late reply

1 Like