Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wi-Fi Neopixel light controlled over MQTT, to be connected through HAP-NodeJS to Siri
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#define PIN 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_GRB + NEO_KHZ800);
const char* ssid = "....";
const char* password = "...";
const char* host = "officelight"; // the name of your fixture, and the base channel to listen to
IPAddress MQTTserver(192, 168, 1, 99);
/* NO NEED TO CHANGE BENEATH THIS LINE */
int hue = 0;
float brightness = 0.0;
float saturation = 0.0;
#define BUFFER_SIZE 100
WiFiClient wclient;
PubSubClient client(wclient, MQTTserver);
void callback(const MQTT::Publish& pub) {
uint16_t i, j;
currentValues();
String myMessage = String(pub.payload_string());
// handle message arrived
Serial.print(pub.topic());
Serial.print(" => ");
String myTopic = String(pub.topic());
if(myTopic == host)
{
Serial.println(pub.payload_string());
if(pub.payload_string() == "on")
{
// use this to reset parameters if you want them to always come on bright white.
//hue = 0;
brightness = 1.0;
//saturation = 0.0;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, HSVColor(hue,saturation,brightness));
}
strip.show();
}
else
{
//hue = 0;
brightness = 0.0;
//saturation = 0.0;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, HSVColor(hue,saturation,brightness));
}
strip.show();
}
}
else if(myTopic == host+(String)"/brightness")
{ // Brightness up to 100
Serial.println(pub.payload_string());
brightness = (myMessage.toFloat())/100;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, HSVColor(hue,saturation,brightness));
}
strip.show();
}
else if(myTopic == host+(String)"/hue")
{ // Hue value 0-360
Serial.println(pub.payload_string());
hue = myMessage.toInt();
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, HSVColor(hue,saturation,brightness));
}
strip.show();
}
else if(myTopic == host+(String)"/saturation")
{ // Saturation value at 0-100
Serial.println(pub.payload_string());
saturation = (myMessage.toFloat())/100;
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, HSVColor(hue,saturation,brightness));
}
strip.show();
}
currentValues();
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// MQTT callback
client.set_callback(callback);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
if (!client.connected()) {
if (client.connect("ESP8266: Fountain")) {
client.publish("outTopic",(String)"hello world, I'm "+host);
client.subscribe(host+(String)"/#");
}
}
if (client.connected())
client.loop();
}
}
// Convert Hue/Saturation/Brightness values to a packed 32-bit RBG color.
// hue must be a float value between 0 and 360
// saturation must be a float value between 0 and 1
// brightness must be a float value between 0 and 1
uint32_t HSVColor(float h, float s, float v) {
h = constrain(h, 0, 360);
s = constrain(s, 0, 1);
v = constrain(v, 0, 1);
int i, b, p, q, t;
float f;
h /= 60.0; // sector 0 to 5
i = floor( h );
f = h - i; // factorial part of h
b = v * 255;
p = v * ( 1 - s ) * 255;
q = v * ( 1 - s * f ) * 255;
t = v * ( 1 - s * ( 1 - f ) ) * 255;
switch( i ) {
case 0:
return strip.Color(b, t, p);
case 1:
return strip.Color(q, b, p);
case 2:
return strip.Color(p, b, t);
case 3:
return strip.Color(p, q, b);
case 4:
return strip.Color(t, p, b);
default:
return strip.Color(b, p, q);
}
}
void currentValues(){
Serial.println("");
Serial.println("Current State");
Serial.print("Hue (0-360):");
Serial.println(hue);
Serial.print("Saturation (0-100 in, 0-1):");
Serial.println(saturation*100);
Serial.print("Brightness (0-100):");
Serial.println(brightness*100);
Serial.println("");
}
@Ruben472

This comment has been minimized.

Copy link

commented Oct 24, 2016

Hi there,
I'm trying for days to get this code to verify on the IDE, but no good results yet...
I keep getting the same error on this line: //PubSubClient client(wclient, MQTTserver);//

Could you help me getting this code running?
thanks in advance!

