Hello friends,
I need some help.
I do work on one IoT. project. I stucked in problem which I describes below:
When I tried to connects my NodeMCU to Adafruit IO website Sathe it's can't connect it's goes to failed. I don't know why it happening and how but I tried all that solution which I know. But does not changes in output of serial monitor it's stable on adafruit connection failed. Anyone can say me what can I do to solve this problem. I describing my code of the project here.
Here I describing my code of project
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <DMD2.h>
#include <SPI.h>
#include <fonts/Arial14.h>
#define SCROLL_DELAY 75
/********* Variables **********/
char str; String payload;
uint32_t present;
bool first_time;
uint16_t scrollDelay; // in milliseconds
#define CHAR_SPACING 1 // pixels between characters
// Global message buffers shared by Serial and Scrolling functions
#define BUF_SIZE 75 char curMessage[BUF_SIZE];
char newMessage[BUF_SIZE];
bool newMessageAvailable = false;
ESP8266WiFiMulti WiFiMulti;
HTTPClient http; const int width = 1;
SPIDMD dmd(width, 1); // DMD controls the entire display
DMD_TextBox box(dmd); // "box" provides a text box to automatically write to/scroll the display
// Define the LED matrix pins
const int CLK_PIN = D5;
const int OE_PIN = D6;
const int A_PIN = D7;
const int B_PIN = D8;
const int SCK_PIN = D1;
const int DATA_PIN = D2;
/* Adafruit.io Setup */
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT “your AIO server”/ use 8883 for SSL
#define AIO_USERNAME "Your UserName"
#define AIO_KEY "Your Adafruit_key"
// Create an ESP8266 WiFiClientSecure class to connect to the MQTT server.
WiFiClient client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe message = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME
"/feeds/one");
Void MQTT_connect();
void setup() {
// Set up the LED matrix and turn it on
dmd.begin();
dmd.setBrightness(255);
dmd.selectFont(Arial14);
scrollDelay = SCROLL_DELAY; strcpy(curMessage, "Hello! ");
newMessage[0] = '\0';
Serial.begin(115200);
Serial.println(F("Booting...."));
int retries = 0;
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("SSID", "PASS1");
WiFiMulti.addAP("SSID2", "PASS2");
WiFiMulti.addAP("SSID3", "PASS3");
while ((WiFiMulti.run() != WL_CONNECTED) && (retries < 10)) { retries++;
delay(500);
Serial.print(".");
}
{if (WiFiMulti.run() == WL_CONNECTED) {
Serial.println(F("WiFi Connected"));}
else {
Serial.println(F("WiFi Not Connected"));}
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(F("Ready!"));
} newMessageAvailable = true; present = millis();
first_time = true;
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&message);
str = "Speak Your Message On GOOGLE ASSISTANT!!!";
}
void scrollText(char* msg)
{ int len = strlen(msg);
for (int i = 0; i < len + 5; i++)
{ for (int j = 0; j < width * 8 + CHAR_SPACING; j++)
{ dmd.clearScreen();
//dmd.fillScreen(0);
//Clear the display by setting all pixels to off (0)
int xOffset = -j;
int charIndex = 0;
while (xOffset < width * 8 && charIndex < len)
{ dmd.drawChar(xOffset, 0, str[charIndex] );
xOffset += 8 + CHAR_SPACING;
charIndex++;
} dmd.scanDisplay(); // update the display
delay(scrollDelay);
}
}
}
void loop() {
MQTT_connect();
// This is our 'wait for incoming subscription packets' busy subloop
// Try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1))) {
if (subscription == &message) {
payload = "";
Serial.print(F("Got: "));
Serial.println((char )message.lastread);
char str = (char *)message.lastread;
payload = (String)str;
payload += " ";
str = &payload[0];
newMessageAvailable = true;
}
scrollText(str);
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT...");
uint8_t retries = 3;
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
retries--;
if (retries == 0) {
// Basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
// Check for new trigger events on the IFTTT webhook
if (WiFiMulti.run() == WL_CONNECTED) {
HTTPClient http;
String url = "IFTTT Maker Webhooks";
url += url;
http.begin(client, url);
int httpCode = http.GET();
if (httpCode > 0) {
// Trigger event was successful
// You can add code here to send a confirmation message to the Adafruit IO feed}
http.end();
}
}
In a next post I describing output of serial monitor of my project
Thank you
