i donât get errors it blank the command from losant says it sent and the device revived it. the device does see the command and i can see the name of the command but when i try to print it to serial directly it just blank. i have tried the suggestion above before asking. those seem to be not connection issues or they didnât receive the command. below is my full code
/**
Example that connects an Adafruit Feather Huzzah to the Losant
IoT platform. This example reports state to Losant whenever a button is
pressed. It also listens for the "toggle" command to turn the LED on and off.
Copyright (c) 2016 Losant. All rights reserved.
http://losant.com
*/
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Losant.h>
#include <Adafruit_NeoPixel.h>
#define PIN 14
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(100, PIN, NEO_RGB + NEO_KHZ400);
// WiFi credentials.
char* WIFI_SSID = "";
char* WIFI_PASS = "";
// Losant credentials.
const char* LOSANT_DEVICE_ID = "";
const char* LOSANT_ACCESS_KEY = "";
const char* LOSANT_ACCESS_SECRET = "";
// Cert taken from
// https://github.com/Losant/losant-mqtt-ruby/blob/master/lib/losant_mqtt/RootCA.crt
static const char digicert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIDrzCCApegAwIBAgIQCDvgVpBCRrGhdWrJWZHHSjANBgkqhkiG9w0BAQUFADBh
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
QTAeFw0wNjExMTAwMDAwMDBaFw0zMTExMTAwMDAwMDBaMGExCzAJBgNVBAYTAlVT
MRUwEwYDVQQKEwxE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
// Set time via NTP, as required for x.509 validation
void setClock() {
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
time_t now = time(nullptr);
while (now < 8 * 3600 * 2) {
delay(500);
now = time(nullptr);
}
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
}
String msgs[6];
bool latch = false;
bool other = false;
void commandChanged() {
// Losant uses a JSON protocol. Construct the simple state object.
// { "Power_State" : true Or false }
StaticJsonDocument<200> doc;
JsonObject root = doc.to<JsonObject>();
// Send the state to Losant.
root["pattern"] = msgs[1];
root["run_time"] = msgs[3];
root["color_1"] = msgs[4];
root["color_2"] = msgs[5];
device.sendState(root);
}
void powerStateChange(bool state) {
Serial.println("State Change!");
// Losant uses a JSON protocol. Construct the simple state object.
// { "Power_State" : true Or false }
StaticJsonDocument<200> doc;
JsonObject root = doc.to<JsonObject>();
if (state == true) {
root["Power_State"] = true;
} else {
root["Power_State"] = false;
}
// Send the state to Losant.
device.sendState(root);
}
// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
Serial.print("Command received: ");
Serial.println(command->name);
latch = true;
if (strcmp(command->name, "ledCommand") == 0)
{
JsonObject& payload = *command->payload;
Serial.println("");
msgs[1] = payload["pattern"].as<String>();
msgs[2] = payload["cycles"].as<String>();
msgs[3] = payload["runTime"].as<String>();
msgs[4] = payload["color1"].as<String>();
msgs[5] = payload["color2"].as<String>();
}
//commandChanged();
}
void connect() {
// Connect to Wifi.
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Validating the SSL for for a secure connection you must
// set trust anchor, as well as set the time.
BearSSL::X509List cert(digicert);
wifiClient.setTrustAnchors(&cert);
setClock();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to Losant.
Serial.println();
Serial.print("Connecting to Losant...");
device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
// For an unsecure connection.
//device.connect(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET);
while (!device.connected()) {
delay(500);
//Serial.println(device.mqttClient.state()); // HERE
Serial.print(".");
}
Serial.println("Connected!");
}
void colorWipe(uint32_t c, uint8_t wait) {
for (uint16_t i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
device.loop();
delay(wait);
}
Serial.println(msgs[1]);
}
void colorsWipe(int wipeTime) {
latch = false;
colorWipe(strip.Color(255, 0, 0), wipeTime); // Red
colorWipe(strip.Color(255, 128, 0), wipeTime);
colorWipe(strip.Color(255, 255, 0), wipeTime);
colorWipe(strip.Color(128, 255, 0), wipeTime);
colorWipe(strip.Color(0, 255, 0), wipeTime); // Green
colorWipe(strip.Color(0, 255, 128), wipeTime);
colorWipe(strip.Color(0, 255, 255), wipeTime);
colorWipe(strip.Color(0, 128, 255), wipeTime);
colorWipe(strip.Color(0, 0, 255), wipeTime); // Blue
colorWipe(strip.Color(128, 0, 255), wipeTime);
colorWipe(strip.Color(255, 0, 255), wipeTime);
colorWipe(strip.Color(255, 0, 128), wipeTime);
}
// this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
latch = false;
uint16_t i, j;
for (j = 0; j < 256; j++) { // 15 cycles of all colors on wheel
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
device.loop();
delay(wait);
Serial.println(msgs[1] + "t");
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
void solidColor(String color) {
latch = false;
if (color == "white") {
colorWipe(strip.Color(255, 255, 255), 50);
} else if (color == "blue") {
colorWipe(strip.Color(0, 0, 255), 50);
} else if (color == "red") {
colorWipe(strip.Color(255, 0, 0), 50);
} else if (color == "green") {
colorWipe(strip.Color(0, 255, 0), 50);
} else if (color == "magenta") {
colorWipe(strip.Color(255, 0, 255), 50);
} else if (color == "yellow") {
colorWipe(strip.Color(255, 255, 0), 50);
} else if (color == "cyan") {
colorWipe(strip.Color(0, 255, 255), 50);
} else if (color == "orange") {
colorWipe(strip.Color(255, 165, 0), 50);
} else if (color == "purple") {
colorWipe(strip.Color(128, 0, 128), 50);
} else if (color == "blueviolet") {
colorWipe(strip.Color(138, 43, 226), 50);
} else if (color == "olivedrab") {
colorWipe(strip.Color(107, 142, 35), 50);
} else if (color == "olive") {
colorWipe(strip.Color(128, 128, 0), 50);
} else if (color == "teal") {
colorWipe(strip.Color(0, 128, 128), 50);
}
strip.show();
}
uint32_t colorPicker(String color) {
uint32_t colorPicked;
if (color == "white") {
colorPicked = strip.Color(255, 255, 255);
} else if (color == "blue") {
colorPicked = strip.Color(0, 0, 255);
} else if (color == "red") {
colorPicked = strip.Color(255, 0, 0);
} else if (color == "green") {
colorPicked = strip.Color(0, 255, 0);
} else if (color == "magenta") {
colorPicked = strip.Color(255, 0, 255);
} else if (color == "yellow") {
colorPicked = strip.Color(255, 255, 0);
} else if (color == "cyan") {
colorPicked = strip.Color(0, 255, 255);
} else if (color == "orange") {
colorPicked = strip.Color(255, 165, 0);
} else if (color == "purple") {
colorPicked = strip.Color(128, 0, 128);
} else if (color == "blueviolet") {
colorPicked = strip.Color(138, 43, 226);
} else if (color == "olivedrab") {
colorPicked = strip.Color(107, 142, 35);
} else if (color == "olive") {
colorPicked = strip.Color(128, 128, 0);
} else if (color == "teal") {
colorPicked = strip.Color(0, 128, 128);
}
return (colorPicked);
}
void colorChase(String background, String forground, int runTime) {
latch = false;
uint32_t forgroundColor;
uint32_t backgroundColor;
int li = 0;
int gi = 0;
forgroundColor = colorPicker(forground);
backgroundColor = colorPicker(background);
Serial.println(forground + forgroundColor);
Serial.println(background + backgroundColor);
for (int i = 0; i < strip.numPixels(); i++) {
li = i - 1;
gi = i + 1;
strip.fill(backgroundColor);
strip.show();
if (li >= 0) {
strip.setPixelColor(li, forgroundColor);
}
if (gi <= strip.numPixels()) {
strip.setPixelColor(gi, forgroundColor);
}
strip.setPixelColor(i, forgroundColor);
strip.show();
device.loop();
delay(runTime);
}
}
void colorBlink( String background, String forground, int runTime) {
latch = false;
uint32_t forgroundColor;
uint32_t backgroundColor;
forgroundColor = colorPicker(forground);
backgroundColor = colorPicker(background);
for (int i = 0; i < strip.numPixels(); i++) {
if (other) {
strip.setPixelColor(i, forgroundColor);
i++;
strip.setPixelColor(i, backgroundColor);
other = false;
} else {
strip.setPixelColor(i, backgroundColor);
i++;
strip.setPixelColor(i, forgroundColor);
other = true;
}
}
strip.show();
device.loop();
delay(runTime);
}
void setup() {
Serial.begin(115200);
while (!Serial) { }
// Register the command handler to be called when a command is received
// from the Losant platform.
device.onCommand(&handleCommand);
connect();
strip.begin();
strip.setBrightness(30); //adjust brightness here
strip.show(); // Initialize all pixels to 'off'
powerStateChange(false);
}
void loop() {
if (msgs[1] == "off") {
Serial.print("off");
powerStateChange(false);
strip.clear();
strip.show();
msgs[1] = "off1";
} else if (msgs[1] == "rainbow") {
Serial.print("rainbow");
if (latch) {
powerStateChange(true);
}
rainbowCycle(msgs[3].toInt());
} else if (msgs[1] == "solid") {
Serial.print("solid");
if (latch) {
powerStateChange(true);
}
solidColor(msgs[4]);
} else if (msgs[1] == "color chase") {
Serial.print("color chase");
if (latch) {
powerStateChange(true);
}
colorChase(msgs[4], msgs[5], msgs[3].toInt());
} else if (msgs[1] == "two color blink") {
Serial.print("two color blink");
if (latch) {
powerStateChange(true);
}
colorBlink(msgs[4], msgs[5], msgs[3].toInt());
} else if (msgs[1] == "colors wipe") {
Serial.print("color wipe");
if (latch) {
powerStateChange(true);
}
colorsWipe(msgs[3].toInt());
}
bool toReconnect = false;
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Disconnected from WiFi");
toReconnect = true;
}
if (!device.connected()) {
Serial.println("Disconnected from Losant");
//Serial.println(device.mqttClient.state());
toReconnect = true;
}
if (toReconnect) {
connect();
}
device.loop();
delay(100);
}