1

I found nice code for nodeMCu for controlling my Hue bulb. http://www.esp8266.com/viewtopic.php?f=11&t=4389

I changed it a bit and added two buttons ON and OFF. If I open serial monitor in sketch everything works ok. But if I try it without serial communication the lights turn on and immediately turn off. So I have to hold the button ON to keep bulb on.

Could you help me what I have to change in code?

#include <ESP8266WiFi.h>

//buttons

const int button_on = 4;
const int button_off = 13;
int buttonState_on = 0;
int buttonState_off = 0;

// Wifi Settings

const char* ssid = "********";
const char* password = "*********";

// Hue Settings

const char* bridge_ip = "192.168.0.20"; // Hue Bridge IP
const int port = 80;
String user="RQQroZ5hePrFdHHy-eBzMBv5d7Y30QZFsDW3ydw4"; // Hue Bridge user -To create a user check this page (http://support.intuilab.com/kb/connecte ... hue-lights)
String light="4";

// Commands

String hue_on="{\"on\":true}";
String hue_off="{\"on\":false}";


void setup() {
pinMode(button_on, INPUT);
pinMode(button_off, INPUT);
Serial.begin(115200);
delay(10);

// We start by connecting to a WiFi network

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected"); 
Serial.println("IP address: ");
Serial.println(WiFi.localIP());



}

void loop() {

  // read the state of the pushbutton value:
  buttonState_on = digitalRead(button_on);
    //delay(50);
  buttonState_off = digitalRead(button_off);
    //delay(50);

  if (buttonState_on == HIGH) {
    hue_control_on();}
  else if (buttonState_off == HIGH) {
    hue_control_off();
  }  

}

void hue_control_on() {
Serial.print("Connecting to ");
Serial.println(bridge_ip);

// Use WiFiClient class to create TCP connections
WiFiClient client;

if (!client.connect(bridge_ip, port)) {
Serial.println("Connection failed");
return;
}

// This will send the request to the server
client.println("PUT /api/" + user + "/lights/" + light + "/state");
client.println("Host: " + String(bridge_ip) + ":" + String(port));
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-type: text/xml; charset=\"utf-8\"");
client.print("Content-Length: ");
client.println(hue_on.length()); // PUT COMMAND HERE
client.println();
client.println(hue_on); // PUT COMMAND HERE

delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Closing connection");
}

void hue_control_off() {
Serial.print("Connecting to ");
Serial.println(bridge_ip);

// Use WiFiClient class to create TCP connections
WiFiClient client;

if (!client.connect(bridge_ip, port)) {
Serial.println("Connection failed");
return;
}

// This will send the request to the server
client.println("PUT /api/" + user + "/lights/" + light + "/state");
client.println("Host: " + String(bridge_ip) + ":" + String(port));
client.println("User-Agent: ESP8266/1.0");
client.println("Connection: close");
client.println("Content-type: text/xml; charset=\"utf-8\"");
client.print("Content-Length: ");
client.println(hue_off.length()); // PUT COMMAND HERE
client.println();
client.println(hue_off); // PUT COMMAND HERE

delay(10);

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("Closing connection");
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
kalu1
  • 53
  • 1
  • 8

0 Answers0