Created
April 3, 2017 06:44
-
-
Save willhooi/08f34103412fdfd93257e0ad6818f462 to your computer and use it in GitHub Desktop.
Arduino sketch for Espresso Lite Workshop Plus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #include <Arduino.h> | |
#include <ESPert.h> | |
#include <Servo.h> | |
ESPert espert; | |
Servo myservo; | |
const char* mqtt_server = "mqtt.espert.io"; | |
unsigned long loopTime = 0; | |
int currentSwitch = true; | |
int pos = 90; | |
String outTopic = "ESPert/" + String(espert.info.getChipId()) + "/Status"; | |
String inTopic = "ESPert/" + String(espert.info.getChipId()) + "/Command"; | |
int currentSwitch2 = true; | |
ESPert_Button button2; | |
uint32_t neoColor[]={0xFF0000,0x008000,0x0000FF,0x00FFFF,0x800080,0x808080,0xFFFF00,0xFFC0CB}; | |
const char* host = "maker.ifttt.com"; | |
const int httpPort = 80; | |
String ifttt_key = ""; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
String strPayload = String((char*)payload).substring(0, length); | |
espert.println("Receive: " + strPayload); | |
espert.oled.clear(); | |
espert.oled.println(espert.info.getId()); | |
espert.oled.println(); | |
espert.oled.printf("[%lu]\r\n\r\n", millis()); | |
espert.oled.println("PARSE OK."); | |
if (espert.json.init(strPayload)) { | |
if (espert.json.containsKey("LED")) { | |
String value = espert.json.get("LED"); | |
if (value == "0") { | |
espert.led.off(); | |
espert.println("LED: Off"); | |
} else if (value == "1") { | |
espert.led.on(); | |
espert.println("LED: On"); | |
} else if (value == "2") { | |
if (espert.led.isOn()) { | |
espert.led.off(); | |
espert.println("LED(Toglle): off"); | |
} else { | |
espert.led.on(); | |
espert.println("LED(Toglle): On"); | |
} | |
} | |
String outString = "{\"LED\":\"" + String(espert.led.isOn() ? 1 : 0) + "\", "; | |
outString += "\"name\":\"" + String(espert.info.getId()) + "\"}"; | |
espert.println("Send...: " + outString); | |
espert.mqtt.publish(outTopic, outString); | |
} | |
if (espert.json.containsKey("Buzzer")) { | |
String value = espert.json.get("Buzzer"); | |
int l = value.toInt(); | |
for (int i = 0; i < l; i++) { | |
delay(200); | |
espert.buzzer.beep(60, 200); | |
} | |
} | |
if (espert.json.containsKey("OLED")) { | |
String value = espert.json.get("OLED"); | |
espert.oled.clear(); | |
espert.oled.println(value); | |
} | |
if (espert.json.containsKey("IFTTT")) { | |
String value = espert.json.get("IFTTT"); | |
ifttt_key = value; | |
espert.eeprom.write(150, ifttt_key); | |
String outString = "{\"IFTTT\":\"" + ifttt_key + "\", "; | |
outString += "\"name\":\"" + String(espert.info.getId()) + "\"}"; | |
espert.println("Send...: " + outString); | |
espert.mqtt.publish(outTopic, outString); | |
} | |
if (espert.json.containsKey("NEO")){ | |
String value = espert.json.get("NEO"); | |
espert.neopixel.init(12,8); | |
if (value == "R" || value == "red"){ | |
for (int i=0;i<8;i++){ | |
espert.neopixel.setPixelColor(i,ColorRed); | |
espert.neopixel.show(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorRed); | |
} else if (value =="G" || value == "green"){ | |
for (int i=0;i<8;i++){ | |
espert.neopixel.setPixelColor(i,ColorGreen); | |
espert.neopixel.show(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorGreen); | |
} else if (value =="B" || value == "blue"){ | |
for (int i=0;i<8;i++){ | |
espert.neopixel.setPixelColor(i,ColorBlue); | |
espert.neopixel.show(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorBlue); | |
} else if (value =="Y" || value == "yellow"){ | |
for (int i=0;i<8;i++){ | |
espert.neopixel.setPixelColor(i,ColorYellow); | |
espert.neopixel.show(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorYellow); | |
}else if (value =="RB" || value == "rainbow"){ | |
espert.neopixel.rainbow(); | |
} | |
else if (value =="STORM" || value == "lightningstorm"){ | |
for (int a=0; a<40; a++){ | |
espert.neopixel.setPixelColor (random(0,7),neoColor[random(0,7)]); | |
espert.neopixel.show(); | |
delay (random(30,80)); | |
espert.neopixel.off(); | |
delay (random(50,80)); | |
} | |
} | |
else if (value =="DRIZ" || value == "drizzle"){ | |
for (int a=0; a<40; a++){ | |
espert.neopixel.setColor (neoColor[random(0,7)]); | |
espert.neopixel.show(); | |
delay (random(80,120)); | |
espert.neopixel.setPixelColor (random(0,7),neoColor[random(0,7)]); | |
espert.neopixel.show(); | |
delay (random(90,120)); | |
espert.neopixel.off(); | |
delay (150); | |
} | |
} | |
else if (value =="RBW" || value == "rainbowheel"){ | |
for (int t=0;t<8;t++){ | |
for (int r=0;r<8; r++){ | |
espert.neopixel.setPixelColor(r, neoColor[r]); | |
espert.neopixel.show(); | |
delay(100); | |
espert.neopixel.clear(); | |
delay(100); | |
} | |
espert.neopixel.off(); | |
} | |
} | |
else if (value =="WHEEL" || value == "wheel"){ | |
for (int t=0; t<8;t++) { | |
for (int d=0;d<8;d++){ | |
for (int c=0; c<9; c++) { | |
espert.neopixel.setPixelColor(c,neoColor[d]); | |
espert.neopixel.show(); | |
delay (50); | |
espert.neopixel.setPixelColor(c-1,neoColor[d+1]); | |
espert.neopixel.show(); | |
delay (50); | |
} | |
for (int c=9; c>-1; c--) { | |
espert.neopixel.setPixelColor(c,neoColor[d]); | |
espert.neopixel.show(); | |
delay (50); | |
espert.neopixel.setPixelColor(c+1,neoColor[d-1]); | |
espert.neopixel.show(); | |
delay (50); | |
} | |
} | |
} | |
} | |
else if (value =="FASTW" || value == "fastwheel"){ | |
for (int t=0;t<8;t++) { | |
for (int d=0;d<8;d++){ | |
for (int c=0; c<9; c++) { | |
espert.neopixel.setPixelColor(c,neoColor[d]); | |
espert.neopixel.show(); | |
delay (20); | |
espert.neopixel.setPixelColor(c-1,neoColor[d+1]); | |
espert.neopixel.show(); | |
delay (20); | |
} | |
for (int c=9; c>-1; c--) { | |
espert.neopixel.setPixelColor(c,neoColor[d]); | |
espert.neopixel.show(); | |
delay (20); | |
espert.neopixel.setPixelColor(c+1,neoColor[d-1]); | |
espert.neopixel.show(); | |
delay (20); | |
} | |
} | |
} | |
} | |
else if (value =="RBLINK" || value == "redblink"){ | |
for (int c=0; c<20; c++){ | |
espert.neopixel.setColor(ColorRed); | |
delay (100); | |
espert.neopixel.off(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorRed); | |
} | |
else if (value =="BBLINK" || value == "blueblink"){ | |
for (int c=0; c<20; c++){ | |
espert.neopixel.setColor(ColorBlue); | |
delay (100); | |
espert.neopixel.off(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorBlue); | |
} | |
else if (value =="GBLINK" || value == "greenblink"){ | |
for (int c=0; c<20; c++){ | |
espert.neopixel.setColor(ColorGreen); | |
delay (100); | |
espert.neopixel.off(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorGreen); | |
} | |
else if (value =="YBLINK" || value == "yellowblink"){ | |
for (int c=0; c<20; c++){ | |
espert.neopixel.setColor(ColorYellow); | |
delay (100); | |
espert.neopixel.off(); | |
delay(100); | |
} | |
espert.neopixel.setColor(ColorYellow); | |
} | |
else { | |
espert.neopixel.off(); | |
} | |
} | |
if (espert.json.containsKey("CW")){ | |
String value = espert.json.get("CW"); | |
int rot=value.toInt(); | |
int cwrot=pos-rot; | |
myservo.write(cwrot); | |
delay(20); | |
} | |
if (espert.json.containsKey("CCW")){ | |
String value = espert.json.get("CCW"); | |
int rot=value.toInt(); | |
int ccwrot=pos+rot; | |
myservo.write(ccwrot); | |
delay(20); | |
} | |
} | |
else { | |
espert.oled.clear(); | |
espert.oled.println(espert.info.getId()); | |
espert.oled.println(); | |
espert.oled.printf("[%lu]\r\n\r\n", millis()); | |
espert.oled.println("PARSE FAILED."); | |
espert.neopixel.off(); | |
} | |
} | |
void setup() { | |
espert.init(); | |
delay(100); | |
//espert.buzzer.init(15); | |
espert.neopixel.init(12,8); //pin 12 assigned to neopixel led | |
myservo.attach(15); | |
myservo.write(pos); | |
espert.neopixel.off(); | |
espert.dht.init(); | |
espert.mqtt.init(mqtt_server, 1883, callback); | |
ifttt_key = espert.eeprom.read(150, 80); | |
button2.init(0); | |
espert.oled.init(); | |
delay(2000); | |
espert.println("Press USER button to switch Mode"); | |
espert.oled.clear(); | |
espert.oled.println(espert.info.getId()); | |
espert.oled.println(); | |
int mode = espert.wifi.init(); | |
if (mode == ESPERT_WIFI_MODE_CONNECT) { | |
espert.println(">>> WiFi mode: connected."); | |
espert.oled.println("WiFi: connected."); | |
espert.oled.print("IP..: "); | |
espert.oled.println(espert.wifi.getLocalIP()); | |
} else if (mode == ESPERT_WIFI_MODE_DISCONNECT) { | |
espert.println(">>> WiFi mode: disconnected."); | |
espert.oled.println("WiFi: not connected."); | |
} | |
loopTime = millis(); | |
} | |
void loop() { | |
espert.loop(); | |
if (espert.mqtt.connect()) { | |
espert.println("MQTT: Connected"); | |
espert.println("MQTT: Out Topic " + outTopic); | |
espert.mqtt.subscribe(inTopic); | |
Serial.println("MQTT: Subscribed " + inTopic); | |
} | |
bool buttonPressed = espert.button.isOn(); | |
if (buttonPressed != currentSwitch) { | |
String outString = "{\"button\":\"" + String(buttonPressed ? 1 : 0) + "\", "; | |
outString += "\"name\":\"" + String(espert.info.getId()) + "\"}"; | |
espert.println("Send...: " + outString); | |
espert.mqtt.publish(outTopic, outString); | |
currentSwitch = buttonPressed; | |
} | |
buttonPressed = button2.isOn(); | |
if (buttonPressed != currentSwitch2) { | |
espert.println("SW"); | |
if (buttonPressed) { | |
String path = "/trigger/button/with/key/" + ifttt_key; | |
espert.println(">>" + espert.wifi.postHTTP(host, path.c_str()) + "<<"); | |
} | |
currentSwitch2 = buttonPressed; | |
} | |
if (millis() - loopTime >= 5000) { | |
loopTime = millis(); | |
float t = espert.dht.getTemperature(); | |
float h = espert.dht.getHumidity(); | |
if (!isnan(t) && !isnan(h)) { | |
String outString = "{\"temperature\":\"" + String(t) + "\", "; | |
outString += "\"humidity\":\"" + String(h) + "\", "; | |
outString += "\"name\":\"" + String(espert.info.getId()) + "\"}"; | |
espert.println(outString); | |
espert.mqtt.publish(outTopic, outString); | |
// espert.oled.clear(); | |
// espert.oled.println(String("....") + millis()); | |
} | |
} | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment