Hi , guys
currently , I’m making a simple project controlling a neopixel by photon via adafruit.io dashboard
but the code not working and I can’t understand how to fix it : here is my code :
I know that photon connects to wifi on its own so that I don’t need to create a client but the
adafruit’s code needs one , I don’t know if i got this well or not , so any ideas , help ?
By the way this was a code for esp but I wanted to modify it so it can fit in photon
// This #include statement was automatically added by the Particle IDE.
#include <TlsTcpClient.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_MQTT.h>
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include "Particle.h"
/************************* WiFi Access Point *********************************/
SYSTEM_MODE(AUTOMATIC);
//#define WLAN_SSID "SSID"
//#define WLAN_PASS "password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 //1883 is the standard port for MQTT // use 8883 for SSL
#define AIO_USERNAME "User Name"
#define AIO_KEY "AIO KEY"
/************ Global State (you don't need to change this!) ******************/
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
TCPClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_SPARK mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
//neopixel
#define PIXEL_PIN D2
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Adafruit_MQTT_Subscribe r-value = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/r-value");
Adafruit_MQTT_Subscribe g-value = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/g-value");
Adafruit_MQTT_Subscribe b-value = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/b-value");
void MQTT_connect();
//vars
int r = 0 , g = 0 , b = 0 ;
void setup() {
/* WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
*/
//subscribtion
mqtt.subscribe(&r-value);
mqtt.subscribe(&g-value);
mqtt.subscribe(&b-value);
//NEO
strip.begin();
strip.setPixelColor(0, pixels.Color( r,g,b ));
strip.show();
}
void loop() {
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &r-value) {
r = r-value.lastread
}
if (subscription == &g-value) {
g = g-value.lastread
}
if (subscription == &b-value) {
b = b-value.lastread
}
strip.setPixelColor(0, strip.Color( r,g,b ));
strip.show();
}
}
BTW, I’m pretty sure there are no minus signs (-) allowed in variable names.
Hence I guess you get compiler errors and hence posting these error messages would be the least to do in order to explain the nature of your problem.
I think the problem is , I can’t use adafruit mqtt library as with esp
I can’t understand the differences , I can’t find a good a example to follow .
also , I think there are some missing concepts with moving from arduino to photon I didn’t know yet
I know this isn’t much help, but can you try choosing the MQTT library again and installing it on your app for Photon. Sometimes I forget to re-load a library, especially if I’m copying code from a previous app or snippet. Even though it says the library is there, it may not actually be getting it on the Photon. If that doesn’t help, I would post on Adafruit’s forum.
There are more things to do to your code.
Some of these could be found by looking at the examples that come with the libraries.
e.g. add this
#include <Adafruit_MQTT_SPARK.h>
(This is some design flaw in the lib, but can be dealt with this way)
With that you will get a step further and the errors you’ll be getting then should not pose too much of trouble
Update:
Looking beyond these errors, it seems that not even the Adafruit_MQTT sample builds, so I guess this lib needs some major overhaul, but since the source repo has disappeared it seems unlikely this lib will ever get repaired.
I could solve the problem partially : what misses it transforming the subscription to integer here is the code P.S. checks @Zengirl2 comment before flashing this code
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_MQTT.h>
#include "Adafruit_MQTT_SPARK.h"
#include "Adafruit_MQTT.h"
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
/************************* WiFi Access Point *********************************/
SYSTEM_MODE(AUTOMATIC);
//#define WLAN_SSID "SSID"
//#define WLAN_PASS "password"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
/************ Global State (you don't need to change this!) ******************/
TCPClient TheClient;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_SPARK mqtt(&TheClient,AIO_SERVER,AIO_SERVERPORT,AIO_USERNAME,AIO_KEY);
//neopixel
#define PIXEL_PIN D2
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
Adafruit_MQTT_Subscribe rvalue = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/r-value");
Adafruit_MQTT_Subscribe gvalue = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/g-value");
Adafruit_MQTT_Subscribe bvalue = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/b-value");
//vars
int r = 0 , g = 0 , b = 0 ;
void setup() {
/* WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
*/
//subscribtion
mqtt.subscribe(&rvalue);
mqtt.subscribe(&gvalue);
mqtt.subscribe(&bvalue);
//NEO
strip.begin();
strip.setPixelColor(0, strip.Color( r,g,b ));
strip.show();
}
void loop() {
if( mqtt.Update() )
{
// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000)))
{
if (subscription == &rvalue) {
r = (int)rvalue.lastread ;
}
if (subscription == &gvalue) {
g = (int)gvalue.lastread ;
}
if (subscription == &bvalue) {
b = (int)bvalue.lastread ;
}
}
strip.setPixelColor(0, strip.Color( r,g,b ));
strip.show();
// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping())
{
mqtt.disconnect();
}
*/
}
}
It is very important to know how the raw return values are made up to properly translate the binary package into the correct representation for your code.