top of page

/*
  Connections 
  d1 = scl
  d2 = sda
*/

// setup for OLED1
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16


// setup for TSL2591
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "SQM_TSL2591.h"

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

SQM_TSL2591 sqm = SQM_TSL2591(2591);
void readSQM(void);

bool isServer = true;
const char* ssid     = "SkyQualityMeter";
const char* password = "orionnebula";

float t = 0.0;

AsyncWebServer server(80);

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
  <meta  name="viewport" content="width=device-width, initial-scale=1">
  <style>
    html {
     font-family: Arial;
     display: inline-block;
     margin: 0px auto;
     text-align: center;
    }
    h2 { font-size: 3.0rem; }
    p { font-size: 3.0rem; }
    .units { font-size: 1.2rem; }
    .dht-labels{
      font-size: 1.5rem;
      vertical-align:middle;
      padding-bottom: 15px;
    }
  </style>
</head>
<body>
  <h2>DIY Sky Quality Meter</h2>
  <p>
    <span class="dht-labels">SQM</span> 
    <span id="temperature">%TEMPERATURE%</span>
    <span id="units">mag./arc sec<sup class="units">2</sup></span>
  </p>
</body>
<script>
setInterval(function ( ) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("temperature").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "/t", true);
  xhttp.send();
}, 1000 ) ;

</script>
</html>)rawliteral";

// Replaces placeholder with DHT values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATURE"){
    return String(t);
  }
}


void setup() {
  Serial.begin(9600);

  if(isServer){
    Serial.print("Setting AP (Access Point)…");
    WiFi.softAP(ssid, password);

    IPAddress IP = WiFi.softAPIP();
    Serial.print("AP IP address: ");
    Serial.println(IP);

    Serial.println(WiFi.localIP());

    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send_P(200, "text/html", index_html, processor);
    });
    server.on("/t", HTTP_GET, [](AsyncWebServerRequest *request){
      request->send_P(200, "text/plain", String(t).c_str());
    });
    
    server.begin();
  }

  // pinMode(13, OUTPUT);
  if (sqm.begin()) {

    Serial.println("Found SQM (TSL) sensor");
    sqm.config.gain = TSL2591_GAIN_LOW;
    sqm.config.time = TSL2591_INTEGRATIONTIME_200MS;
    sqm.configSensor();
    sqm.showConfig();
    sqm.setCalibrationOffset(0.0);
  } else{
    Serial.println("SQM sensor not found");
}

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }else{
     Serial.println(F("found screen"));    
  }

// add a logo or other text for initialization
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(3);
  display.setCursor(40,6);
  display.print("DIY");
  display.setCursor(40,34);
  display.print("ZZZ");  
  display.display();
  delay(1000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

}

void loop() {
// digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)

  sqm.takeReading();
  // digitalWrite(13, LOW);   // turn the LED on (HIGH is the voltage level)

/*
//write to serial monitor for debugging purposes
  Serial.print("full:   "); Serial.println(sqm.full);
  Serial.print("ir:     "); Serial.println(sqm.ir);
  Serial.print("vis:    "); Serial.println(sqm.vis);
  Serial.print("mpsas:  "); Serial.print(sqm.mpsas);
  Serial.print(" +/- "); Serial.println(sqm.dmpsas);
*/

  Serial.println("======================================");

// write to OLED
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("   Mag/Arc-Sec");
  display.setTextSize(3);
  display.setCursor(0, 24);
  display.print(" ");
  display.print(sqm.mpsas);
  display.setTextSize(1);
  display.setCursor(0,48);
  display.print("  ");
  display.setCursor(0,56);
  display.print("   +/- ");
  display.print(sqm.dmpsas);
  display.display();


 

  if(isServer){
        t = sqm.mpsas;
        Serial.println(t);
  }
  delay(2000);
}

bottom of page