ESP8266 WiFi and Cayenne credential configurator

Update : Check this link for the new version of this library :

Here is a library to configure WiFi credential, static IP adress and hostname for your ESP8266.
It also set your Cayenne username, password and clientID via a web page in AP mode.
Inspired from kentaylor/WiFiManager (forked from tzapu/WiFiManager)

Cayenne_Connect folder contain the library.

Cayenne_test contain the example Cayenne_test.ino file.

Summary
 /****************************************************************************************
    * File	:		Cayenne_test.ino
    * Date	:		2017-Dec-14
    * By	:		Jean-Daniel Lavoie
    * Description :	Inspired from kentaylor/WiFiManager (forked from tzapu/WiFiManager)
    *				Manage WiFi connection at every reboot for 60 seconds whit fixed IP.
    *				Connect to cayenne.mydevices.com
    ****************************************************************************************/

    #include <Cayenne_Connect.h>

    //#define CAYENNE_DEBUG
    #define CAYENNE_PRINT Serial
    #include <CayenneMQTTESP8266.h>
    char username[48];																	// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
    char password[48];
    char clientID[48];

    #include <queue>																	// std::queue
    typedef enum user_request {
    	RESTART,																		// Reset event.
    	SEND,																			// Send event.
    } user_request;
    std::queue<user_request> request_queue;


    //***************************************************************************************
    // setup()		Put your setup code here, to run once:
    //***************************************************************************************
    void setup(void) {
    	LED_Init();																		// Turn on LED during BOOT.
    	LED_ON();

    	Serial.begin(115200);															// Start serial for debug.
    	Serial.println(F(" "));															// First print is garbage.

    	Cayenne_Connect my_connection;													// Manage WiFi connection whit staticIP.
    	if (WiFi.isConnected()) {														// Turn LED OFF if connected.
    		LED_OFF();
    	}

    	strcpy(username, my_connection.getMQTTusername());
    	strcpy(password, my_connection.getMQTTpassword());
    	strcpy(clientID, my_connection.getMQTTclientID());
    	Cayenne.begin(username, password, clientID);									// Start Cayenne MQTT client.
    }


    //***************************************************************************************
    // loop()		Endless storie
    //***************************************************************************************
    void loop() {
    	Cayenne.loop();
    	yield();

    	if (!request_queue.empty()) {
    		switch (request_queue.front()) {
    			case SEND:																// Update Cayenne dashboard.
    				LED_Toggle();
    // Put your Cayenne.virtualWrite(...); here	
    				Cayenne.virtualWrite(1, false);										// Update cayenne_send button.
    				Serial.println(F("Cayenne server updated."));
    				break;

    			case RESTART:															// Reset!!
    				Cayenne.virtualWrite(0, false);										// Update cayenne_restart button.
    				Serial.println(F("!!Restarting NOW!!"));
    				ESP.restart();
    				delay(5000);
    				break;

    			default:
    				CAYENNE_LOG("Unknow user_request");
    		}
    		request_queue.pop();
    	}
    }


    //***************************************************************************************
    // Function for processing actuator commands from the Cayenne Dashboard.
    //***************************************************************************************
    CAYENNE_IN(0) {
    	request_queue.push(RESTART);
    	CAYENNE_LOG("Restart requested.");
    }


    CAYENNE_IN(1) {
    	request_queue.push(SEND);
    	CAYENNE_LOG("Cayenne write requested.");
    }


    CAYENNE_IN_DEFAULT() {
    	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
    	//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
    }
2 Likes

Thank you for posting this @DJayDeee !

Awesome, thanks for posting this!

For an unfortunate reason Cayenne does not connect if we use static IP address anymore so I rework it to switch to DHCP after the configuration portal is close. But there is an issue (https://github.com/esp8266/Arduino/pull/4145) with Arduino for ESP8266 and I need to use the lastest github repository of it.

Check this link for the new version of this library : https://github.com/DJayDeee/Cayenne_Connect

1 Like