Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Ahork
Published

M5StickC for Pilot Hue

Pilot Phillips Hue with M5Stick.

BeginnerProtip1 hour2,769
M5StickC for Pilot Hue

Things used in this project

Hardware components

M5StickC ESP32-PICO Mini IoT Development Board
M5Stack M5StickC ESP32-PICO Mini IoT Development Board
×1
Philips hue
Philips hue
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code for M5stickC

Arduino
#include <SPI.h>
#include <WiFi.h>
#include <M5StickC.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h>


//change xxxx
char ssid[] = "xxx";        //  your network SSID (name)
char pass[] = "xxx";  // your network password
int NumberLamp = 2;
int NumLamp[] = {1, 2};
int Lamp[] = {0, 0};
String username = "xxxx";
String ServerName = "xxxx";
IPAddress server(x,x,x,x);


int status = WL_IDLE_STATUS;
int Lampactiv = 1;

int gmtOffset_sec = 0;


WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
WiFiClient client;

RTC_TimeTypeDef RTC_TimeStruct;
void updateNTP (void *pvParams);




void setup() {
  M5.begin();
  Serial.begin(9600);
     
  status = WiFi.begin(ssid, pass);
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Lcd.printf(".");
    }
  
    M5.Lcd.printf("Connected to wifi");

    pinMode(M5_BUTTON_HOME, INPUT);
    pinMode(M5_BUTTON_RST, INPUT);
    Lampactiv = 1;

    timeClient.begin();
    timeClient.setTimeOffset(gmtOffset_sec);
    xTaskCreate (updateNTP, "NTP Client", 4096, NULL, 2, NULL);
}


  void loop() {
 
    M5.Rtc.GetTime(&RTC_TimeStruct);

    String Aff ;
    // change lamp
    if(digitalRead(M5_BUTTON_RST) == LOW){
    Lampactiv++;
     if (Lampactiv>NumberLamp) {Lampactiv = 1;}
     
    if (Lamp[Lampactiv-1] == 1) 
    { Aff = "Lamp "+ String(NumLamp[Lampactiv-1]) +" : ON"; }  
    else
    { Aff = "Lamp "+ String(NumLamp[Lampactiv-1])+" : OFF"; }
    
    M5.Lcd.setRotation( 3 );
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setCursor(0, 10);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setTextSize(3);
    M5.Lcd.print(Aff );
    M5.Lcd.setCursor(0, 60);
    M5.Lcd.setTextSize(2);
    M5.Lcd.printf("%02d:%02d:%02d\n",RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

    delay(100);
    }

    // On Off Lamp
    if(digitalRead(M5_BUTTON_HOME) == LOW){
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      
      // Make a HTTP request:
      client.println("PUT /api/"+username+"/lights/"+ NumLamp[Lampactiv-1]+"/state HTTP/1.1");
      char myChar = 34;
      String maChaine;
      if (Lamp[Lampactiv-1] == 1) 
           {maChaine ="Host: "+  ServerName  +"\r\nConnection: close\r\nAccept: */*\r\nContent-Type: text/plain;charset=UTF-8\r\nContent-Length: 13\r\n\r\n{\"on\":true}\r\n";}
          else
          {maChaine ="Host: "+  ServerName  +"\r\nConnection: close\r\nAccept: */*\r\nContent-Type: text/plain;charset=UTF-8\r\nContent-Length: 14\r\n\r\n{\"on\":false}\r\n";}
     //Serial.println(maChaine);
     client.print(maChaine);     
  }
  
  
  
  M5.Lcd.setRotation( 3 );
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setCursor(0, 10);
  M5.Lcd.setTextColor(WHITE);
  M5.Lcd.setTextSize(3);
  
  if (Lamp[Lampactiv-1] == 1) 
    { Aff = "Lamp "+ String(NumLamp[Lampactiv-1]) +" : ON";
      Lamp[Lampactiv-1] = 0;
     }  
    else
    { Aff = "Lamp "+ String(NumLamp[Lampactiv-1]) +" : OFF";      
      Lamp[Lampactiv-1] = 1;}
     M5.Lcd.print(Aff);
     M5.Lcd.setCursor(0, 60);
     M5.Lcd.setTextSize(2);
     M5.Lcd.printf("%02d:%02d:%02d\n",RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);

   
  // debug 
  while (client.available()) {
    char c = client.read();
    //Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
      client.stop();  
  }
 }
 delay(200);
}

void updateNTP (void *pvParameters) {
  (void) pvParameters;

  for (;;) {
    while(!timeClient.update()) {
      timeClient.forceUpdate();
    }
       setTime (timeClient.getEpochTime ());
       time_t t = now ();
       RTC_TimeTypeDef TimeStruct;
  TimeStruct.Hours   = hour(t);
  TimeStruct.Minutes = minute(t);
  TimeStruct.Seconds = second(t);
  M5.Rtc.SetTime(&TimeStruct);
   vTaskDelay ((1000/portTICK_PERIOD_MS) * 60 * 59);  // update every 59 minutes.
    }
}

Credits

Ahork
6 projects • 5 followers
Maker

Comments