ESP8266 Wifi + 1 Relay Configuration


#1

HI…
I need Arduino Uno code for 1 Relay and it must work with Wifi using ESP8266 Wifi Module.
Please provide me with the working code.

Thanks,
Simar Preet Singh
er.simarpreetsingh@gmail.com


#2

Hi @er.simarpreetsingh, welcome to the Cayenne Community.

Have you been able to connect your Arduino Uno using ESP8266 as a shield with Cayenne already? If not, see this post for some example code to get your started.

To add a Relay, you can use the RelaySwitch example in the Arduino Library. You’ll need to configure it for the pins on your relay and Cayenne widget per the comments at the top, and then combine it with the connectivity sketch linked for Uno/ESP8266. If you do get stuck doing this, show us your code (you can blank out the Cayenne token), and we’ll be happy to assist.


#3

Thanks rsiegel. Great… You make the big problem so easy… But still yet to see live.

Well, How to get the Authentication Token that needs to be filled in the program from Cayenne?

In the RelaySwitch example, What Virtual pin number means? Also, Which button widget is being referred for switching on and off the relay?

Also can I control relay On/Off from my Mobile over the internet? Is there any Cayenne App?


#4

To get the authentication token, you’ll need to log into your Cayenne Web Dashboard, and select Add New > Device/Widget > Microcontrollers > Arduino. Following the on-screen instructions you’ll get to a screen with your token where I’ve placed this box:

Once the Arduino is connected, you’ll need to make a button widget on your dashboard via Add New > Device / Widget > Custom Widgets > Button:

When you’re filling out the settings for that, you can set the I/O field to ‘Virtual’ and choose any pin number you like. Just make sure that the number here matches the number you set in the sketch.

For your last bit, yes, this is the purpose of Cayenne, to allow you to control IoT projects via the internet. You’ll be able to control that Button widget from your web dashboard, as well as iOS and Android apps.


#5

Thanks Rsiegel,

I have started doing step by step as suggested by you. For the first part, I am getting this error on compiling the code:

Please help what to define on Serial1 as I want to configure ESP8266 to take IP from my router and access internet.


#6

Thanks Rsiegel.

For the second part, RelaySwitch, the compilation of the code is successful. But on Uploading I am getting this error:


This seems the code is not getting uploaded to Arduino Uno Board.

Please Suggest.


#7

Hi @er.simarpreetsingh,

For the 2nd issue (the ‘not in sync’ messages), this looks like it is a communication issue between the Arduino IDE and your device, not anything to do with the code. As a test, you might want to step back and make sure that a basic sketch like the built in ‘Blink’ sketch from File > Examples > 01. Basics > Blink is able to upload to the device. I suspect in that state, it probably will not.

There a number of things to try to resolve that error message, this post from StackExchange does a good job of covering what to check (USB Cable, Port, Board setting in IDE, etc)


#8

For this issue (the error mentioning Serial1), see this post from a bit farther down that thread, which has revised code that should compile OK for you: WifiShield with arduino uno compiling error

Basically because the Arduino Uno doesn’t have a 2nd serial port like the original code was expecting, this revised code disables the serial port typically used for debug and uses it to facilitate communication with the ESP chip.


#9

I tried all the methods to resolve, but yet also I am facing the same error.


Please help.


#10

I think now the upload is complete.


I have removed TX and RX i.e. Pin 0 and 1 of Arduino Uno board, which I connected with the ESP8266 wifi.

Should I remove it only during uploading the code? or I must not connect anything to pin 0 or pin 1 ??


#11

Great Rsiegel… Thanks…
As suggested, I have uploaded the code in my Arduino Uno Board connected with Wifi8266 Shield on RX at 7 and TX at 8 Pin.

