1.5.16. fejezet, Eventually

httpevent.h

class EvtHttpListener : public EvtListener {
  protected:
  WiFiServer * _server;
  public:
	EvtHttpListener(WiFiServer *server, EvtAction t);
	WiFiClient client;
	void setupListener();
	bool isEventTriggered();
};
 
EvtHttpListener::EvtHttpListener(WiFiServer *server, EvtAction t) {
  this->_server = server;
  this->triggerAction = t;
}
 
void EvtHttpListener::setupListener() {
 
}
 
bool EvtHttpListener::isEventTriggered() {
  client = _server->available();
  return client != NULL;
}

ds18b20-nodemcu-eventually.ino

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <U8g2lib.h>
#include <Eventually.h>
#include "httpevent.h"
 
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0); 
 
#define ONE_WIRE_BUS 12 //D6 on esp2866
 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);
 
//Connect to WiFi router
const char* ssid =  "xxx";
const char* password = "xxx";
WiFiServer server(80);
int status = WL_IDLE_STATUS;
 
String header;
IPAddress ip(192, 168, 1, 78);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dnserver(192, 168, 1, 1);
 
EvtManager mgr;
 
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(2000);
 
  u8g2.begin();
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_logisoso28_tr);  // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
  u8g2.drawStr(8,29,"Init...");  // write something to the internal memory
  u8g2.sendBuffer();         // transfer internal memory to the display
 
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);  // don't continue
  }
 
  // attempt to connect to Wifi network:  
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  status = WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  WiFi.config(ip,dnserver,gateway,subnet);  
  Serial.println("Device Started");
  Serial.println("-------------------------------------");
  Serial.println("Running Temperature sensor server!");
  Serial.println("-------------------------------------");
 
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");
 
  server.begin();
  sensors.begin();
 
  mgr.addListener(new EvtHttpListener(&server, (EvtAction)onClientAvailableEvent));
  mgr.addListener(new EvtTimeListener(5000, true, (EvtAction)setDisplayEvent));
  Serial.println("Server started");
 
}
 
bool setDisplayEvent() {
  Serial.print("Display refresh...");
  char str_temp[6];
  sensors.requestTemperatures();
  dtostrf(sensors.getTempCByIndex(0), 4, 2, str_temp);
  Serial.print("Temperature:");
  Serial.println(str_temp);
 
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.drawStr(8,29,str_temp);  // write something to the internal memory
  u8g2.sendBuffer();         // transfer internal memory to the display
  return false;
}
 
bool onClientAvailableEvent(EvtHttpListener *lstn) {
  WiFiClient client = lstn->client;
  Serial.println("client available.");
  if (client) {
    Serial.println("new client");
    String currentLine = "";
    while(client.connected()){
      if (client.available()) {
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            processRequest(client,header);
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            delay(10);
            client.stop();
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");    
  }
  return false;
}
 
void processRequest(WiFiClient client, String request) {
  readTemperatureFromSensors(client);
}
 
void readTemperatureFromSensors(WiFiClient client){
  sensors.requestTemperatures();
  client.println("<html style='height: 100%;'>");
  client.println("<head><meta http-equiv=\"refresh\" content=\"5\"></head>");
  client.println("<body style='height: 100%;'>");
  client.print("<div style='width:80%;margin-top:10px;margin-left:auto;margin-right:auto;height: 100vh;font-size:28px;font-weight:bold;text-align:center;'>");
  client.print(sensors.getTempCByIndex(0));
  client.println("</div>");
  client.println("</body>");
  client.println("</html>");
  delay(10);
}
 
USE_EVENTUALLY_LOOP(mgr)