HOME  - -  Many other Arduino "How Tos", etc, at my older site

An Xth generation Arduserver

Lessons learned
Scar tissue accumulating

(filename: ardsrv_23518_2t2h_door.htm)

This comes with all of the code to build one. Free. Cost: about $45 with two $10 sensors

Arduserver: A gizmo you plug into a USB charger. It connects to your LAN (in this case, using Wifi). And serves pages to an ordinary browser, e.g. Firefox, with reading from sensors connected to the gizmo. in this case two temperatures, two humidities, whether a door has been open (and for how long) and a simple digital "on/off" sensor.

All the software IS available, free, no tricks, in the zip file the link a moment ago gives you. That's the good news. The bad news is that this support document is not as complete as I hope it may someday be.

The system works. I won't promise that it is ALWAYS "up", but feel free to try http://mon277rr.dyndns.org:1220 (Yes, http, not https. Sorry, but the cost and hassle of running a certificate for a server inside an Arduino is more than I can stand. It won't ask you for information, so what's the risk? (Your browser or anti-malware suite may moan.) You'll need to copy/paste the URL. I haven't made it a "just click on it" link, so that there's a better chance you will have read the https/ http notes before going to the Arduserver.

You should... or would!... see a page starting...

Message from a wywtk.com Arduserver...

hhhhsttthhhhstttbcccccnnnnn
4824+2203872+25513315400882
 latter part... dcccccnnnnn  ... ccccc changes if the device is running.
 d:State of DIn / ccccc:cycles / nnnnn:how long door has been open.
 Both ccccc and nnnnn are 'clipped' to their least significant 5 digits.
 Both are counts since the Arduserver was last booted.

The two lines above are not meant for humans....

What the sensors see...

When the sensors last returned a result, the two humidities monitored were...

  48.14 and 39.43% RH

And the two temperatures monitored were...

  22.06 and 25.51 deg.C

The digital input was HIGH

Interrupts (door open) to date: 882



