support Witty Wifi module, set rgb led via MQTT
This commit is contained in:
parent
84dfe25160
commit
130d2a7f77
36
things.ino
36
things.ino
@ -21,6 +21,12 @@ extern "C" {
|
|||||||
|
|
||||||
#define CONFIGPIN D3 // config-button is connected to this pin
|
#define CONFIGPIN D3 // config-button is connected to this pin
|
||||||
|
|
||||||
|
#define RGBRED D8 // red led channel on witty module
|
||||||
|
#define RGBGRN D6 // green led channel on witty module
|
||||||
|
#define RGBBLU D7 // blue led channel on witty module
|
||||||
|
|
||||||
|
#define LDRPIN A0 // analog LDR input on witty module
|
||||||
|
|
||||||
// define default values here, overwritten by values from config.json
|
// define default values here, overwritten by values from config.json
|
||||||
char mqtt_server[40];
|
char mqtt_server[40];
|
||||||
char mqtt_port[6] = "1883";
|
char mqtt_port[6] = "1883";
|
||||||
@ -129,22 +135,21 @@ void read_sensor() {
|
|||||||
|
|
||||||
// callback for MQTT, gets called if we receive a message
|
// callback for MQTT, gets called if we receive a message
|
||||||
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
|
||||||
|
char inData[length+1];
|
||||||
|
for(int i = 0; i < length; i++) {
|
||||||
|
inData[i] = (char)payload[i];
|
||||||
|
}
|
||||||
|
inData[length] = 0;
|
||||||
Serial.print("Message arrived [");
|
Serial.print("Message arrived [");
|
||||||
Serial.print(topic);
|
Serial.print(topic);
|
||||||
Serial.print("] ");
|
Serial.print("] ");
|
||||||
for (int i = 0; i < length; i++) {
|
Serial.println(inData);
|
||||||
Serial.print((char)payload[i]);
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
|
|
||||||
// switch on the LED if an 1 was received as first character
|
StaticJsonBuffer<200> jsonBuffer;
|
||||||
if ((char)payload[0] == '1') {
|
JsonObject& json = jsonBuffer.parseObject(inData);
|
||||||
Serial.println("setting LED to low");
|
analogWrite(RGBRED, json["red"]);
|
||||||
digitalWrite(BUILTIN_LED, LOW); // turn the LED on
|
analogWrite(RGBGRN, json["green"]);
|
||||||
} else {
|
analogWrite(RGBBLU, json["blue"]);
|
||||||
Serial.println("setting LED to high");
|
|
||||||
digitalWrite(BUILTIN_LED, HIGH); // turn the LED off
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure we're connected to MQTT broker
|
// make sure we're connected to MQTT broker
|
||||||
@ -158,7 +163,7 @@ void mqtt_reconnect() {
|
|||||||
// once connected, publish an announcement...
|
// once connected, publish an announcement...
|
||||||
mqtt_client.publish(topic("online"), "1", true);
|
mqtt_client.publish(topic("online"), "1", true);
|
||||||
// ... and resubscribe:
|
// ... and resubscribe:
|
||||||
mqtt_client.subscribe("inTopic");
|
mqtt_client.subscribe("setrgb");
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
Serial.print("failed, rc=");
|
||||||
Serial.print(mqtt_client.state());
|
Serial.print(mqtt_client.state());
|
||||||
@ -188,7 +193,12 @@ void setup() {
|
|||||||
pinMode(BUILTIN_LED, OUTPUT); // set led pin as output
|
pinMode(BUILTIN_LED, OUTPUT); // set led pin as output
|
||||||
ticker.attach(0.5, toggle_led); // toggle led slowly during initialization
|
ticker.attach(0.5, toggle_led); // toggle led slowly during initialization
|
||||||
|
|
||||||
|
pinMode(RGBRED, OUTPUT);
|
||||||
|
pinMode(RGBGRN, OUTPUT);
|
||||||
|
pinMode(RGBBLU, OUTPUT);
|
||||||
|
|
||||||
pinMode(CONFIGPIN, INPUT); // flash-button on nodemcu
|
pinMode(CONFIGPIN, INPUT); // flash-button on nodemcu
|
||||||
|
pinMode(LDRPIN, INPUT);
|
||||||
|
|
||||||
// clean FS, for testing
|
// clean FS, for testing
|
||||||
//SPIFFS.format();
|
//SPIFFS.format();
|
||||||
|
Loading…
Reference in New Issue
Block a user