Author Topic: Oak interaction via web - Garage door opener  (Read 33499 times)

tycen

  • Newbie
  • *
  • Posts: 18
Oak interaction via web - Garage door opener
« on: March 09, 2016, 04:52:35 pm »
Anyone have any suggestions for a sketch to interact with the Oak via a web interface.  I have my Oak up and running and would like to move forward with a project to use it to open/close my garage door (using a relay).  I see the ESP projects out there, but I thought I saw something about ESP sketches possibly breaking the Oak (for now).  So, the end result I'm looking for is for the Oak to host a web page I can hit from my phone (while on my wifi) where I can toggle a pin on the Oak to activate the relay controlling the garage door.

Any ideas?  Thanks!

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #1 on: March 09, 2016, 05:14:12 pm »
Unless you want to create you own web page to interact with the Oak, why not use Blynk. It offers a variety of widgets including button widgets that can interact with Oak pins (real and virtual). I've only pushed data to Blynk but I think you can easily find an example use of buttons to effect actions on the Oak side.

While Blynk support is not official yet for the Oak, you can get it to work with a simple addition to  BlynkSimpleOak.h and the inclusion of SimpleTimer.h.

See this post http://digistump.com/board/index.php/topic,2069.0.html for details. If you need more clarity on how to mod BlynkSimleOak.h just shout out. But basically if you take a look at the .h file it may be obvious.

My Blynker (sic) monitors pool temp and pushes temp data to a gauge widget and messages to a virtual LCD like "Come on in the water is fine" or "Polar Bear Club Only!". You need to sign up for a Blynk account (download the app to your phone) and get an auth token to link the Oak to the Blynk app.
« Last Edit: March 09, 2016, 05:15:55 pm by exeng »

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #2 on: March 09, 2016, 06:03:21 pm »
UPDATE: to last post. You don't need SimpleTimer.h. It's only used for periodically firing of a function in a sketch. It's not needed for a Blynk button widget controlling one of the Oak real pins.

What ever pin you control the relay from would be specified in the Blynk app button widget.

Your code could be as simple as this.
Code: [Select]
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleOak>

char auth[] = "YOURAUTHCODE";

void setup()
{
  Serial.begin(9600); // Only if using serial output
  delay(5000);
  Blynk.begin(auth);
}

void loop()
{
  Blynk.run();
}

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #3 on: March 09, 2016, 08:55:26 pm »
OK... If you would like to use Blynk and have an access code required to open your garage, here is an example I put together using button and LCD widgets on virtual pins. The buttons are on V1, V2, V3, V4 and represent digits 1 to 4 respectively. The LCD widget in on virtual pin 5 (V5). You will need to create the corresponding project with widgets on Blynk, 4 buttons, 1 LCD.
Code: [Select]
*                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example runs directly on Oaks.
 *
 * Please be sure to select Oak
 * in the Tools -> Board menu!
 *
 * Change Blynk auth token to run :)
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleOak.h>
//#include <SimpleTimer.h>

WidgetLCD lcd(V5); // Blynk LCD widget

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YOURAUTHTOKENGOESHERE";

int accessCODE[4] = {1,4,1,3}; // Modifiy to your liking and/or increase number of digits
int accessINPUT[4];
int digit = 0;

int OakLEDpin = 1;   // Oak onboard LED pin 1
BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  if (param.asInt()==1) {
    lcd.print(digit,1, "*");
    accessINPUT[digit++] = 1;
    digit = digit % 4;
    if (digit == 0)
      checkAccess();
  }
}
BLYNK_WRITE(V2) //Button Widget is writing to pin V2
{
  if (param.asInt()==1) {
    lcd.print(digit,1, "*");
    accessINPUT[digit++] = 2;
    digit = digit % 4;
    if (digit == 0)
      checkAccess();
  }
}
BLYNK_WRITE(V3) //Button Widget is writing to pin V3
{
  if (param.asInt()==1) {
    lcd.print(digit,1, "*");
    accessINPUT[digit++] = 3;
    digit = digit % 4;
    if (digit == 0)
      checkAccess();
  }
}
BLYNK_WRITE(V4) //Button Widget is writing to pin V4
{
  if (param.asInt()==1) {
    lcd.print(digit,1, "*");
    accessINPUT[digit++] = 4;
    digit = digit % 4;
    if (digit == 0)
      checkAccess();
  }
}

