Esp8266

Everything is possible.

It would have to be a new combined library though, and that is a lot of
work.

I wonder if you could run Cayenne on an ESP as well, then pass it data
through to the serial port. The ESP could be set to wake-up on pin change.
Just use the same dashboard ID on both.

Cheers,

Craig

1 Like

Not a coading guy that’s why having trouble

Want to develop a deviceby reading a digital input value use esp or Ethernet …

What about mqtt code you mentioned to try earlier

I have the Arduino code to use MQTT here Using Node-RED as a Local Fallback Server however it’s not a copy and paste. Try the code below and see if it works. I did not test it.

Change settings on the line “if (client.connect(clientID, username, password)) {” to match your MQTT info.

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "mqtt.mydevices.com";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  int timeoutcounter = 0;
  
  Serial.begin(9600);
  WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  while (WiFi.status() != WL_CONNECTED) {
    timeoutcounter++;
    delay(500);
    Serial.print(".");
    if (timeoutcounter >= 30){
      Serial.println("Failed to connect to wireless - sleeping for 5 minutes");
      delay(100);
      ESP.deepSleep(300000000, WAKE_RF_DEFAULT);
      delay(100);
    }
  }
  
  Serial.println("");
  Serial.println("WiFi connected");

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(clientID, username, password)) {
      Serial.println("connected");
      client.subscribe("CayenneInput");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Sleep for 5 minutes before retrying
      Serial.println("Failed to connect to MQTT server - sleeping for 5 minutes");
      delay(100);
      ESP.deepSleep(300000000, WAKE_RF_DEFAULT);
      delay(100);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  float t = 20.2;
  float h = 30.3;
  float hif = 40.4;
  
  String mqtt_message = "temp,f=" + String(t) + ",channel=10";
  mqtt_message.toCharArray(msg, 50);

  Serial.print("Publish message: ");
  Serial.println(msg);
  client.publish("arduino_1_data", msg, true);

  mqtt_message = "rel_hum,rel_hum=" + String(h) + ",channel=11";
  mqtt_message.toCharArray(msg, 50);

  Serial.print("Publish message: ");
  Serial.println(msg);
  client.publish("arduino_1_data", msg, true);

  mqtt_message = "temp,f=" + String(hif) + ",channel=12";
  mqtt_message.toCharArray(msg, 50);

  Serial.print("Publish message: ");
  Serial.println(msg);
  client.publish("arduino_1_data", msg, true);
  delay(5000);
}
1 Like

Thnx will try it soon and then report u

in this video they have connected esp8266 to cayyene .
does it work?

Hi @priyaranjan391 and welcome to the Cayenne Community.

Yes, we have many users on this forum who have successfully connected ESP8266 microcontrollers to Cayenne and used them in projects using the code in post #20 of this thread. Give it a shot and let us know if you run into any questions or trouble.

What is the prefered version of AT commands with cayenne? 0.22 or the new 1.1?

Hi @vapor83,

It is very confusing. Doesn’t help that there are multiple ways to program, multiple softwares, and multiple ESP devices and boards based on those devices.

I prefer to tell people to just program the ESP directly with Cayenne, and use an I/O expander if you need more I/O.

So I don’t have an answer for you. What we need to do is try, and let us know what works. Then we can create a detailed procedure and a table showing what works.

Cheers,

Craig

I’ve been using mainly the .22 which seems to work good. Just starting to use the 1.1 and that seems to work fine too. Just a fyi

Don’t think it matters as long as it works.

What ESP, and how are you programming them?

Can you share a rough procedure for us?

Cheers,

Craig

So I’ve been using the ESP8266-01 & the new ESP8266-01S. I take a proto shield and mount the ESP to it or use a breadboard adapter if you want to be able to remove it (see pics). I basically make a ESP shield that goes to the Serial RX/TX with a switch on it for easy uploading to the arduino.

First you need to upload the AT firmware for the ESP so the arduino/cayenne can talk to it using AT commands. You need the following: ESP_download_tool_v2.4 & the ESP_IOT_SDK 1.0.0. I included just the files you need here for simplicity. Open the download tool and setup the files and addresses(ADDR) exactly as shown.

