PDA_ESP_WIFI.ino 6.44 KB
Newer Older
Finn Bear's avatar
Finn Bear committed
1 2
#include <Preferences.h>
#include <WiFi.h>
Finn Bear's avatar
Finn Bear committed
3 4
#include <Wire.h>
#include <SFE_MicroOLED.h>
Finn Bear's avatar
Finn Bear committed
5 6
#include <SparkFunBME280.h>
#include <SparkFunCCS811.h>
Finn Bear's avatar
Finn Bear committed
7

Finn Bear's avatar
Finn Bear committed
8
#define REFRESH_RATE 5000
Finn Bear's avatar
Finn Bear committed
9

Finn Bear's avatar
Finn Bear committed
10 11 12
#define OLED_RESET_PIN 9
#define OLED_JUMPER_MODE 1

Finn Bear's avatar
Finn Bear committed
13 14 15 16
#define BME280_ADDRESS 0x77

#define CCS811_ADDRESS 0x5B

Finn Bear's avatar
Finn Bear committed
17 18 19 20 21
#define RFID_ADDRESS 0x7D
#define RFID_TAG_BYTES 6
#define RFID_TIME_BYTES 4
#define RFID_TOTAL_BYTES 10

Finn Bear's avatar
Finn Bear committed
22 23 24 25
Preferences preferences;

WiFiServer server(80);

Finn Bear's avatar
Finn Bear committed
26 27
MicroOLED oled(OLED_RESET_PIN, OLED_JUMPER_MODE);

Finn Bear's avatar
Finn Bear committed
28 29 30 31
BME280 bme280;

CCS811 ccs881(CCS811_ADDRESS);

Finn Bear's avatar
Finn Bear committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
void scanI2C() {
  byte error, address;
  int nDevices;

  nDevices = 0;
  for (address = 1; address < 127; address++ )
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.println("!");

      nDevices++;
    }
    else if (error == 4)
    {
      Serial.print("Unknow error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found");
}

Finn Bear's avatar
Finn Bear committed
64
void setup() {
Finn Bear's avatar
Finn Bear committed
65 66
  pinMode(LED_BUILTIN, OUTPUT);
  
Finn Bear's avatar
Finn Bear committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  Serial.begin(9600);

  Serial.println("");
  
  preferences.begin("pda", false);

  char wifiName[20];
  preferences.getString("wifi ssid 1", wifiName, sizeof(wifiName));
  Serial.print("WiFi ssid: ");
  Serial.println(wifiName);
  
  char wifiPassword[20];
  Serial.print("WiFi password length: ");
  Serial.println(preferences.getString("wifi pw 1", wifiPassword, sizeof(wifiPassword)));

  WiFi.begin(wifiName, wifiPassword);

  server.begin();

Finn Bear's avatar
Finn Bear committed
86 87
  Wire.begin();

Finn Bear's avatar
Finn Bear committed
88 89
  scanI2C();

Finn Bear's avatar
Finn Bear committed
90
  oled.begin();
Finn Bear's avatar
Finn Bear committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  bme280.settings.commInterface = I2C_MODE;
  bme280.settings.I2CAddress = BME280_ADDRESS;
  bme280.settings.runMode = 3; //Normal mode
  bme280.settings.tStandby = 0;
  bme280.settings.filter = 4;
  bme280.settings.tempOverSample = 5;
  bme280.settings.pressOverSample = 5;
  bme280.settings.humidOverSample = 5;

  delay(10);

  bme280.begin();

  ccs881.begin();
Finn Bear's avatar
Finn Bear committed
106 107

  oled.clear(ALL);
Finn Bear's avatar
Finn Bear committed
108 109
}

Finn Bear's avatar
Finn Bear committed
110 111 112 113 114 115 116 117
String readRFID() {
  byte tempTag = 0; 
  long tempTime = 0; 
  int shift = 24; 
  long time = 0; 
  String tagID; 
  
  Wire.requestFrom((uint8_t)RFID_ADDRESS, (uint8_t)RFID_TOTAL_BYTES); 
Finn Bear's avatar
Finn Bear committed
118
  for(int x = 0; x < RFID_TAG_BYTES; x++) {
Finn Bear's avatar
Finn Bear committed
119
    tempTag = Wire.read();
Finn Bear's avatar
Finn Bear committed
120
    tagID += (String)tempTag; 
Finn Bear's avatar
Finn Bear committed
121 122
  }
  
Finn Bear's avatar
Finn Bear committed
123
  for (int x = 0; x < RFID_TIME_BYTES; x++){
Finn Bear's avatar
Finn Bear committed
124 125 126 127 128 129 130 131 132
    // Time is fed a byte at a time onto the buffer so we need to put it back
    // in order when we read it. 
    tempTime = Wire.read();
    time |= (tempTime << shift); 
    shift = shift - 8; 
  }

  return tagID;
}
Finn Bear's avatar
Finn Bear committed
133

Finn Bear's avatar
Finn Bear committed
134 135
long lastUpdate = millis();

Finn Bear's avatar
Finn Bear committed
136
long scrollingToggle = 0;
Finn Bear's avatar
Finn Bear committed
137

Finn Bear's avatar
Finn Bear committed
138 139 140 141 142 143
float lastTemperature = 0;
float lastHumidity = 0;
float lastPressure = 0;
float lastCO2 = 0;
float lastTVOC = 0;

