The Alpha Geek – Geeking Out

Project #10: ESP8266 Thing – Web Server – Mk03

AP Web Server

Not only can the ESP8266 connect to a WiFi network and interact with the Internet, but it can also set up a network of its own, allowing other devices to connect directly to it. This example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages to any connected client.

After uploading this sketch, find another device that you can connect to a WiFi network – phone, laptop, etc. Look for a network called “Thing-XXXX”, where XXXX is the last 2 bytes of the Thing’s MAC address.

WiFi => Yes

ESP8266 Thing XXXX

He sketch sets the network’s password to “donlucmk01”.

After connecting to your Thing’s AP network, load up a browser and point it to 192.168.4.1/read. The Thing should serve up a web page showing you its ADC and digital pin 12 readings:

Analog Pin = XXX
Digital Pin: XXX
Humidity and Temperature
Humidity: XX.XX%
Celsius: XX.XX*C
Fahrenheit: XX.XX*F

LED Green

After that, give 192.168.4.1/led/0 (No) and 192.168.4.1/led/1 (Yes) a try, and keep an eye on the Thing’s green LED while you do.

RHT03 Humidity and Temperature Sensor

The RHT03 is a low cost humidity and temperature sensor with a single wire digital interface. The sensor is calibrated and doesn’t require extra components so you can get right to measuring relative humidity and temperature.

DonLuc1901Mk02

1 x SparkFun ESP8266 Thing
1 x SparkFun FTDI Basic Breakout – 3.3V
1 x RHT03 Humidity and Temperature Sensor
3 x Jumper Wires 6″ M/M
1 x Full-Size Breadboard
1 x SparkFun Cerberus USB Cable

SparkFun ESP8266 Thing

LG1 – Digital 5
RHT – Digital 4
GND – GND
VIN – +3.3V

DonLuc1901Mk02p.ino

// ***** Don Luc Electronics *****
// Software Version Information
// Project #10: SparkFun ESP8266 Thing – AP Web Server - Mk02
// 01-02
// DonLuc1901Mk01p.ino 01-02
// SparkFun ESP8266 Thing
// AP Web Server
// RHT03 Humidity and Temperature Sensor

// Include Library Code
#include <ESP8266WiFi.h>
#include <SparkFun_RHT03.h>

// WiFi Definitions 
const char WiFiAPPSK[] = "donlucmk01";

// Pin Definitions 
const int LED_PIN = 5;                  // Thing's onboard, green LED
const int ANALOG_PIN = A0;              // The only analog pin on the Thing
const int DIGITAL_PIN = 12;             // Digital pin to be read

// WiFi
WiFiServer server(80);

// RHT Humidity and Temperature Sensor
const int RHT03_DATA_PIN = 4;           // RHT03 data pin Digital 4
RHT03 rht;                              // This creates a RTH03 object, which we'll use to interact with the sensor
float latestHumidity;
float latestTempC;
float latestTempF;
  
void loop() 
{

  // RHT03 Humidity and Temperature Sensor
  isRHT03();

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    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 = 0; // Will write LED low
  else if (req.indexOf("/led/1") != -1)
    val = 1; // Will write LED high
  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";
  // If we're setting the LED, print out a message saying we did
  if (val >= 0)
  {
    s += "LED is now ";
    s += (val)?"on":"off";
  }
  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 = ";
    s += String(digitalRead(DIGITAL_PIN));
    s += "<br>";                                       // Go to the next line.
    s += "Humidity and Temperature";
    s += "<br>";                                       // Go to the next line.    
    s += "Humidity : ";
    s += String(latestHumidity);                       // Humidity
    s += "%";
    s += "<br>"; // Go to the next line.
    s += "Celsius: ";
    s += String(latestTempC);                          //  Temperature *C
    s += "*C";
    s += "<br>"; // Go to the next line.
    s += "Fahrenheit: ";
    s += String(latestTempF);                          // Temperature *F 
    s += "*F";        
  }
  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
  
}

getRHT.ino

// RHT03 Humidity and Temperature Sensor
void isRHT03(){

  // Call rht.update() to get new humidity and temperature values from the sensor.
  int updateRet = rht.update();

  // The humidity(), tempC(), and tempF() functions can be called -- after 
  // a successful update() -- to get the last humidity and temperature value 
  latestHumidity = rht.humidity();
  latestTempC = rht.tempC();
  latestTempF = rht.tempF();
  
}

setWiFi.ino

// WiFi
void setupWiFi()
{
  
  // WiFi mode WIFI_AP
  WiFi.mode(WIFI_AP);

  // Append the last two bytes of the MAC (HEX'd) to "Thing-":
  uint8_t mac[WL_MAC_ADDR_LENGTH];
  WiFi.softAPmacAddress(mac);
  String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
                 String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
  macID.toUpperCase();
  String AP_NameString = "ESP8266 Thing " + macID;

  char AP_NameChar[AP_NameString.length() + 1];
  memset(AP_NameChar, 0, AP_NameString.length() + 1);

  for (int i=0; i<AP_NameString.length(); i++)
    AP_NameChar[i] = AP_NameString.charAt(i);

  WiFi.softAP(AP_NameChar, WiFiAPPSK);
  
}
// init Hardware
void initHardware()
{

  // Serial
  Serial.begin(115200);
  // LED Green
  pinMode(DIGITAL_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  // RHT03 Humidity and Temperature Sensor
  // Call rht.begin() to initialize the sensor and our data pin
  rht.begin(RHT03_DATA_PIN);
  
}

setup.ino

// Setup
void setup() 
{

  // Hardware
  initHardware();
  // WiFi
  setupWiFi();
  server.begin();
  
}

Don Luc

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories
Archives