Hello,
The purpose of my project is to create an automated blind system using MQTT, with a servo attached to my board. I am using the LoLin V3 NodeMcu Lua WIFI Development Board / LoLin V3 NodeMcu Lua WIFI Development Board, along with a Futaba S3003 servo motor. Below is the sketch that I am using:
/***************************************************
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board:
----> https://www.adafruit.com/product/2471
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
/************************* WiFi Access Point (CHANGE THIS) *********************************/
#define WLAN_SSID "MyWiFiSSID"
#define WLAN_PASS "MyWiFiPW"
/************************* MQTT Setup (CHANGE THIS) *********************************/
#define AIO_SERVER "192.***.*.**"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "user"
#define AIO_KEY "password"
/************ Global State (Don't Change) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
/****************************** Feeds (CHANGE THESE)***************************************/
const char BLINDSTATUS[] PROGMEM = AIO_USERNAME "/blinds/state";
Adafruit_MQTT_Publish blindstatuspub = Adafruit_MQTT_Publish(&mqtt, BLINDSTATUS);
const char BLINDCOMMAND[] PROGMEM = AIO_USERNAME "/blinds/command";
Adafruit_MQTT_Subscribe blindcommandsub = Adafruit_MQTT_Subscribe(&mqtt, BLINDCOMMAND);
/*************************** Sketch Code ************************************/
Servo myservo;
int state = 0;
int prevstate = 0;
int dirc = 0;
int spintime = 0;
int servoPin = D4; //CHANGE TO WHATEVETR PIN YOUR USING
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
Serial.println("Blind Startup Sequence");
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
mqtt.subscribe(&blindcommandsub);
}
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &blindcommandsub) {
Serial.print(F("Receieved command: "));
String blindcommand = (char *)blindcommandsub.lastread;
Serial.print(blindcommand);
if ( blindcommand == "open") {
Serial.println(" 433MHZ OPEN COMMAND RECEIVED");
dirc = 180; // direction for servo to run
spintime = 3700; // << CHANGE TO WHATEVER TIME YOU NEED TO OPEN YOUR BLINDS
state = 1; //sets current state
}
else if ( blindcommand == "close" ) {
Serial.println(" 433MHZ CLOSE COMMAND RECEIVED");
dirc = 0;
spintime = 3300; // << CHANGE TO WHATEVER TIME YOU NEED TO CLOSE YOUR BLINDS
state = 2; //sets current state
}
if (state != prevstate) {
Serial.println("State change!");
servo_move();
}
prevstate = state;
}
if (! mqtt.ping()) {
mqtt.disconnect();
}
delay(500);
}
}
void servo_move() {
Serial.println("State Change. Rotating Servo");
if ( dirc == 180) {
blindstatuspub.publish("opened");
}
else if (dirc == 0) {
blindstatuspub.publish("closed");
}
myservo.attach(servoPin);
myservo.write(dirc);
delay(spintime);
myservo.detach();
Serial.println("Returning to main loop");
return;
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}
I have tried several different iterations of the above sketch, but can not get it passed WiFi Connected. Below is the error message that I receive:
Connecting to TC23
..............
WiFi connected
IP address:
192.168.2.3
Connecting to MQTT...
Exception (3):
epc1=0x4000bf3c epc2=0x00000000 epc3=0x00000000 excvaddr=0x40231954 depc=0x00000000
ctx: cont
sp: 3ffefd60 end: 3ffeff80 offset: 01a0
>>>stack>>>
3ffeff00: 3ffe8690 0000000b 3ffeef24 3ffeef50
3ffeff10: 3fffdad0 3ffeece0 3ffeece0 40203782
3ffeff20: 3ffe83fc 3ffeedd4 3ffeef24 40204948
3ffeff30: 0302a8c0 00ffffff 3ffeef24 3ffeef50
3ffeff40: 00000003 3ffeece0 3ffeef24 40202506
3ffeff50: 3fffdad0 00000000 3ffeef49 40202550
3ffeff60: 3fffdad0 00000000 3ffeef49 40203d38
3ffeff70: feefeffe feefeffe 3ffeef60 40100958
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
The sketch uploads no problem, but reset after it connects to WiFi. I have tried a few examples, all of which work, except for the example MQTT for ESP8266. I have researched the problem and rst causes, but can find no solution. I have tried delays, different usb cables, WiFi.persistent(false) (which I had read was a solution to Exception (3)), different baud rates, etc.
Below is are some pictures of how I have the board setup:
I have very limited experience with Arduino, so please excuse me if I have missed something simple or misunderstood something. Thank you in advance.