/*Cayenne ESP8266 Shield WiFi Example
Adapted from Blynk's ESP8266_Shield_HardSer Example

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

You should install the ESP8266HardwareSerial.zip library via the Arduino IDE (Sketch->Include Library->Add .ZIP Library)
from the Cayenne extras/libraries folder (e.g. My Documents\Arduino\libraries\Cayenne\extras\libraries) to compile this example.

NOTE: Ensure a stable serial connection to ESP8266!
      Firmware version 1.0.0 (AT v0.22) or later is needed.
      You can change ESP baud rate. Connect to AT console and call:
          AT+UART_DEF=115200,8,1,0,0

For Cayenne Dashboard widgets using digital or analog pins this sketch will automatically
send data on those pins to the Cayenne server. If the widgets use Virtual Channels, data
should be sent to those channels using virtualWrites. Examples for sending and receiving
Virtual Channel data are under the Basics folder.
*/

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#include <CayenneESP8266Shield.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "";
char ssid[] = "";
char password[] = "";

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(7, 8); // RX, TX
#define CAYENNE_PRINT DebugSerial  // Comment this out to disable prints and save space

// Set ESP8266 Serial object
#define EspSerial Serial
ESP8266 wifi(EspSerial);

void setup()
{
	DebugSerial.begin(9600);
	delay(10);
	// Set ESP8266 baud rate
	EspSerial.begin(115200);
	delay(10);

	Cayenne.begin(token, wifi, ssid, password);
}

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

But now the issue is, I have removed the USB Cable from the computer, how I can check whether the Arduino Uno board is connected successfully to my router or not? Is my Arduino Uno board now working as a access point? or what? I can see some ESP_14C03 wifi connection OPEN in my available wifi connections. If this works as a access point now, then can I change the SSID and how can i secure this wifi connection?


#12

Also which option to select among Arduino Uno from the Dashboard for my project of ESP8266 Wifi + 1 Relay :


#13

You don’t actually have to check anything on that page, since you’ve already gotten your sketch code from the forum, and aren’t using any of those options. All you need from that page is the ‘Auth Token’ in the upper right, that you can paste into the sketch in this line:

char token[] = "put your auth token here";

If everything is working correct, when you run the sketch, it should make an internet connection using that token to identify your account with our server, and the website should advance from the page you’ve pictured to a blank dashboard that looks like this:

If you need to debug, as you mentioned, it looks like @kreggly has you covered a bit farther down that thread: :slight_smile: WifiShield with arduino uno compiling error

Alternatively, if you connect your Uno with one of our supported shields (the ones pictured in your last screenshot) instead of the ESP8266 for connectivity, then you would be able to use the Arduino IDE to debug as the serial on the Uno wouldn’t be occupied with communication with the ESP.


#14

Yes… I have put the auth code. But How can i check whether the ESP8266 is connected to the wifi router of not?


#15

If it shows up in Cayenne as an online device (like in my blank dashboard image, without a yellow banner saying the device is offline), you could check that way. Otherwise you could try the solution in kreggly’s post to enable software serial to see debug output which could help you determine this.

If you have access to the router admin, the simplest way might be to look for a list of connected devices. Most routers I’ve owned have had a page like that in their web interface where you can see a list of each device currently connected.


#16

I am getting this on my Serial Monitor:

Please Help


#17

Have a look at this thread for additional troubleshooting steps (like verifying your firmware) when another user had this same issue.


#18

But How I can run the AT Commands in Arduino Uno? How to open that terminal to run AT commands?


#19

I don’t know that you can. Why would you want to?


#20

Thread suggested by rsiegel in above reply checks whether AT command response is coming or not.
So to check that response, I have to query how to run the AT command.

Well adam, my query is:

I have Arduino Uno board. I have ESP8266 Wifi Module. I have attached 1 relay to turn light ON and OFF. I want to switch ON and OFF the light using the mobile app through internet from remote location. This all is working fine with Blynk Server. But issues with Blynk is we have to attach USB Cable from PC to Arduino Uno board to run Blynk server+ for internet connection to the Uno Board.
After blynk, someone told me thingspeak.com can solve this issue, but till date I am not getting the Arduino code of thingspeak.com for this problem. I explored on google for that, and I came to know about Cayenne. Cayenne seems good and seems to provide much more features than thingspeak.com. Since that day till today I am struggling hard to solve this problem, but yet I am not getting any proper solution for my problem and my problem still stands the same.

I want Arduino Uno Board to work independently using the internet from the Wifi Router and without the use of PC for switching ON and OFF the light using 1 relay attached to it.
Please suggest if you or anyone have some solution to this.