HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers     

Environmental Monitoring Webserver

Inexpensive- apart from SHT_15 sensor

filename: aht5env-mon-ws.htm

This PAGE is poor.... not well edited, bits undeveloped.

Also, it is a very close cousin to my page about a $16+ sensor environmental monitor server based on the less expensive Si7021 humidity and temperature sensor.

And the software is only "fair". It WORKS, though, and it is free. And it runs in a Sparkfun ESP8266 (about $16), and if you don't like the sensor I've used, you can replace that element of the package.

What I was trying to do: I wanted a SIMPLE (reliable!), INEXPENSIVE (well, relatively) device which could sit on a WiFi, be accessible across the internet, and report a few environmental sensor readings. The device I envisioned (and present here) doesn't do "clever things", like draw graphs, or tell you the hottest or coldest readings in the past 24 hours, etc.

It doesn't do those things because I envision a separate computer automatically, periodically, "harvesting" readings from one or more of the simple devices described here. That computer can log and present and analyze the data. (I have in fact written programs for this... usually in Lazarus... but they are still more fit for my eyes than public scrutiny. My tutorial on fetching a page of html is all you need to get started with your own version of what I envision, if you are in a hurry.)

A Mash Up

Usually in my tutorials, I look at one process per tutorial. And I usually do a more thorough job of "explaining".

In this tutorial I have mashed together, rather crudely, two demo programs from Sparkfun, for the ESP8266 and a sensor, the SHT15. (You can replace the sensor bits with whatever you need for YOUR sensor quite easily.)

You can download the sourcecode for my ESP8266 Environmental Monitor Webserver. Be advised: It WILL NOT RUN exactly as downloaded. You need to tweak the code AT LEAST to tell it your WiFi's SSID and key ("password"). And you may well want to change the IP address the server comes up on. (It is configured to use a static IP address, because it must have one (one way or another) if you want to access the device from outside your LAN. If you get it working from INSIDE your LAN, the rest is "easy", and won't depend on anything about the ESP8266 webserver that the supplied code, with the little tweaks mentioned, creates.)

Blow by blow...

I should split bits of this out. It tells two stories, side by side...

a) Getting started with ESP8266. I will give the details for a specific one, the Sparkfun Thing Dev Board (WRL-13711.. a product distinct from their very similarly named "Sparkfun Thing"). I have other pages, much tidier, more complete, about this. But THIS page, at 8/18, is my latest "run through" the steps for a beginner, and may have peculiar charms.

b) Using the (now retired) Sparkfun Humidity and Temperature Sensor Breakout based on the SHT15 (SEN-13683)... don't worry... if you don't want to use this sensor, there is still LOTS that will be useful to you, as you adapt what is here to manage a different sensor.

Among other things, I hope you will notice how a lot of "stuff" was "wrapped up" in the "ReadSensor" procedure. (Good). Even if the code uses some global variable badly. (Bad... but we all do it, and this is one of those circumstances where the practice can be tolerated, I feel.)

Things to note....

The 8266 is not "an Arduino", strictly speaking... but it is very like one... with bonus features! And only about $16! If you want to ignore the 8266 parts of this you can. Virtually everything relating specifically to the Si7021 should work just fine with a more basic Arduino.

The SEN-13683 will operate over a range of 2.4v to 5.5v

It uses two data lines, for a digital interface. (Not I2C, but similar)

Here we go...

1 Sep 2018, I fired up a PC (Win10) on which I'd done Arduino work in the past. It had the vers 1.8.5 Arduino IDE on it. It didn't seem that I'd done any 8266 work on the machine previously... there was no entry for the 8266 in the "Tools/Board" menu.

Always set the "Board" before connecting a device to the IDE..

I went off to the Sparkfun intro to the 8266. Quote: "Installing the Add-on With the Arduino Boards Manager: With the release of Arduino 1.6.4, adding third party boards to the Arduino IDE is easily achieved through the board manager."

This has been my experience.

1 Sep 2018, Sparkfun pointed me to the following, which you use as follows.

http://arduino.esp8266.com/stable/package_esp8266com_index.json

In the Arduino IDE, open up File/ Preferences.

Add the URL ("http://arduino.esp8266...") to the "Additional Board Manager URLs" section. click OK.