this was the full error message:
`Arduino: 1.6.12 (Mac OS X), Board:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

LEDSTRIP:24: error: no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, IPAddress&)'
PubSubClient client(wclient, MQTTserver);
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:24:40: note: candidates are:
In file included from /Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:5:0:
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:116:4: note: PubSubClient::PubSubClient(const char_, uint16_t, std::function<void(char_, unsigned char_, unsigned int)>, Client&, Stream&)
PubSubClient(const char_, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:116:4: note: candidate expects 5 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:115:4: note: PubSubClient::PubSubClient(const char_, uint16_t, std::function<void(char_, unsigned char_, unsigned int)>, Client&)
PubSubClient(const char_, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:115:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:114:4: note: PubSubClient::PubSubClient(const char_, uint16_t, Client&, Stream&)
PubSubClient(const char_, uint16_t, Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:114:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:113:4: note: PubSubClient::PubSubClient(const char_, uint16_t, Client&)
PubSubClient(const char_, uint16_t, Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:113:4: note: candidate expects 3 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:112:4: note: PubSubClient::PubSubClient(uint8_t_, uint16_t, std::function<void(char_, unsigned char_, unsigned int)>, Client&, Stream&)
PubSubClient(uint8_t , uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:112:4: note: candidate expects 5 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:111:4: note: PubSubClient::PubSubClient(uint8_t_, uint16_t, std::function<void(char
, unsigned char*, unsigned int)>, Client&)
PubSubClient(uint8_t , uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:111:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:110:4: note: PubSubClient::PubSubClient(uint8_t
, uint16_t, Client&, Stream&)
PubSubClient(uint8_t , uint16_t, Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:110:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:109:4: note: PubSubClient::PubSubClient(uint8_t
, uint16_t, Client&)
PubSubClient(uint8_t , uint16_t, Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:109:4: note: candidate expects 3 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:108:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char
, unsigned char_, unsigned int)>, Client&, Stream&)
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:108:4: note: candidate expects 5 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:107:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, std::function<void(char_, unsigned char_, unsigned int)>, Client&)
PubSubClient(IPAddress, uint16_t, MQTT_CALLBACK_SIGNATURE,Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:107:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:106:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&, Stream&)
PubSubClient(IPAddress, uint16_t, Client& client, Stream&);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:106:4: note: candidate expects 4 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:105:4: note: PubSubClient::PubSubClient(IPAddress, uint16_t, Client&)
PubSubClient(IPAddress, uint16_t, Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:105:4: note: candidate expects 3 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:104:4: note: PubSubClient::PubSubClient(Client&)
PubSubClient(Client& client);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:104:4: note: candidate expects 1 argument, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:103:4: note: PubSubClient::PubSubClient()
PubSubClient();
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:103:4: note: candidate expects 0 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:83:7: note: PubSubClient::PubSubClient(const PubSubClient&)
class PubSubClient {
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:83:7: note: candidate expects 1 argument, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:83:7: note: PubSubClient::PubSubClient(PubSubClient&&)
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:83:7: note: candidate expects 1 argument, 2 provided
LEDSTRIP:27: error: 'MQTT' does not name a type
void callback(const MQTT::Publish) {
^
LEDSTRIP:27: error: expected unqualified-id before ')' token
void callback(const MQTT::Publish) {
^
LEDSTRIP:27: error: 'MQTT' does not name a type
void callback(const MQTT::Publish) {
^
LEDSTRIP:27: error: expected unqualified-id before ')' token
void callback(const MQTT::Publish) {
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino: In function 'void callback(...)':
LEDSTRIP:32: error: 'pub' was not declared in this scope
String myMessage = String(pub.payload_string());
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino: In function 'void setup()':
LEDSTRIP:120: error: 'class PubSubClient' has no member named 'set_callback'
client.set_callback(callback);
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino: In function 'void loop()':
LEDSTRIP:131: error: no matching function for call to 'PubSubClient::publish(const char [9], StringSumHelper&)'
client.publish("outTopic",(String)"hello world, I'm "+host);
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:131:67: note: candidates are:
In file included from /Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:5:0:
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:130:12: note: boolean PubSubClient::publish(const char_, const char_)
boolean publish(const char_ topic, const char* payload);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:130:12: note: no known conversion for argument 2 from 'StringSumHelper' to 'const char_'
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:131:12: note: boolean PubSubClient::publish(const char_, const char_, boolean)
boolean publish(const char_ topic, const char* payload, boolean retained);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:131:12: note: candidate expects 3 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:132:12: note: boolean PubSubClient::publish(const char_, const uint8_t_, unsigned int)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:132:12: note: candidate expects 3 arguments, 2 provided
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:133:12: note: boolean PubSubClient::publish(const char_, const uint8_t_, unsigned int, boolean)
boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:133:12: note: candidate expects 4 arguments, 2 provided
LEDSTRIP:132: error: no matching function for call to 'PubSubClient::subscribe(StringSumHelper&)'
client.subscribe(host+(String)"/#");
^
/Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:132:43: note: candidates are:
In file included from /Users/RubenLafarre/Documents/Arduino/WIFI HOMEKIT/LEDSTRIP/LEDSTRIP.ino:5:0:
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:135:12: note: boolean PubSubClient::subscribe(const char_)
boolean subscribe(const char_ topic);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:135:12: note: no known conversion for argument 1 from 'StringSumHelper' to 'const char_'
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:136:12: note: boolean PubSubClient::subscribe(const char_, uint8_t)
boolean subscribe(const char* topic, uint8_t qos);
^
/Users/RubenLafarre/Documents/Arduino/libraries/PubSubClient/src/PubSubClient.h:136:12: note: candidate expects 2 arguments, 1 provided
exit status 1
no matching function for call to 'PubSubClient::PubSubClient(WiFiClient&, IPAddress&)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.