Finn Bear's avatar
Finn Bear committed
144
void loop() {
Finn Bear's avatar
Finn Bear committed
145 146 147 148 149 150 151 152 153 154 155 156
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";  
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        if (c == '\n') {
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
Finn Bear's avatar
Finn Bear committed
157 158
            client.println("Content-type:text/html");
            client.println();
Finn Bear's avatar
Finn Bear committed
159
            client.println("<script type='text/javascript'>setInterval(function() { if (document.getElementById('auto-update').checked) { location.reload(); } }, 4000);</script>");
Finn Bear's avatar
Finn Bear committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            client.println("<h1>FinnPDA</h1>");
            client.print("<p>Temperature: ");
            client.print(lastTemperature);
            client.print(" C</p>");
            client.print("<p>Humidity: ");
            client.print(lastHumidity);
            client.print(" %</p>");
            client.print("<p>Pressure: ");
            client.print(lastPressure);
            client.print(" kPa</p>");
            client.print("<p>CO2: ");
            client.print(lastCO2);
            client.print(" ppm</p>");
            client.print("<p>TVOC: ");
            client.print(lastTVOC);
            client.print(" ppb</p>");
            client.print("<p>Last update: ");
            client.print((millis() - lastUpdate) * 0.001);
            client.print(" s</p>");
Finn Bear's avatar
Finn Bear committed
179
            client.print("<input id='auto-update' type='checkbox'>");
Finn Bear's avatar
Finn Bear committed
180
            client.println();
Finn Bear's avatar
Finn Bear committed
181 182 183 184 185 186 187 188 189 190 191
            
            break;
          } else {
            currentLine = "";
          }
        }
      }
    }

    client.stop();
  }
Finn Bear's avatar
Finn Bear committed
192
  
Finn Bear's avatar
Finn Bear committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
  if (millis() > lastUpdate + REFRESH_RATE) {
    oled.clear(PAGE);
    oled.setFontType(0);
    oled.setCursor(0, 0);
    oled.print(F("FinnPDA"));
    
    oled.setCursor(0, 8);
    switch (WiFi.status()) {
      case WL_CONNECTED: {
          Serial.print("Connected to WiFi at: ");
          Serial.println(WiFi.localIP());
          String ip = WiFi.localIP().toString();
          
          if (ip.length() > 10) {
            int start = scrollingToggle % (ip.length() - 10 + 1);
            ip = ip.substring(start, start + 10);
          }
      
          oled.print(ip);
          
          break;
Finn Bear's avatar
Finn Bear committed
214
        }
Finn Bear's avatar
Finn Bear committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
      case WL_NO_SHIELD:
        oled.print("WiFi error");
        break;
      case WL_IDLE_STATUS:
        oled.print("Connecting");
        break;
      case WL_NO_SSID_AVAIL:
        oled.print("No SSID");
        break;
      case WL_CONNECTION_LOST:
        oled.print("Lost WiFi");
        break;
      case WL_DISCONNECTED:
        oled.print("No WiFi");
        break;
    }
Finn Bear's avatar
Finn Bear committed
231
    
Finn Bear's avatar
Finn Bear committed
232 233
    //oled.setCursor(0, 16);
    //oled.print(readRFID().substring(0, 10));
Finn Bear's avatar
Finn Bear committed
234 235 236 237

    lastTemperature = bme280.readTempC();
    lastHumidity = bme280.readFloatHumidity();
    lastPressure = bme280.readFloatPressure() * 0.001;
Finn Bear's avatar
Finn Bear committed
238 239 240 241
  
    oled.setCursor(0, 24);
    switch (scrollingToggle % 3) {
      case 0:
Finn Bear's avatar
Finn Bear committed
242
        oled.print(lastTemperature);
Finn Bear's avatar
Finn Bear committed
243
        oled.print(" C");
Finn Bear's avatar
Finn Bear committed
244
        break;
Finn Bear's avatar
Finn Bear committed
245
      case 1:
Finn Bear's avatar
Finn Bear committed
246
        oled.print(lastHumidity);
Finn Bear's avatar
Finn Bear committed
247 248 249
        oled.print(" %");
        break;
      case 2:
Finn Bear's avatar
Finn Bear committed
250
        oled.print(lastPressure);
Finn Bear's avatar
Finn Bear committed
251 252 253
        oled.print(" kPa");
        break;
    }
Finn Bear's avatar
Finn Bear committed
254
  
Finn Bear's avatar
Finn Bear committed
255
    ccs881.readAlgorithmResults();
Finn Bear's avatar
Finn Bear committed
256 257 258

    lastCO2 = ccs881.getCO2();
    lastTVOC = ccs881.getTVOC();
Finn Bear's avatar
Finn Bear committed
259 260 261 262
  
    oled.setCursor(0, 32);
    switch (scrollingToggle % 2) {
      case 0:
Finn Bear's avatar
Finn Bear committed
263
        oled.print(lastCO2);
Finn Bear's avatar
Finn Bear committed
264 265 266
        oled.print(" ppm");
        break;
      case 1:
Finn Bear's avatar
Finn Bear committed
267
        oled.print(lastTVOC);
Finn Bear's avatar
Finn Bear committed
268 269 270 271 272
        oled.print(" ppb");
        break;
    }
    
    oled.display();
Finn Bear's avatar
Finn Bear committed
273
  
Finn Bear's avatar
Finn Bear committed
274
    scrollingToggle++;
Finn Bear's avatar
Finn Bear committed
275

Finn Bear's avatar
Finn Bear committed
276
    lastUpdate = millis();
Finn Bear's avatar
Finn Bear committed
277
  
Finn Bear's avatar
Finn Bear committed
278 279 280 281 282 283
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
  }

  yield();
Finn Bear's avatar
Finn Bear committed
284
}