Again, shamelessly just copy/pasting from Sparkfun, now you "navigate to the Board Manager by going to Tools > Boards > Boards Manager. Look for esp8266. Click the "more info" under that entry, then select Install."

(If you put "8266" in the filter at the top of the Boards Manager dialog, you'll find the right entry faster! The right thing to install 1 Sep 2018 was "esp8266 by ESP8266 Community"). I was offered vers 2.4.2, clicked Install, and it was DONE almost before I finished this paragraph.

If you went off to top up coffee while the install proceeded, look closely. There's no big "DONE!" banner... but now, up at the top of the material about the package, in a nice color and all caps, you should see "Installed". If you know to look!

Close the Boards Manager dialog. (You'll only have to do this "install package" thing once.

Use "Tools/Board" to configure the IDE for creating code for the ESP8266 you are using. Note that there are separate entries for the Sparkfun ESP8266 Thing and ESP8266 Thing Dev Board.

NOW you can plug in your Thing.

Set the IDE to "talk" to Thing on correct port. (Tools/ Port")

Check to see if there was code already in the Thing that gives info via the serial monitor when Thing boots.

Try a quick instance of "Blink" to see all is well so far. the "ordinary" blink is NEARLY what you want, but toggle the D5 line, instead of the usual D13. ('Cause that's where the Thing's LED is.) If you want to get fancy you can use "ESP8266_LED" for "5"! (A good practice, actually... but a bit "clever" for "Blink@!)

On my Sparkfun ESP8266 Thing Dev Board (WRL-13711) (quite old... from c. 1/17?) There are at least 3 tiny LEDs on the board. A red "power present" LED, a faint yellow LED that flickers during code upload, and a nice bright blue LED, "outside" the power LED, that is the one you can turn on or off with D5.

Progress!

So... including writing this, doing all of the above, it has taken me 35 minutes to get this far.

The humidity/ temperature sensor

Four connections are needed to the Arduino/8266:

The last two are the I2C communication wires. I should mention that I've done very little before now with I2C! The list above is from the point of view of the Sparkfun SHT15 breakout.

For hookup, go to...

Sparkfun page about hooking up SHT15, Sparkfun (retired product) SEN-13683

The Sparkfun 8266 Thing has two pins dedicated to SDA, SCL. The Sparkfun 8266 Thing Dev Board makes pins 2 and 14 "work overtime... serving the needs of I2C serial data (SDA) and I2C serial clock (SCL) respectively.... in addition to serving other roles, depending on the software running on the device.

The Sparkfun page discusses things in detail. My "takeaway": For the Simple Life, if other factors don't intrude, use two GPIO pins NOT affiliated with the I2C service, if you can. On my 8266, I am going to use pins 12 and 13 for the "Data" and "SCL" ("serial clock") lines, respectively.

(At this point I broke off writing this/ exploring the sensor hookup to provide my sensor with some wires to connect it to the 8266!... and to rework the above as in the process of connecting the wires I discovered I was dealing with a different sensor than I thought I was writing about. Happily, it was a SIMILAR sensor!)

Leaving out the time spend on the uncomplicated but necessary "stuff" explained in the previous paragraph, I was still only 45 minutes into this project, including writing the first draft of these notes for you, 45 minutes "in" starting from a PC which had never been used for programming an ESP8266.

Back to work...

In the previously cited hook-up guide from Sparkfun, there was, 8/18... and I expect it will remain there... a link to a library you can download and install to make working with a bunch of the (excellent and widely used) Sensirion sensors. And if "libraries" bother you, download it anyway, and look into it, reverse engineer what is in the library.

At this point, I got myself all confused. It is just an "ordinary" library. Install it in the "ordinary" way. (The Sparkfun and Github pages have notes for you.)

Just be careful when using libraries... you want to stay on top of what devices use which libraries, and where the libraries came from. And probably keep your own archive copies of the library downloads.

The following is one way "in"...

https://github.com/sparkfun/SHT15_Breakout/archive/master.zip

There are many SHT-x libraries. The one I'm using in this comes up in the IDE as "SHT_1x", and is by Jonathan Oxer and Joel Bartlett. I was using vers 1.1.0

There's more on the library at...

http://playground.arduino.cc/Code/Sensirion

... just be sure that the playground material relates to the Oxer and Bartlett library, as I believe it does, not to something similar.

ONWARD, in spite of the bad notes about installing the library we are going to use....

Reading the sensor

This... apologies for sparse notes... is how I read the sensor. Walk before you run.

I made a few changes to the demo from Sparkfun that it is based on, usually related to the fact that I hooked the four wires for the sensor to different points on the driving microprocessor.

And I don't have the software online in something you can download. It should Copy/ paste... but there are sometimes "issues" with a few characters that both the Arduino (and 8266) AND html use in special ways. DO CONTACT ME, please... save the next person... if you have any hiccups, however tiny. These pages are for beginners, remember. Please refer to the page's URL / URI: aht5env-mon-ws.htm

I present the code twice, two formats. Same code. Or should be, anyway!

/******************************************************************************
ORIGINALLY: SHT15 Example

Joel Bartlett @ Sparkfun Electronics
16 Sept 2015
Modified by TKB, 1 Sep 2018, to become...
   SHT15_on_ESP8266_Thing_Dev_Brd

https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library

This example shows how to get the temperature in
   F or C and humidity
Originally developed/Tested with:
SparkFun RedBoard
Arduino IDE 1.6.5

ORIGINAL...
Connections:
GND  -> A2
Vcc  -> A3
DATA -> A4
SCK  -> A5

TKB CONNECTIONS TO Sparkfun ESP8266 Thing Dev Board...

(breakout)  (ESP8266)
GND           GND
Vcc           3v3
DATA          (D)12
SCK           (D)13


Requires:
SHT1X Arduino Library
https://github.com/sparkfun/SHT15_Breakout/

This code is beerware.
Distributed as-is; no warranty is given.
******************************************************************************/
#include <SHT1X.h>

//variables for storing values
float tempC = 0;
float tempF = 0;
float humidity = 0;

//Create an instance of the SHT1X sensor
//ORIGINAL...  SHT1x sht15(A4, A5);//Data, SCK

SHT1x sht15(12,13);//Data, SCK for TKB version on
   //ESP8622 Thing Dev Board

//declare output pins for powering the sensor
//tkb remmed out int power = A3;
//tkb remmed out int gnd = A2;



void setup()
{
  Serial.begin(9600); // Open serial connection to
      //report values to host

//tkb remmed out   pinMode(power, OUTPUT);
//tkb remmed out   pinMode(gnd, OUTPUT);

//tkb remmed out   digitalWrite(power, HIGH);
//tkb remmed out   digitalWrite(gnd, LOW);
}//setup()
//-------------------------------------------------------------------------------------------
void loop()
{
  readSensor();
  printOut();
  delay(1000);
}
//-------------------------------------------------------------------------------------------
void readSensor()
{
  // Read values from the sensor
  tempC = sht15.readTemperatureC();
  tempF = sht15.readTemperatureF();
  humidity = sht15.readHumidity();
}//readSensor()
//-------------------------------------------------------------------------------------------
void printOut()
{
  Serial.print(" Temp = ");
  Serial.print(tempF);
  Serial.print("F, ");
  Serial.print(tempC);
  Serial.println("C");
  Serial.print(" Humidity = ");
  Serial.print(humidity);
  Serial.println("%");
}



/********************************************************* ORIGINALLY: SHT15 Example Joel Bartlett @ SparkFun Electronics 16 Sept 2015 Modified by TKB, 1 Sep 2018, to become...   SHT15_on_ESP8266_Thing_Dev_Brd https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library This example shows how to get the temperature in F or C and humidity Developed/Tested with: SparkFun RedBoard Arduino IDE 1.6.5 ORIGINAL... Connections: GND  -> A2 Vcc  -> A3 DATA -> A4 SCK  -> A5 TKB CONNECTIONS TO Sparkfun ESP8266 Thing Dev Board... (breakout)  (ESP8266) GND           GND Vcc           3v3 DATA          (D)12 SCK           (D)13 Requires: SHT1X Arduino Library https://github.com/sparkfun/SHT15_Breakout/ This code is beerware. Distributed as-is; no warranty is given. ******************************************************************************/ #include <SHT1X.h> //variables for storing values float tempC = 0; float tempF = 0; float humidity = 0; //Create an instance of the SHT1X sensor //ORIGINAL...  SHT1x sht15(A4, A5);//Data, SCK SHT1x sht15(12,13);//Data, SCK for TKB version on ESP8266 Thing Dev Board //declare output pins for powering the sensor //tkb remmed out int power = A3; //tkb remmed out int gnd = A2; void setup() {  Serial.begin(9600); // Open serial connection to report values to host //tkb remmed out   pinMode(power, OUTPUT); //tkb remmed out   pinMode(gnd, OUTPUT); //tkb remmed out   digitalWrite(power, HIGH); //tkb remmed out   digitalWrite(gnd, LOW); }//setup() //------------------------------------------------------------------------------------------- void loop() {  readSensor();  printOut();  delay(1000); } //------------------------------------------------------------------------------------------- void readSensor() {  // Read values from the sensor  tempC = sht15.readTemperatureC();  tempF = sht15.readTemperatureF();  humidity = sht15.readHumidity();   }//readSensor() //------------------------------------------------------------------------------------------- void printOut() {  Serial.print(" Temp = ");  Serial.print(tempF);  Serial.print("F, ");  Serial.print(tempC);  Serial.println("C");  Serial.print(" Humidity = ");  Serial.print(humidity);  Serial.println("%"); }

It only took a further 40 minutes to get it working. By then I was seeing readings from the sensor in the serial monitor. Just taking a deep breath, holding for a moment, and blowing that across sensor would cause a detectable humidity spike.

Onward...

We've got the ESP8266 to read the sensor. Now how about making it a webserver, with data from the sensor in the page served up?

Not a BIG deal, but there are a few tricky aspects... not least getting it to read the sensor often enough, but not too often, or at wrong time.

I'm more than out of time. Sorry I can't give you all the details... there ARE notes in the code... And it is free!....

Most of what I am going to present I learned from Sparkfun's good explanation of setting up an ESP8266 as a webserver.

a) Arduino IDE has to be set up for the 8266... see the...

http://arduino.esp8266.com/stable/package_esp8266com_index.json

... step in previous, if you skipped to here, and haven't done that yet.

The code that follows is a messy "mash-up" of the two Sparkfun examples. (Webserver and sensor reader.) Lots of Bad Stuff... but some good, too. And it does work!

Note the "unnecessary" internal documentation... it reports both to the Serial Monitor and to the served webpage the name of the "Sketch" and a version ID.

To make connecting it to the WAN easier, it has been modified to sit at a static IP address on the LAN. (At one point I was using 218... but I won't promise to keep code and text in step. That could have changed) (This approach assumes you do NOT want your DHCP to "put" it at an IP address based on MAC. Which also implies setting aside some IP addresses outside of the DHCP pool. Stories covered at length elsewhere. Get this working on LAN first. Then "go for" WAN access.

For the environmental monitor, bits of the Sparkfun server demo... the turning on and off of LED, the reading of the two inputs... not necessary. The scraps of that not fully weeded from the code.

Sorry! Not my usual standard of "polish"... but worth what you paid for it? I hope it helps, however messy it may be.

I present the code twice, two formats. Same code. Or should be, anyway!

//ESP8266WebServerSparkfunWiSht15
//TKB notes- WORKED.. and played nicely with router DHCP!
//More heavily knobbled Sparkfun... to keep pin 12 avail... and moved to a static IP
//Plus reading, reporting SHT15, also derived from Sparkfun sources.
//eSet saw as "ESP_E23D82"
//From Sparkfun, 1 Sep 2018
//https://learn.sparkfun.com/tutorials/esp8266-thing-development-board-hookup-guide/example-sketch-web-server

#include <ESP8266WiFi.h>
//#include <ESP8266mDNS.h>

const String vers="2Sept18";

//Begin large block kludged in from other Sparkfun demo...

/*ORIGINALLY: SHT15 Example

Joel Bartlett @ SparkFun Electronics
16 Sept 2015
Modified by TKB, 1 Sep 2018, to become...
   SHT15_on_ESP8266_Thing_Dev_Brd

https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library

This example shows how to get the temperature in F or C and humidity
Developed/Tested with:
SparkFun RedBoard
Arduino IDE 1.6.5


TKB CONNECTIONS TO Sparkfun ESP8266 Thing Dev Board...

(breakout)  (ESP8266)
GND           GDN
Vcc           3v3
DATA          (D)12
SCK           (D)13


Requires:
SHT1X Arduino Library
https://github.com/sparkfun/SHT15_Breakout/

This code is beerware.
Distributed as-is; no warranty is given.
*********************************************************
*/

#include <SHT1X.h>

//variables for storing values
float tempC = 0;
float tempF = 0;
float humidity = 0;

SHT1x sht15(12,13);//Data, SCK for TKB version on ESP8266 Thing Dev Board

//END large block kludged in

//////////////////////
// WiFi Definitions ** MUST BE TWEAKED FOR YOUR LOCATION ** //
//////////////////////
const char WiFiSSID[] = "xxx put YOUR SSID here and end line // "; ... without the //
const char WiFiPSK[] = "xxx put YOUR pre-shared WiKi key/ password here and end line // "; ... without the //

/////////////////////
// Pin Definitions //
/////////////////////
const int LED_PIN = 5; // Thing's onboard, green LED (TKB note: Blue on some (old) Thing Dev Boards)
const int ANALOG_PIN = A0; // The only analog pin on the Thing
//TKB Remmed out const int DIGITAL_PIN = 12; // Digital pin to be read

WiFiServer server(80);

long liLoopCount=0;
unsigned long liMillisNow;
unsigned long liMillisPrev;

void setup()
{
  initHardware();
  connectWiFi();
  server.begin();
  //setupMDNS();
  Serial.begin(9600); // Open serial connection to report
  delay(400);
  Serial.println();
  Serial.println("Welcome.. TKB Chimera of two Sparkfun demos");
  Serial.println("(Bad) name, 2 Sept 18: ESP8266WebServerSparkfunWiSht15");
  Serial.println("Version "+vers);
  Serial.println("Puts webserver reporting SHT15 tture's and humidity on LAN");
  Serial.println("Created on pc2ndEllie 1 Sep 2018 after nice eve with ML and D");
  Serial.println();
  Serial.println("Configured to connect to RR extender");
  Serial.println();
  readSensor();//establish inital values.
  liMillisPrev=millis();
  liMillisNow=liMillisPrev;
}

void loop()
{
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
   //New material added to webserver demo... if no client wants service,
   //we can Do Things now....
   if (boDoNewRead())
     {
       liLoopCount=liLoopCount+1;
       readSensor();
     }
     return;
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  Serial.println(req);
  client.flush();

  // Match the request
  int val = -1; // We'll use 'val' to keep track of both the
                // request type (read/set) and value if set.
  if (req.indexOf("/led/0") != -1)
    val = 1; // Will write LED high
  else if (req.indexOf("/led/1") != -1)
    val = 0; // Will write LED low
  else if (req.indexOf("/read") != -1)
    val = -2; // Will print pin reads
  // Otherwise request will be invalid. We'll say as much in HTML

  // Set GPIO5 according to the request
  if (val >= 0)
    digitalWrite(LED_PIN, val);

  client.flush();

  // Prepare the response. Start with the common header:
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";

  //Block that always appears. (Added to Sparkfun demo by TKB)
  s += "<br>";
  s += "ESP8266 environmental monitor of 1 Sept 18, pc2ndEllie<br>";
  s += "(Bad) name, 2 Sept 18: ESP8266WebServerSparkfunWiSht15<br><br>";
  s += "Version: "+vers+"<br>";
  s += "(On pc2ndEllie)<br><br>";
  s += String(liMillisNow);
  s +=" ms since 8266 booteed.<br>";
  s +="<br>TturC=";
  s += String(tempC);
  s +="<br>Humid=";
  s += String(humidity);
  s +="<br><br>";
  //End of TKB "always appears" block

 // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"off":"on";
  }
  else if (val == -2)
  { // If we're reading pins, print out those values:
    s += "Analog Pin = ";
    s += String(analogRead(ANALOG_PIN));
    s += "<br>"; // Go to the next line.
//    s += "Digital Pin 12 = (disabled this feature)";
//TKB Remmed out    s += String(digitalRead(DIGITAL_PIN));
  }
  else
  {
    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";
  }
  s += "</html>\n";

  // Send the response to the client
  client.print(s);
  delay(1);
  Serial.println("Client disonnected");

  // The client will actually be disconnected
  // when the function returns and 'client' object is destroyed
}

void connectWiFi()
{
  byte ledStatus = LOW;
  Serial.println();
  Serial.println("Connecting to: " + String(WiFiSSID));
  // Set WiFi mode to station (as opposed to AP or AP_STA)
  WiFi.mode(WIFI_STA);

  // WiFI.begin([ssid], [passkey]) initiates a WiFI connection
  // to the stated [ssid], using the [passkey] as a WPA, WPA2,
  // or WEP passphrase.
  WiFi.begin(WiFiSSID, WiFiPSK);


//TKB block added...
//begin block from https://forum.arduino.cc/index.php?topic=460595.0
//which allows static IP to be assigned, if it is right!
//YES: It goes AFTER the WiFi.begin

  IPAddress ip(192,168,0,218);//Needs tweaking for your requirement
  IPAddress gateway(192,168,0,1);//Needs tweaking for your environment
  IPAddress subnet(255,255,255,0);//"The usual"... but you may need to tweak
  WiFi.config(ip, gateway, subnet);

//end block from forum

  // Use the WiFi.status() function to check if the ESP8266
  // is connected to a WiFi network.
  while (WiFi.status() != WL_CONNECTED)
  {
    // Blink the LED
    digitalWrite(LED_PIN, ledStatus); // Write LED high/low
    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;

    // Delays allow the ESP8266 to perform critical tasks
    // defined outside of the sketch. These tasks include
    // setting up, and maintaining, a WiFi connection.
    delay(100);
    // Potentially infinite loops are generally dangerous.
    // Add delays -- allowing the processor to perform other
    // tasks -- wherever possible.
  }
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

/*Taken out because using static ip
void setupMDNS()
{
  // Call MDNS.begin(<domain>) to set up mDNS to point to
  // "<domain>.local"
  if (!MDNS.begin("thing"))
  {
    Serial.println("Error setting up MDNS responder!");
    while(1) {
      delay(1000);
    }
  }
  Serial.println("mDNS responder started");

}
*/

void initHardware()
{
  Serial.begin(9600);
//TKB Remmed out  pinMode(DIGITAL_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);
  // Don't need to set ANALOG_PIN as input,
  // that's all it can be.
}

void printOut()
{
  Serial.print(" Temp = ");
 // Serial.print(tempF);
 // Serial.print("F, ");
  Serial.print(tempC);
  Serial.print("C");
  Serial.print(" / Humidity = ");
  Serial.print(humidity);
  Serial.println("%");
}

void readSensor()
{
  // Read values from the sensor
  tempC = sht15.readTemperatureC();
 // tempF = sht15.readTemperatureF();
  humidity = sht15.readHumidity();
}//readSensor()

bool boDoNewRead()
{
 liMillisNow=millis();
 if ((liMillisNow-liMillisPrev>5000)||
      (liMillisNow<liMillisPrev) )//(Rolled over)
    {
      liMillisPrev=liMillisNow;
      printOut();
      return true;
    }//no ; here.. else...
    {
      return false;
    }
}//boDoRead




////////////////////// // WiFi Definitions ** MUST BE TWEAKED FOR YOUR LOCATION ** // ////////////////////// const char WiFiSSID[] = "xxx put YOUR SSID here and end line // "; ... without the //";//If changed, change Welcome message const char WiFiPSK[] = "xxx put YOUR pre-shared WiKi key/ password here and end line // "; ... without the //"; ///////////////////// // Pin Definitions // //ESP8266WebServerSparkfunWiSht15 //TKB notes- WORKED.. and played nicely with router DHCP! //More heavily knobbled Sparkfun... to keep pin 12 avail... and moved to a static IP //Plus reading, reporting SHT15, also derived from Sparkfun sources. //eSet saw as "ESP_E23D82" //From Sparkfun, 1 Sep 2018 //https://learn.sparkfun.com/tutorials/esp8266-thing-development-board-hookup-guide/example-sketch-web-server #include <ESP8266WiFi.h> //#include <ESP8266mDNS.h> const String vers="2Sept18"; //Begin large block kludged in from other Sparkfun demo... /*ORIGINALLY: SHT15 Example Joel Bartlett @ SparkFun Electronics 16 Sept 2015 Modified by TKB, 1 Sep 2018, to become...   SHT15_on_ESP8266_Thing_Dev_Brd https://github.com/sparkfun/SparkFun_ISL29125_Breakout_Arduino_Library This example shows how to get the temperature in F or C and humidity Developed/Tested with: SparkFun RedBoard Arduino IDE 1.6.5 TKB CONNECTIONS TO Sparkun ESP8266 Thing Dev Board... (breakout)  (ESP8266) GND           GDN Vcc           3v3 DATA          (D)12 SCK           (D)13 Requires: SHT1X Arduino Library https://github.com/sparkfun/SHT15_Breakout/ This code is beerware. Distributed as-is; no warranty is given. ******************************************************************************/ #include <SHT1X.h> //variables for storing values float tempC = 0; float tempF = 0; float humidity = 0; SHT1x sht15(12,13);//Data, SCK for TKB version on ESP8622 Thing Dev Board //END large block kludged in ////////////////////// // WiFi Definitions ** MUST BE TWEAKED FOR YOUR LOCATION ** // ////////////////////// const char WiFiSSID[] = "xxx put YOUR SSID here and end line // "; ... without the //";//If changed, change Welcome message const char WiFiPSK[] = "xxx put YOUR pre-shared WiKi key/ password here and end line // "; ... without the //"; ///////////////////// // Pin Definitions // ///////////////////// const int LED_PIN = 5; // Thing's onboard, green LED (TKB note: Blue on some (old) Thing Dev Boards) const int ANALOG_PIN = A0; // The only analog pin on the Thing //TKB Remmed out const int DIGITAL_PIN = 12; // Digital pin to be read WiFiServer server(80); long liLoopCount=0; unsigned long liMillisNow; unsigned long liMillisPrev; void setup() {  initHardware();  connectWiFi();  server.begin();  //setupMDNS();  Serial.begin(9600); // Open serial connection to report  delay(400);  Serial.println();  Serial.println("Welcome.. TKB Chimera of two Sparkfun demos");  Serial.println("(Bad) name, 2 Sept 18: ESP8266WebServerSparkfunWiSht15");  Serial.println("Version "+vers);  Serial.println("Puts webserver reporting SHT15 tture's and humidity on LAN");  Serial.println("Created on pc2ndEllie 1 Sep 2018 after nice eve with ML and D");  Serial.println();  Serial.println("Configured to connect to RR extender");  Serial.println();  readSensor();//establish inital values.  liMillisPrev=millis();  liMillisNow=liMillisPrev; } void loop() {  // Check if a client has connected  WiFiClient client = server.available();  if (!client) {   //New material added to webserver demo... if no client wants service,   //we can Do Things now....   if (boDoNewRead())     {       liLoopCount=liLoopCount+1;       readSensor();     }     return;  }  // Read the first line of the request  String req = client.readStringUntil('\r');  Serial.println(req);  client.flush();  // Match the request  int val = -1; // We'll use 'val' to keep track of both the                // request type (read/set) and value if set.  if (req.indexOf("/led/0") != -1)    val = 1; // Will write LED high  else if (req.indexOf("/led/1") != -1)    val = 0; // Will write LED low  else if (req.indexOf("/read") != -1)    val = -2; // Will print pin reads  // Otherwise request will be invalid. We'll say as much in HTML  // Set GPIO5 according to the request  if (val >= 0)    digitalWrite(LED_PIN, val);  client.flush();  // Prepare the response. Start with the common header:  String s = "HTTP/1.1 200 OK\r\n";  s += "Content-Type: text/html\r\n\r\n";  s += "<!DOCTYPE HTML>\r\n<html>\r\n";  //Block that always appears. (Added to Sparkfun demo by TKB)  s += "<br>";  s += "ESP8266 environmental monitor of 1 Sept 18, pc2ndEllie<br>";  s += "(Bad) name, 2 Sept 18: ESP8266WebServerSparkfunWiSht15<br><br>";  s += "Version: "+vers+"<br>";  s += "(On pc2ndEllie)<br><br>";  s += String(liMillisNow);  s +=" ms since 8266 booteed.<br>";  s +="<br>TturC=";  s += String(tempC);  s +="<br>Humid=";  s += String(humidity);  s +="<br><br>";  //End of TKB "always appears" block   // If we're setting the LED, print out a message saying we did  if (val >= 0)  {    s += "LED is now ";    s += (val)?"off":"on";  }  else if (val == -2)  { // If we're reading pins, print out those values:    s += "Analog Pin = ";    s += String(analogRead(ANALOG_PIN));    s += "<br>"; // Go to the next line. //    s += "Digital Pin 12 = (disabled this feature)"; //TKB Remmed out    s += String(digitalRead(DIGITAL_PIN));  }  else  {    s += "Invalid Request.<br> Try /led/1, /led/0, or /read.";  }  s += "</html>\n";  // Send the response to the client  client.print(s);  delay(1);  Serial.println("Client disonnected");  // The client will actually be disconnected  // when the function returns and 'client' object is detroyed } void connectWiFi() {  byte ledStatus = LOW;  Serial.println();  Serial.println("Connecting to: " + String(WiFiSSID));  // Set WiFi mode to station (as opposed to AP or AP_STA)  WiFi.mode(WIFI_STA);  // WiFI.begin([ssid], [passkey]) initiates a WiFI connection  // to the stated [ssid], using the [passkey] as a WPA, WPA2,  // or WEP passphrase.  WiFi.begin(WiFiSSID, WiFiPSK); //TKB block added... //begin block from https://forum.arduino.cc/index.php?topic=460595.0 //which allows static IP to be assigned, if it is right! //YES: It goes AFTER the WiFi.begin  IPAddress ip(192,168,0,218);//Needs tweaking for your requirement     IPAddress gateway(192,168,0,1);//Needs tweaking for your environment     IPAddress subnet(255,255,255,0);//"The usual"... but you may need to tweak     WiFi.config(ip, gateway, subnet); //end block from forum  // Use the WiFi.status() function to check if the ESP8266  // is connected to a WiFi network.  while (WiFi.status() != WL_CONNECTED)  {    // Blink the LED    digitalWrite(LED_PIN, ledStatus); // Write LED high/low    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;    // Delays allow the ESP8266 to perform critical tasks    // defined outside of the sketch. These tasks include    // setting up, and maintaining, a WiFi connection.    delay(100);    // Potentially infinite loops are generally dangerous.    // Add delays -- allowing the processor to perform other    // tasks -- wherever possible.  }  Serial.println("WiFi connected");    Serial.println("IP address: ");  Serial.println(WiFi.localIP()); } /*Taken out becaus eusing static ip void setupMDNS() {  // Call MDNS.begin(<domain>) to set up mDNS to point to  // "<domain>.local"  if (!MDNS.begin("thing"))  {    Serial.println("Error setting up MDNS responder!");    while(1) {      delay(1000);    }  }  Serial.println("mDNS responder started"); } */ void initHardware() {  Serial.begin(9600); //TKB Remmed out  pinMode(DIGITAL_PIN, INPUT_PULLUP);  pinMode(LED_PIN, OUTPUT);  digitalWrite(LED_PIN, HIGH);  // Don't need to set ANALOG_PIN as input,  // that's all it can be. } void printOut() {  Serial.print(" Temp = "); // Serial.print(tempF); // Serial.print("F, ");  Serial.print(tempC);  Serial.print("C");  Serial.print(" / Humidity = ");  Serial.print(humidity);  Serial.println("%"); } void readSensor() {  // Read values from the sensor  tempC = sht15.readTemperatureC(); // tempF = sht15.readTemperatureF();  humidity = sht15.readHumidity(); }//readSensor() bool boDoNewRead() { liMillisNow=millis(); if ((liMillisNow-liMillisPrev>5000)||      (liMillisNow<liMillisPrev) )//(Rolled over)    {      liMillisPrev=liMillisNow;      printOut();      return true;    }//no ; here.. else...    {      return false;    }    }//boDoRead


   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office/ Libre Office.

Some pages for programmers.
Using the parallel port of a Windows computer.


If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.




Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.. Material on this page © TK Boyd qDATE


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

Click here to visit the homepage of my biggest site.

Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.



To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed!


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


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

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