reichley
Published © GPL3+

WiFi Blinking Heart Box (using an ESP8266)

Let a loved one know you're thinking about her/him whether you're upstairs or away at work.

IntermediateProtip2 hours1,892
WiFi Blinking Heart Box (using an ESP8266)

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1

Story

Read more

Code

heartbox.ino

Arduino
Please see section above on what changes need to be made to the code...
/*
blinkingheartbox by Nick Reichley 14 March 2016
using aREST and aREST_UI by Marco Schwartz and the Arduino OTA (no more serial connection uploads!) toggle on/off an LED (LED heart) attached to collector side of NPN transistor using pin #2
*/
#include <ESP8266WiFi.h> 
#include <aREST.h> 
#include <aREST_UI.h> 
#include <ESP8266mDNS.h> 
#include <WiFiUdp.h> 
#include <ArduinoOTA.h> 
aREST_UI rest = aREST_UI();
// WiFi parameters
const char* ssid = "YOUR SSID";
const char* password = "YOUR PASSWORD";
// The port to listen for incoming TCP connections
#define LISTEN_PORT           80
// Create an instance of the server
WiFiServer server(LISTEN_PORT);
void setup(void)
{
  // Start Serial
  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();
  }
  // Port defaults to 8266
  // ArduinoOTA.setPort(8266);
  // Hostname defaults to esp8266-[ChipID]
   ArduinoOTA.setHostname("heartbox");
  // No authentication by default
  // ArduinoOTA.setPassword((const char *)"123");
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\n", (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());
  // Create UI
  rest.title("Blinking Heart Box");
  rest.button(2);
  // Give name and ID to device
  rest.set_id("1");
  rest.set_name("heartbox");
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Start the server
  server.begin();
  Serial.println("Server started");
  // Print the IP address
  Serial.println(WiFi.localIP());
}
void loop() {
  ArduinoOTA.handle();
  // Handle REST calls
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while(!client.available()){
    delay(1);
  }
  rest.handle(client);
}

Credits

reichley
2 projects • 5 followers
Contact
Thanks to Ray Burnette.

Comments

Please log in or sign up to comment.