Esp8266-mqtt


#1

Hello @rsiegel ,
I Successfully added ESP8266-12E development board to cayenne.But LED button is not responding, It continuously searching to connect. I programmed successfully ESP12E using OTA.
The code is given below ( it is same as Acrobatic/Cayenne Youtube your sponsored video Remote monitoring using MQTT.)

#include <CayenneMQTTESP8266.h>
#define CAYENNE_DEBUG
#define CAYENNE_PRINT serial

char ssid[] = "iBall-Baton";
char password[] = "****************";

char username[] = "mqtt-username";
char mqtt_password[] = "mqtt-password";
char client_id[] = "clientID";



void setup() {
  // put your setup code here, to run once:
  Cayenne.begin(username,mqtt_password,client_id, ssid,  password);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  

}

void loop() {
  // put your main code here, to run repeatedly:
  Cayenne.loop();

}

CAYENNE_IN(0)
{
digitalWrite(2, !getValue.asInt());
}

Your thoughts on this will be greatly appreciated.


Ayuda para conectar mi arduino con ESP8266
#2

Hmmmm…Did you check if there could be wiring issue? Or are you trying to blink on-board LED?

-Benny


#3

I have an idea here but want to check the code on my device, check back soon! :slight_smile:


#4

Here is how I accomplished this, this was tested on Arduino and not ESP8266, but I don’t think that should make a difference. I edited it so that it still has your ESP connectivity code, but you’ll need to add back your Username/Password/Client ID (I edited them out for privacy reasons).

In my case the LED is on digital pin 9, and the actuator widget on my cayenne dashboard was added to channel 1.

#include <CayenneMQTTESP8266.h>
#define CAYENNE_DEBUG
#define CAYENNE_PRINT serial

char ssid[] = "iBall-Baton";
char password[] = "****************";

char username[] = "mqtt-username";
char mqtt_password[] = "mqtt-password";
char client_id[] = "clientID";

    int ledPin = 9;

    void setup() {
    	Serial.begin(9600);
    	Cayenne.begin(username,mqtt_password,client_id, ssid,  password);
            pinMode(ledPin, OUTPUT);
    }

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

    //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(1)
    {
    	digitalWrite(ledPin, getValue.asInt());
    }

#5

I uploaded the sketch . But still the same result.
While adding Button in Enter Setting is it necessary to add cayenne code .
given below.

**
* @file SimplePublish.c
*
* Simplified example app for using the Cayenne MQTT C library to publish example data.
*/

#include "CayenneMQTTClient.h"

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char* username = "MQTT_USERNAME";
char* password = "MQTT_PASSWORD";
char* clientID = "CLIENT_ID";

Network network;
CayenneMQTTClient mqttClient;