(And the above is only highlights of what is on the page the Arduserver returns.

If it doesn't work, that's my router's fault. Across the LAN the Aduserver is on, response is always excellent.

Before I go further...

Gloom and doom. Problems, problems.

But now we can move on...

Further introduction

The Arduserver, all by itself, is pretty cool. Couple it with a "FarWatchWatcher" (a program), and you can capture readings, save them graph them, ring bells if values go to "bad" ranges, etc, etc... automatically! (Write your own, if you don't want mine. There are no "secrets" to lock you out.)

---
Besides watching the two temperatures and humidities, this Arduserver will tell you the level present on a digital input at the time you query it. Yawn. But useful for some things. Particularly things that change slowly. Or the output of further electronics which process inputs and reduce them to a single digital signal. (For newbies reading this: All that "digital" means in this context is "something that can be in one of only two states". We often call them "on and off", or "high and low". Fundamentally, in this context, we mean "in effect connected to negative side of battery" or "in effect connected to positive side of battery". The "battery" might some other source of a voltage. You might call "the negative side" "zero volts" or "ground". It's all the same thing.

A Much Better "on"/ "off" monitor

This Arduserver, one that is running ar202, has another "sensor".

We'll go into the details in a moment.

It would be crazy to access the Arduserver every two seconds. Temperatures and humidities just don't change that often. Let's say that checking the Arduserver every five minutes is reasonable.

But suppose you wanted to "watch" whether a normally closed door was open.

Someone might easily open the door and close it again between the instants you fetched one of the "current situation snapshot"s from the Arduserver.

So here's what you do...

Get yourself an oscillator. (A thing that goes on/ off/ on/ off... )(Easily arranged, if you don't like the solution I'll present in a moment. Google "555 timer". Very simple, very inexpensive.)

Connect it to an input on the Arduino. Set that input up to trigger an interrupt service routine to count how many edges are seen.

Connect the oscillator to the input through a switch. A switch that is CLOSED when the door is OPEN (and, obviously, open when the door is closed.

Presto! The record of edges seen is a record of when the door was open, and for how long! And you don't have to arrange any tedious high frequency polling of the input, or of the webpage! You only have to adjust your thinking to "see" the cumulative count as telling you these things.

(I use a similar trick to measure rainfall and wind speed. I don't get readings of "how much this hour" or "mph"... but I can work those out from counts of "1 cc of rain collected and dumped" and "turns of the windmill-thingie.)(By the way, THIS Arduserver could be connected to devices to measure rainfall or wind speed or whatever your imagination can come up with. It isn't only for watching doors.)

So! Clear, I hope?

Forgive a little self-pride?

I'd worked out most of the above before my "Eureka" moment arrived.

I realized I wouldn't need even a simple little 555 timer.

What is the first program any of use ran in an Arduino? The little program that blinks an LED? Well... that's an oscillator!

I had more GPIO pins than I needed. (And two on-board LEDs.) I simply added a little code to ar202, and that gave me a pin that goes on/ off/ on/ off... at the frequency of my choosing. (It is done in a fancy way that avoids the "delay()" function, but that's just a bonus. (The delay() function is a blocking function. Bad.)

((ar202Connections-Lp1210556v2.png text for image))

---
A quick word about GPIO 16- Maybe I was just being dozy, but I couldn't get it to do something I thought it should. I was trying to use it as a simple digital input. I don't think I had a stray pinMode(16,OUTPUT) anywhere in my code, but when I tried to pull it low, the voltage (as monitored with meter) didn't dip very far, and other weird stuff was going on. And yes, I did study material at the excellent Adafruit page about the 8266 Feather Huzzah's pins, and their uses. (I hope you will study that too... While most of the pins can be used as simple digital inputs or outputs, something as clever as the Feather Huzzah has many things going on!

Sorry, but...

Sorry... I'd like to take you through "everything", but I've spent the time on trying to make the code good. And full of comments.

Maybe someday I'll find time to revisit this and fill it out. But there doesn't seem to be much interest in my pages, and now that I have the Arduserver I want for my own purposes, I am moving on. I hope you will overcome this limited webpage, and enjoy the sourcecode. (That's the link to download it, repeated from the place I had it above.)

I'll also repeat the link I posted earlier to ar202's predecessor. That page might well be helpful to anyone trying to use ar202, it is about a similar Arduserver I did earlier. Both are "late models", filled with lessons learnt while making many earlier Arduservers.)

Enjoy!



What follows WAS the start of this page!

Some... but not all... of what follows repeats earlier material. Sorry. Worth at least what you are paying for it! And a lot more than the time I've spent on even this sorry excuse for a page.

At this time, 5/23, I've been making Arduservers for years. The one written up here is an evolution of an earlier one, itself an evolution, etc. This one is hosted on an Adafruit Huzzah Feather 8266.(Product ID 2821), but I've done very nearly the same thing on a Sparkfun Dev Thing, too.

I've posted lots of pages about various elements of it all, and I'm rushing this out at a busy time. And from the very limited feedback I get, I wonder if anyone reads what I write... SO.... forgive me if this isn't very polished!

Roots....

This Arduserver is just the one I did in 2/23, running 'ar787-2ath312wifi', taken further.

BEWARE: I stupidly did some development work in a fork of ar787 before doing a PROPER "move" to a new serial. I hope I have changed "ar787" (the old ID) to "ar202" (the new ID) in all the right places, but be aware I may have missed one or two. (The text following either of those is "a frill". The IDs are "law".)

---
I try to write code that is easily extended. I succeeded up to a point, but there are a few bits of regrettable messiness that I failed to avoid.

I took the old code and improved one aspect, and added a feature.

The old one, as usual for an Arduino, has a "setup()" and a "loop()". No rocket science there.

The loop code Did Stuff*, and then it hit a statement saying "delay (2500)";

* "Did Stuff": That consisted mostly of...

The "delay(2500)" was to prevent the code reading the sensors hundreds of times a minute... and that is a Bad Way to accomplish that.

The new code that this page is about uses a different mechanism. The new mechanism (looking at what 'millis()' returns is better because it is not a blocking routine.

Unfortunately, I am still using "delay()" for winking the red LED. (If you see just "LED", or "MessagesLED" that's the RED LED which "comes with" the Feather Huzzah 8266, on-board.

In this extension of the earlier program, I am also using the (small) BLUE on-board LED! I will always say "BlueLED" when speaking of that. We will come to what do I use that for.

The untidy "mix" of 'delay()' and 'millis()' to time events that has crept into my code make my ski crawl. I am ashamed of it. But I think I am getting away with it.

I believe that at least I DID do a good job of writing the code in a way that makes it (relatively!) easy to change what sensors you attach to the Arduserver.

Looking at the structure of "loop()" a little more closely...

In ar202, loop(), broadly speaking, looks like what follows. The "long delay" that it mentions is the "long" interval between the times we recheck the temperature and humidity sensors, and the "simple" digital input. The program is mostly idle during those delays... but should be able to respond to an incoming request to serve up a page of HTML.

However, because they are monitored with the ISR, falling edges coming from the "the door is open" pulse stream WILL be tallied, even when other things are NOT happening! (^_^)

(PSEUDO CODE...)

void loop()

unlongRecentMillis=millis();//get current milliseconds since boot.
unintPassesSinceStrt=unintPassesSinceStrt+1;//add 1 to count of
   //passes through the loop subroutine

IF (we have NOT reached the end of the long delay) THEN....

   See if a client is asking for a page of html
   Change state of blue LED, if it is time.
      (The frequency of the square wave will not be exactly steady, alas)
   END-THEN

 ELSE (i.e. when we HAVE had the long delay)
   "Do all the other stuff"...
      Read sensors
      Rebuild page of html with the new values
      "reset" the thing that starts the timing of the next long delay
   END-ELSE

ONWARD...

The Big New Feature

Forgive me... but I am glowing in the belief that I'm implemented a new feature very cleverly.

AsI hope you understand, the Arduserver will "serve up", via a webpage, readings from some sensors.

I've added a new sensor. It will tell you how LONG (total to date) a switch has been closed. I happen to use it to see how long a door has been open. (I have a separate program which runs on a Windows machine. It "watches" the Arduserver, and, over time, draws graphs of what the sensors say.)

Here's how the new "sensor" works:

When the switch is closed, it connects...
    * the output of something that goes on/off/on/off... over and over
  ...TO...
    * a pin on the Arduino which has been set up to count how many times it "sees" the input go from High to Low.

The pin has been set up to trigger an interrupt service routine (ISR). This *is* The Way To Go for something like this.

ISRs ARE a bit arcane. But it isn't hard to copy someone else's answer, once you get your head around it all.

Ah! But! Where did I get the "thing that goes on/off/on/off over and over"???

I could have soldered up a little oscillator. Not hard.

If all that went a bit too fast, I wrote a page with the general ideas explained.

But instead, I simply programmed an output of the Arduserver to do that! (This is where I blush with self-pride at my cleverness!) I gave that role to the pin that drives the blue LED which comes as an on-board part of the Adafruit Huzzah Feather 8266.(Product ID 2821).

I have another page for you introducing the broad principles of the ISP, and of using 'millis()' for timing.

Getting the code

Earlier I said I'd give you the code for this. And I have! But the zip containing it contains a folder containing three files. Leave the names as they are, unless you know all about the Arduino IDE's weird folder/ file name rules. (You need to create a folder with the same name as that which is on the .ino file inside it. Put the .ion and the other two files in that.

The folder/ names requirement is just the usual demand of the Arduino IDE. The other two files... "Tkb23227_1stLib.cpp" and "Tkb23227_1stLib.h" give you some general sub-routines I've written and have used in the .ino.

There are some other files needed. Libraries. If they are not already present on your system, you will ALSO have to download some additional libraries to satisfy...

#include <Wire.h>//to allow use of I2C
#include "Adafruit_HTU31D.h"//for Adafruit temperature/humidity sensor.
  //Use Tools/ Manage Libraries to install. See the following if
  //  more help needed... https://github.com/adafruit/Adafruit_HTU31D
#include <ESP8266WiFi.h>

... but these are either available via "manage libraries" or from reputable public sources like the Adafruit website.

(Before any of this, you'll have to download the files for the board. They're available via the IDE's Boards Manager... A gotcha: They aren't in the to-me-obvious "Adafruit" group. They come with the "ESP8266 Boards" group. (Just use "8266" to filter the boards files offered.)

Details! Annoying details!




A few words from the sponsors...

Please get in touch if you discover flaws in this page. Please cite the page's URL. (http://wywtk.com/ardsrv/ardsrv_23518_2t2h_door).

If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.



index sitemap
What's New at the Site Advanced search
Search tool (free) provided by FreeFind... whom I've used since 2002. Happy with it, obviously!

Unlike the clever Google search engine, this one merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have three other sites, and that this search will not include them. They have their own search buttons.

My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.

My SheepdogGuides.com site.

My site at Arunet.




How to email or write this page's editor, Tom Boyd. Please cite page's URL (http://wywtk.com/ardsrv/ardsrv_23518_2t2h_door) if you write.


Valid HTML Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....