blank.bin → 0xFE000
boot_v1.5.bin → 0x00000
user1.1024.new.2.bin → 0x1000
blank.bin → 0x7E000
esp_init_data_default.bin → 0xFC000

  1. Connect ESP and put into firmware upload mode by grounding GPIO0 and then powering on the ESP.
  2. Make sure SpiAutoSet is checked & select your correct com port. Baud usually works at 230400 but try lower if you have troubles.
  3. Click Start.

Now you should have AT command set firmware! Unground GPIO0 and reboot the ESP. Open up a serial terminal and test with come commands.

Set Baud at 115200 and try typing AT. It should send back “ok” if everything worked.

Now we need to set it to station mode. Type AT+CWMODE_DEF? and it will probably return the number 2… we want it to say 1. Now type AT+CWMODE_DEF=1 and it should say ok. Verify with the first command AT+CWMODE_DEF? and it should return 1 now. Now enter AT+RST and it will reboot and say ready. Now were ready to connect it to our sketch/cayenne.

You need to include this library : #include <CayenneESP8266Shield.h>
In the header of your sketch you need to define which serial were using which is usually the first Serial (pins 0 &1):
#define EspSerial Serial
ESP8266 wifi(EspSerial);

Then in setup you need to tell it to start by:
EspSerial.begin(1115200);
delay(10);

So when were done it looks like this:

#include <CayenneESP8266Shield.h>

char token[] = "blah";
char ssid[] = "devices";
char password[] = "password";

#define EspSerial Serial
ESP8266 wifi(EspSerial);

void setup()
{
	EspSerial.begin(115200);
	delay(10);

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

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

Thats it!

1 Like

Cheers. That’s awesome!

Thx,

Craig

The DS18B20 does not work on WeMos D1 R2 on Cayenne.
Connected to pin D9, Virtual V1.
Code:

#include "CayenneDefines.h" 
#include "BlynkSimpleEsp8266.h" 
#include "CayenneWiFiClient.h" 
#define CAYENNE_DEBUG 
#define CAYENNE_PRINT seryjny
#include <OneWire.h>
#include <DallasTemperature.h>

// Virtual Pin of the DS18B20 widget.
#define VIRTUAL_PIN V1

// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
const int tmpPin = D9;

OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);

char token[] = "..........";
char ssid[] = "............";
char password[] = "";

void setup()
{
  Serial.begin(115200);
  Cayenne.begin(token, ssid, password);
}
void loop()
{
  Cayenne.run();
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
  // Send the command to get temperatures.
  sensors.requestTemperatures();
  // This command writes the temperature in Celsius to the Virtual Pin.
  Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));
  // To send the temperature in Fahrenheit use the corresponding code below.
  //Cayenne.fahrenheitWrite(VIRTUAL_PIN, sensors.getTempFByIndex(0));
}

I personally don’t like running sensors.requestTemperatures in a CAYENNE_OUT call. No big deal when it’s quick, but if you have multiples on the one wire bus, it can take some time.

I think standard practice should be to run it in a timed loop using the timer library interval function, then store the values into global variables that the CAYENNE_OUT functions can simply grab when requested.

Here’s an example scheme you can implement.

Cheers,

Craig

1 Like

i think the problem in that code is:
where it says
Cayenne.celsiusWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));

mod to
Cayenne.virtualWrite(VIRTUAL_PIN, sensors.getTempCByIndex(0));

thats gets my ds18b20 sensor up and running.

regards
rck

hi there,i run esp8266-01 with ssr attached to it using that
for the ssr, i use channel 3 cause all other channel boot with the ssr high and relay modules needs 5v ssr needs 3v to activate
simply copy and paste in arduino ide then change all ***** with your infos then upload

1 #define CAYENNE_DEBUG // Uncomment to show debug messages
2 #define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
3
4 #include “CayenneDefines.h”
5 #include “BlynkSimpleEsp8266.h”
6 #include “CayenneWiFiClient.h”
7
8 // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
9 char token = “********”;
10 // Your network name and password.
11 char ssid = “";
12 char password[] = "
”;
13
14 void setup()
15 {
16
17 Serial.begin(9600);
18 Cayenne.begin(token, ssid, password);
19 }
20
21 void loop()
22 {
23 Cayenne.run();
24 }

Code should work the same, just need to research how to put it in program
mode and set the device setting for the amount of memory etc your device
has.

Google your device and set the Arduino IDE settings to match.

1 Like

change celsiusWrite for virtualWrite

regards
rck

3 Likes