Created
March 20, 2020 15:48
-
-
Save tkeith/49a1a88976838f7874379b251eea192a to your computer and use it in GitHub Desktop.
ESP8266 code
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 <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
const char* ssid = "..."; | |
const char* password = "..."; | |
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("..."); | |
ArduinoOTA.setPassword("..."); | |
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(); | |
} | |
delay(e - millis()); | |
} | |
////////////////////////////////////////////////////////////////////////////////////////// | |
#include <ESP8266HTTPClient.h> | |
#include <Servo.h> | |
#include <ArduinoJson.h> | |
Servo servo; | |
HTTPClient http; | |
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); | |
} | |
void myLoop() { | |
Serial.println("beginning "); | |
http.setTimeout(1000); | |
http.begin("..."); | |
StaticJsonDocument<10000> doc; | |
doc["access_key"] = "..."; | |
doc["role"] = "..."; | |
doc["mac"] = WiFi.macAddress(); | |
String serial; | |
serializeJson(doc, serial); | |
int httpCode = http.POST(serial); | |
Serial.println("code: "); | |
Serial.println(httpCode); | |
String payload = http.getString(); | |
Serial.println(payload); | |
deserializeJson(doc, payload); | |
for (JsonVariant step : doc["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