January 21

Weather Station: Entry 02

In this entry, I bring up the chosen platform (Huzzah), the sensor, and start on some framework. A good deal of this will be bolting tutorials and code pieces together, so there will be a great deal of outside referencing

Think of this as “I put this down for six months; gotta get back up to speed”

First, get the Huzzah up and running.  The Huzzah is supported within the Arduino ecosystem, so I followed the tutorial to install the correct libraries and board definitions, and quickly load a blink test and wifi test, to verify my tool setup and my hardware.

Next, the most basic piece: a temperature sensor.  In this case, I picked the HTU21D-F Temperature and Humidity sensor – no particular reason vs any other I2C temp sensor.  I want humidity data eventually, and I thought it’d save me I2C address space in the future.  Pulling up the demo on the Huzzah, I could verify correct libraries and functional hardware.

Next, a bit of interfacing: serving data from the Huzzah.  Normally it should not handle much at all in the case of serving webpages – it is limited in resources (memory, processing power, power in general), and the general world interface should be handled by something with much more power.  However, a quick diagnostic webpage is useful and fun.  So, I started with a code example for serving a basic webpage.  From there, a bit of code moshing between the HTU21D-T sensor demo and the webpage code, and there is now a webpage that will report the temperature and humidity of the little lash-up

// Created JourneymanWizard Creative Commons as-is 2019
// Code adaptions/copies include from Adafruit (HTU21D-F sensor https://www.adafruit.com/products/1899), 
// Webpage tutorial https://tttapa.github.io/ESP8266/Chap10%20-%20Simple%20Web%20Server.html, and others

// hardware: Huzzah ESP8266, HTU21D-F Adafruit module

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h> 
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>   // Include the WebServer library

ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'

ESP8266WebServer server(80);    // Create a webserver object that listens for HTTP request on port 80

void handleRoot();              // function prototypes for HTTP handlers
void handleNotFound();

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

String HTMLmsgstr;

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');
  Serial.println("Startup block");
  wifiMulti.addAP("YOUR SSID", "YOUR PASSWORD");   // add Wi-Fi networks you want to connect to
  //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

  Serial.print("Connecting to wifi ");
  Serial.print("...");
  int i = 0;
  while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
    delay(250);
    Serial.print('.');
  }
  Serial.println('\n');
  Serial.print("Sucess. Connected to ");
  Serial.print(WiFi.SSID());              // Tell us what network we're connected to
  Serial.print(" at ");
  Serial.println(WiFi.localIP());           // Send the IP address of the ESP8266 to the computer

  server.on("/", handleRoot);               // Call the 'handleRoot' function when a client requests URI "/"
  server.onNotFound(handleNotFound);        // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"

  server.begin();                           // Actually start the server
  Serial.println("HTTP server started");
  
  if (!htu.begin())
    Serial.println("Couldn't find HTU21D-F sensor!");
  else
	Serial.println("HTU21D-F sensor detected.");

}

void loop() {
  server.handleClient();                    // Listen for HTTP requests from clients
  //Serial.print("Temp: "); Serial.print(htu.readTemperature());
  //Serial.print("\t\tHum: "); Serial.println(htu.readHumidity());
  //delay(500);
}

void handleRoot() {
  if (!htu.begin()) {
	  HTMLmsgstr = "<p>HTU sensor not found</p>";
  } 
  else {
	  HTMLmsgstr = "<p>Temp " + String(htu.readTemperature()) + "C    %rH " + String(htu.readHumidity()) + "%</p>";
  }
  server.send(200, "text/html", HTMLmsgstr);   // Send HTTP status 200 (Ok) and send some text to the browser/client
}

void handleNotFound(){
  server.send(404, "text/plain", "404: Webpage not found"); // Send HTTP status 404 (Not Found) when there's no handler for the URI in the request
}

The next step is figuring out how to get data from the remote device to a server for storage, processing, serving, etc.  For that purpose, one method I found was to use HTML with GET/POST/PUT and the like.  Too, I found MQTT, a message broker system with IoT and other lightweight, low-bandwidth solutions in mind.  Going forward, I will pursue that path to get/receive data from the weather station.

Looking at scope, the Huzzah may not have sufficient capabilities for the final implementation. However, I’m not going to worry about scaling it until I start hitting its limits.

———-
Standard disclaimers: This is all provided as is/good luck.  I make no claims that this will work in your particular case (thought I hope it does), nor do I certify it for ANY critical or particular uses for a myriad of reasons – including the fact that while I am an Electrical Engineer by trade and practice, I have not taken any of the state licensing that certifies me as a licensed Engineer (FE/PE exam).  Any links to products or sites are not endorsements and I have not received any compensation for the links (unless otherwise stated) (tho if someone wants to send me swag, I’m down with it).  If I do link, and it’s a reference or product you found helpful, show them love.



Free to use license (unless otherwise noted). Attribution / linkback always appreciated.

Posted 2019-01-21 by JourneymanWizard in category "Uncategorized

Leave a Reply