void checkAccess() {
  for(int i=0; i<4; i++) {
    if (accessINPUT[i] != accessCODE[i]) {
      lcd.print(0,0, "NO CIGAR!");
      lcd.print(0,1, "    ");
      digitalWrite(OakLEDpin,LOW);
      return;
    }
  }
  lcd.print(0,0, "Winner!  ");
  lcd.print(0,1, "    ");
  // Do something
  digitalWrite(OakLEDpin,HIGH);
  return;
}
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
 
  pinMode(OakLEDpin, OUTPUT);
  digitalWrite(OakLEDpin,LOW);
}

void loop() {
  Blynk.run();
}


exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #4 on: March 10, 2016, 09:11:20 am »
With regard to the last post... here is what your Blynk app could look like. Again the buttons are on V1, V2, V3, V4 and represent the digits 1 - 4 respectively. The LCD widget is on virtual pin V5.

tycen

  • Newbie
  • *
  • Posts: 18
Re: Oak interaction via web - Garage door opener
« Reply #5 on: March 10, 2016, 12:06:12 pm »
exeng - that is some great info!  Thanks for taking the time.  I had thought about Blynk (I was even a backer for their Kickstarter before Oak partnered with them).  I'll play around with it and let you know.

The Blynk route will definitely work in the short term.  However, I want it to be web based eventually for the following reasons:
- I want my wife to be able to use and I don't think she'd be interested in me installing Blynk on her phone
- I want to be able to open/close it remotely (via home VPN).  Blynk is only local connections - right?  Or maybe not.

But yeah, Blynk might definitely get me what I need in the interim.  Thanks again!

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #6 on: March 10, 2016, 12:35:51 pm »
Haven't tested it but I believe that if your Oak is connected to the internet,  you should be able to access it remotely via Blynk connected wireless or cellular.  That is if you are out and about you should be able to login to Blynk over the cell network. Blynk will know if your Oak is connected.

You could even add a sensor to indicate whether or not your garage door is open.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #7 on: March 10, 2016, 01:37:15 pm »
OK... Tested it and as long as your Oak is internet connected you can login to Blynk from anywhere and control the Oak. Doesn't matter if you get to Blynk on a wireless net or cellular. The downside is you have to be logged in to Blynk to use it. And... it's yet another app on your phone that wants access to files and camera.

Have fun and let us know how it works out for you. You got me thinking about doing the same with an added open close sensor and possibly an RTC that alerts me if it's open in the evening or for unusually long periods of time. Heck, I once went on vacation and left the garage open. My good neighbor tried to close it from in the garage but kept tripping the safety beam on the way out. He ended up nailing the thing shut and covering the block of wood (temp lock) with my trash cans.

tycen

  • Newbie
  • *
  • Posts: 18
Re: Oak interaction via web - Garage door opener
« Reply #8 on: March 10, 2016, 04:11:36 pm »
Thanks for testing it out.  Sorry if I missed it, but where do I get BlynkSimpleOak.h?

When I compile I get "fatal error: BlynkSimpleOak.h: No such file or directory"

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #9 on: March 10, 2016, 04:50:18 pm »
It lives here on my Windows system:

C:\Users\Stephen\AppData\Roaming\Arduino15\packages\digistump\hardware\oak\0.9.5\libraries\Blynk

Basically, it should have been installed with the 0.9.5 repo

You'll have to make the addition to the BlynkSimpleOak.h file that I described in a prior post which is...

