Skip to content

Instantly share code, notes, and snippets.

@tkeith
Created March 25, 2020 16:00
Show Gist options
  • Save tkeith/fdd523b81e72426b1e667a14f68412f2 to your computer and use it in GitHub Desktop.
Save tkeith/fdd523b81e72426b1e667a14f68412f2 to your computer and use it in GitHub Desktop.
#define WIFI_SSID ""
#define WIFI_PW ""
#define KEY ""
#define API_URL ""
#define NAME ""
#define ROLE ""
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266HTTPClient.h>
#include <Servo.h>
#include <ArduinoJson.h>
const char* ssid = WIFI_SSID;
const char* password = WIFI_PW;
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.setHostname(NAME);
ArduinoOTA.setPassword(KEY);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
Serial.println("Start updating " + type);
});
ArduinoOTA
.onEnd([]() {
Serial.println("\nEnd");
ESP.restart();
});
ArduinoOTA
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
mySetup();
}
void loop() {
ArduinoOTA.handle();
myLoop();
delay(1);
}
void myDelay(long t) {
long e = millis() + t;
while (e - millis() > 10) {
delay(1);
ArduinoOTA.handle();
}
long lastDelay = e - millis();
if (lastDelay > 0) {
delay(lastDelay);
}
}
//////////////////////////////////////////////////////////////////////////////////////////
Servo servo;
String mac;
#define PIN_BUZZER 4
#define PIN_SERVO 16
void beep(long t) {
digitalWrite(PIN_BUZZER, HIGH);
myDelay(t);
digitalWrite(PIN_BUZZER, LOW);
}
void mySetup() {
servo.attach(PIN_SERVO);
pinMode(PIN_BUZZER, OUTPUT);
}
DynamicJsonDocument post(String url, JsonDocument docOut) {
String serial;
serializeJson(docOut, serial);
HTTPClient http;
http.setTimeout(1000);
http.begin(url);
int httpCode = http.POST(serial);
String payload = http.getString();
http.end();
DynamicJsonDocument docIn(10000);
deserializeJson(docIn, payload);
return docIn;
}
void myLoop() {
Serial.println("beginning");
DynamicJsonDocument docOut(10000);
docOut["access_key"] = KEY;
docOut["role"] = ROLE;
docOut["mac"] = WiFi.macAddress();
JsonDocument docIn = post(API_URL, docOut);
for (JsonVariant step : docIn["steps"].as<JsonArray>()) {
JsonObject stepDoc = step.as<JsonObject>();
Serial.println("speed");
int speed = stepDoc["speed"].as<int>();
Serial.println(speed);
if (speed == -1) {
beep(stepDoc["duration"]);
} else {
servo.write(stepDoc["speed"]);
myDelay(stepDoc["duration"]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment