Thinger.io
Published © MIT

Smart Lamp with IKEA Lampan, Sonoff and Thinger.io

In this project, we show how to integrate a Sonoff with Thinger. io and how to wire it up to create a simple IoT lamp for less than $10.

BeginnerWork in progress4 hours4,357
Smart Lamp with IKEA Lampan, Sonoff and Thinger.io

Things used in this project

Hardware components

IKEA Lampan
×1
Sonoff Basic
Itead Sonoff Basic
×1
SparkFun USB to Serial Breakout - FT232RL
SparkFun USB to Serial Breakout - FT232RL
×1

Software apps and online services

Thinger.io Platform
Thinger.io Platform
Arduino IDE
Arduino IDE
Maker service
IFTTT Maker service
Actions on Google
Actions on Google

Hand tools and fabrication machines

screwdriver

Story

Read more

Code

Untitled file

Arduino
#define THINGER_DEVICE_SSID "SonoffBasic"
#define THINGER_DEVICE_SSID_PSWD "SonoffBasic"
#define _DEBUG_

// Requires WifiManager from Library Manager or https://github.com/tzapu/WiFiManager
#include <ThingerWebConfig.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Ticker.h>

#define BUTTON_PIN 0
#define RELAY_PIN 12
#define LED_PIN 13

ThingerWebConfig thing;
Ticker ticker;

unsigned long millisSinceChange = 0;

void toggleWiFiStatus(){
  digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}

void setup() {
  Serial.begin(115200);

  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);

  digitalWrite(RELAY_PIN, LOW); // LIGH off
  digitalWrite(LED_PIN, HIGH); //LED off.

  // define thinger resources for the touch
  thing["light"] << digitalPin(RELAY_PIN);

  // atach interrupt to detect switch changes
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonChangeCallback, CHANGE);

  // set OTA hostname
  char hostname[15];
  sprintf(hostname, "sonoff-%06x", ESP.getChipId());
  ArduinoOTA.setHostname(hostname);

  // set a basic OTA password
  ArduinoOTA.setPassword("sonoff");

  // show progress while upgrading flash
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    toggleWiFiStatus();
  });

  // init arduino OTA
  ArduinoOTA.begin();

  ticker.attach(1, toggleWiFiStatus);
}

void buttonChangeCallback() {
  unsigned long currentMillis = millis();
  if (digitalRead(BUTTON_PIN) == 1) {
    // >10s, clean both thinger and wifi information
    if (currentMillis - millisSinceChange > 10000) {
      WiFi.disconnect();
      thing.clean_credentials();
      ESP.reset();
    // >5s, clean only wifi information
    }else if(currentMillis - millisSinceChange > 5000){
      WiFi.disconnect();
      ESP.reset();
    }
    // else, toggle relay
    else if (currentMillis - millisSinceChange > 100) {
      digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));
    }
  }
  millisSinceChange = currentMillis;
}

void loop() {
  // handle connection
  thing.handle();

  // keep light on while connected
  digitalWrite(LED_PIN, LOW); //LED ON on WifiConnect

  // handle OTA
  ArduinoOTA.handle();
}

Thinger.io Sonoff example code

Credits

Thinger.io

Thinger.io

3 projects • 8 followers

Comments