Add this to your local copy...
Code: [Select]
// ADDED for Oak (by exeng) - similar to above but without args to connectWiFi()
  void connectWiFi()
    {
        BLYNK_LOG("Oak in connect WiFi");
        WiFi.begin(); // Oak is already has a configured SSID to connect to
       
        while (WiFi.status() != WL_CONNECTED) {
            ::delay(10000);  // delay 10 seconds
        }
        BLYNK_LOG("Connected to WiFi");

        IPAddress myip = WiFi.localIP();
        BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
    }
// END of ADDED for Oak (by exeng)

After these lines in the BlynkWifi class:

Code: [Select]
class BlynkWifi
    : public BlynkProtocol<BlynkArduinoClient>
{
    typedef BlynkProtocol<BlynkArduinoClient> Base;
public:
    BlynkWifi(BlynkArduinoClient& transp)
        : Base(transp)
    {}

    void connectWiFi(const char* ssid, const char* pass)
    {
        /*BLYNK_LOG("Connecting to %s", ssid);
        if (pass && strlen(pass)) {
        WiFi.begin(ssid, pass);
        } else {
        WiFi.begin(ssid);
        }*/
        while (WiFi.status() != WL_CONNECTED) {
            ::delay(500);
        }
        BLYNK_LOG("Connected to WiFi");

        IPAddress myip = WiFi.localIP();
        BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
    }

Basically the repo version was complaining about no matching function for connectWiFI() and I increased the delay in the while loop to 10 seconds.

Hope this helps.

tycen

  • Newbie
  • *
  • Posts: 18
Re: Oak interaction via web - Garage door opener
« Reply #10 on: March 10, 2016, 05:05:27 pm »
Weird, I see the file (I'm on Mac, but I can find it in the corresponding location), but it's not showing up in my libraries in the IDE and the compiling still fails. 

I'll have to dig into it more later.  I also see I have 0.9.2 in there, which I thought I hadn't installed and thought I recalled there being something bad about that - so I'll have to dig into that as well.

Thanks again for the help - I'll get that issue worked out and then try it out.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #11 on: March 10, 2016, 05:22:55 pm »
I don't think 0.9.2 has the Blynk library in it and is also missing some others that are in 0.9.5. I moved that old repos out to a save directory and did the Boards Manager install for Oak to get the 0.9.5 installed. You can't just drop it in the directory (as I found out). It only showed up after the formal Board Manager install in the IDE.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: Oak interaction via web - Garage door opener
« Reply #12 on: March 11, 2016, 07:46:05 am »
Just a heads up... Blynk will charge for widgets at some point. See my Blynk post here: http://digistump.com/board/index.php/topic,2069.msg9481.html#msg9481

tycen

  • Newbie
  • *
  • Posts: 18
Re: Oak interaction via web - Garage door opener
« Reply #13 on: March 11, 2016, 02:40:38 pm »
exeng - you're awesome!  It's totally working.  I didn't realize you were putting an access code on there, too.  Very clever.  I think I finally figured out my problem with BlynkSimpleOak.h - I had edited it in TextWrangler and I think that changed the permission on it (although the looked good on the command line.  So, I blew away my Arduino installation and libraries and reinstalled everything and then edited that file via command line.  Everything's working now and this will be a great solution for getting what I need for now.

I was a Blynk Kickstarter backer (basic level, I think) so I should be good with keeping the basic widgets at least.

So many projects are coming to mind now...

Thanks again for helping me get an early jump on using this!

tycen

  • Newbie
  • *
  • Posts: 18
Re: Oak interaction via web - Garage door opener
« Reply #14 on: March 11, 2016, 04:12:55 pm »
Can I bug you with one more question?  I tried looking for the answer myself, but am not having much luck (still new to Blynk).

If I wanted to take out the access code and just have a single button for actuating the relay - what would that look like?

Another random question - my LCD in my Blynk app has Chinese characters on the bottom - I noticed yours does too in your screenshot.  Any idea what that's about?