// Connect to the Cayenne server.
int connectClient(void)
{
	// Connect to the server.
	int error = 0;
	printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
	if ((error = NetworkConnect(&network, CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
		return error;
	}

	if ((error = CayenneMQTTConnect(&mqttClient)) != MQTT_SUCCESS) {
		NetworkDisconnect(&network);
		return error;
	}
	printf("Connected\n");

	// Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
	CayenneMQTTPublishData(&mqttClient, NULL, SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
	CayenneMQTTPublishData(&mqttClient, NULL, SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "Linux");

	return CAYENNE_SUCCESS;
}


// Main loop where MQTT code is run.
void loop(void)
{
	while (1) {
		// Yield to allow MQTT message processing.
		CayenneMQTTYield(&mqttClient, 1000);

		// Publish some example data every second. This should be changed to send your actual data to Cayenne.
		CayenneMQTTPublishDataFloat(&mqttClient, NULL, DATA_TOPIC, 0, TYPE_TEMPERATURE, UNIT_CELSIUS, 30.5);
		CayenneMQTTPublishDataInt(&mqttClient, NULL, DATA_TOPIC, 1, TYPE_LUMINOSITY, UNIT_LUX, 1000);
	}
}

// Main function.
int main(int argc, char** argv)
{
	// Initialize the network.
	NetworkInit(&network);

	// Initialize the Cayenne client.
	CayenneMQTTClientInit(&mqttClient, &network, username, password, clientID, NULL);

	// Connect to Cayenne.
	if (connectClient() == CAYENNE_SUCCESS) {
		// Run main loop.
		loop();
	}
	else {
		printf("Connection failed, exiting\n");
	}

	return 0;
}

I tried it but it is not compiling in Arduino IDE


#6

What is the error you get in the IDE console?


#7

I didn’t get any error message on Arduino IDE consol. The sketch is 100% uploded. And device ESP12E development board with CP2102 (usb to serial) is connected properly to cayenne. When I add Button it is added to Cayenne dashboard But on clicked it is not changing the state of button, It shows continuous searching action just like not reachable action.


#8

I’m getting confused, maybe a language translation issue? Lets start with the basics, which sketch are you currently using? I see you initially posted a sketch but in your last post you included a different sketch. Also I see that the MQTT username/password/clientID is still set at the default values. Was that intentional for posting to the forum? It is a good idea to delete this info before posting, just wanted to make sure you were changing in the sketch you are uploading.


#9

The sketch I am using is given below.

#include <CayenneMQTTESP8266.h>
#define CAYENNE_DEBUG
#define CAYENNE_PRINT serial

char ssid[] = "iBall-Baton";
char password[] = "***************";

char username[] = "*********************************************";
char mqtt_password[] = "*********************************************";
char client_id[] = "****************************************";

int ledPin = 2;

void setup() 
 {
  // put your setup code here, to run once.
  Serial.begin(9600);
  Cayenne.begin(username, mqtt_password, client_id, ssid,  password);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  }

void loop() 
{
  // put your main code here, to run repeatedly:
  Cayenne.loop();
}

CAYENNE_IN(0)
{
digitalWrite(ledPin, !getValue.asInt());
}

With this code uploading ESP12E is added to Cayenne.

I want to make led on pin no2 to ON/OFF
So I added Custom Widget "Button"
Name-LED1
Device - ESP8266-MQTT
Sensor - Digital Actuator
Unit-Digital (0/1)
Channel 0
Icon - lamp
Then I clicked "Add widget"
Note: The option “Step1: Code” I cannot use. (This only I missing , I think)

The “Button” appears on cayenne dashboard.
But the action of LED ON/OFF expected, is not working.


#10

There should be no need to use that “Step 1: Code” button or anything to enter there. That button just provides example C code that you could use for connecting a similar MQTT device – you can ignore it since you are using Arduino Sketch code anyway.

I tried your code and it worked OK for me (again, on an Arduino, but I even re-wired the LED to Pin 2 and changed my button widget to channel 0.) I did notice you inverted the logic by setting the pin to HIGH in setup() and inverting the getValue.asInt() result, but that worked OK on my end too.

Unless this is something ESP specific, could it potentially be a wiring issue? Have you tried with the LED on any other pin? I’m curious if it even lights up the sketch is first uploaded and hits the initial digitalWrite(ledPin, HIGH); statment, even if the button is not working as expected.


#11

If you run a regular sketch that just turns on/off gpio 2 does the led flash? I’m assuming you are using the built in led, right?


#12

You are absolutely Correct, I am using inbuilt LED . When I run a regular
sketch that just turns on/off gpio 2 the led flashes.


#13

Yes! I got it .
I traced physically the inbuilt led pin connection on my AMICA board, it is on GPIO16
But this was not the cause.
I removed my ESP-board from Cayenne. And added new credentials username,password, client id.
It immediately worked. Last four days I was using the same earlier credentials.


#15

I was having a similar issue with a relay on a Sparkfun 8266 Thing. This thread helped me out and was able to solve my problem. Kudos to all of you! :slight_smile:


#16

@rsiegel @adam Howdy!

I’m trying the above sketch on a esp32 in hopes it will upload and connect. I get avr/pgmspace.h no such file. Can you direct me to where on github I can get this file? Just trying to connect first.
thanks
william


#17

ESP32 is not supported yet. I do have one at home but I’m not sure when I’ll get time to look at it to see if I can get it connected. I have about 10 projects going on at once now… if you want to try yourself my plan would be to use the pubsubclient MQTT library (standard MQTT library, not Cayenne) to see if that works.