Skip to content

Instantly share code, notes, and snippets.

@ttseng
Created November 1, 2019 02:53
Show Gist options
  • Save ttseng/4201a86d0d7c0b27061268aae040b090 to your computer and use it in GitHub Desktop.
Save ttseng/4201a86d0d7c0b27061268aae040b090 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <M5Stack.h>
#include <HTTPClient.h>
HTTPClient colorHttp;
HTTPClient counterHttp;
String tsData;
const char* ssid = "YOURSSID";
const char* password = "YOURPW";
const char* host = "m5stack.glitch.me";
void setup()
{
M5.begin();
WiFi.disconnect();
delay(1000);
M5.Lcd.println("START");
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(300);
}
M5.Lcd.println("connected");
M5.Lcd.println((WiFi.localIP()));
}
uint16_t getFillColor(uint8_t r, uint8_t g, uint8_t b)
{
return ((r / 8) << 11) | ((g / 4) << 5) | (b / 8);
}
void loop()
{
M5.Lcd.clear(BLACK);
M5.update();
// get the latest color
colorHttp.begin("https://m5stack.glitch.me/getColor");
int colorHttpCode = colorHttp.GET();
if(colorHttpCode > 0){
if(colorHttpCode == HTTP_CODE_OK){
String rgb = colorHttp.getString();
M5.Lcd.setTextSize(3);
M5.Lcd.println("GET " + rgb);
M5.Lcd.println("rgb");
M5.Lcd.println(rgb);
// this returns a string in the format [r, g, b];
// extra out the r g b components
int rEnd = rgb.indexOf(",");
M5.Lcd.println("rEnd " + rEnd);
int gEnd = rgb.lastIndexOf(",");
M5.Lcd.println("gEnd " + gEnd);
int r = rgb.substring(1, rEnd).toInt();
int g = rgb.substring(rEnd+1, gEnd).toInt();
int b = rgb.substring(gEnd+1, rgb.length()-1).toInt();
M5.Lcd.print("red ");
M5.Lcd.print(r);
M5.Lcd.print(" green ");
M5.Lcd.print(g);
M5.Lcd.print(" blue ");
M5.Lcd.print(b);
// convert type to uint16_t color
uint16_t color = getFillColor(r, g, b);
M5.Lcd.println("color ");
M5.Lcd.println(color);
// fill the m5Stack display with the color
M5.Lcd.fillScreen(color);
}
}else{
M5.Lcd.println(colorHttp.errorToString(colorHttpCode).c_str());
}
if(M5.BtnA.wasReleased() || M5.BtnB.wasReleased() || M5.BtnC.wasReleased()){
M5.Lcd.setTextSize(7);
M5.Lcd.println("button pressed");
// send an update to the Glitch app to increment the counter
counterHttp.begin("https://m5stack.glitch.me/updateCounter");
int counterHttpCode = counterHttp.POST("");
if(counterHttpCode > 0){
if(counterHttpCode == HTTP_CODE_OK){
// flash the screen for confirmation
M5.Lcd.clear(BLACK);
}
}else{
// error!
}
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment