Created
December 12, 2023 08:56
-
-
Save shyney7/40c350b71d7ff53089fe29a2be39a36d to your computer and use it in GitHub Desktop.
conflicts with a previous declaration typedef File32 File;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
WebSerialLite Demo AP | |
------ | |
This example code works for both ESP8266 & ESP32 Microcontrollers | |
WebSerial is accessible at 192.168.4.1/webserial URL. | |
Author: HomeboyC | |
*/ | |
#include <Arduino.h> | |
#if defined(ESP8266) | |
#include <ESP8266WiFi.h> | |
#include <ESPAsyncTCP.h> | |
#elif defined(ESP32) | |
#include <WiFi.h> | |
#include <AsyncTCP.h> | |
#endif | |
#include <ESPAsyncWebServer.h> | |
#include <WebSerialLite.h> | |
#include <SPI.h> | |
#include <TimeLib.h> | |
#include <RtcDS3231.h> | |
#include <SdFat.h> | |
struct __attribute__((__packed__)) nrfData { | |
float pm1; | |
float pm25; | |
float pm10; | |
float sumBins; | |
float temp; | |
float altitude; | |
float hum; | |
float xtra; | |
float co2; | |
uint16_t year; | |
uint8_t month; | |
uint8_t day; | |
uint8_t hour; | |
uint8_t minute; | |
uint8_t second; | |
double lat; | |
double lng; | |
double heading; | |
}data; | |
//SD Card | |
const uint8_t SD_CS_PIN = D8; | |
// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur. | |
#define SPI_CLOCK SD_SCK_MHZ(50) | |
// Try to select the best SD card configuration. | |
#if HAS_SDIO_CLASS | |
#define SD_CONFIG SdioConfig(FIFO_SDIO) | |
#elif ENABLE_DEDICATED_SPI | |
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK) | |
#else // HAS_SDIO_CLASS | |
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK) | |
#endif // HAS_SDIO_CLASS | |
SdFat32 sd; | |
File32 file; | |
unsigned long epochTime = 0; | |
unsigned long previousMillis = 0; | |
unsigned long printTimeTimer = 0; | |
float tzDiff = 1; | |
void printUTCTime() { | |
//unsigned long t = epochTime; | |
//char buff[32]; | |
//sprintf(buff, "%02d.%02d.%02d %02d:%02d:%02d", day(t), month(t), year(t), hour(t), minute(t), second(t)); | |
WebSerial.printf("%02d.%02d.%02d %02d:%02d:%02d\n", day(epochTime), month(epochTime), year(epochTime), hour(epochTime), minute(epochTime), second(epochTime)); | |
} | |
void initData(nrfData &data) { | |
data.pm1 = 0; | |
data.pm25 = 0; | |
data.pm10 = 0; | |
data.sumBins = 0; | |
data.temp = 0; | |
data.altitude = 0; | |
data.hum = 0; | |
data.xtra = 0; | |
data.co2 = 0; | |
data.year = year(epochTime); | |
data.month = month(epochTime); | |
data.day = day(epochTime); | |
data.hour = hour(epochTime); | |
data.minute = minute(epochTime); | |
data.second = second(epochTime); | |
data.lat = 0; | |
data.lng = 0; | |
data.heading = 0; | |
} | |
AsyncWebServer server(80); | |
const char* ssid = "ESPWebSerial"; // Your WiFi AP SSID | |
const char* password = "abc123456"; // Your WiFi Password | |
void setup() { | |
Serial.begin(115200); | |
WiFi.softAP(ssid, password); | |
//Serial.print("AP IP address: "); | |
//Serial.println(IP); | |
// WebSerial is accessible at "<IP Address>/webserial" in browser | |
WebSerial.begin(&server); | |
/* Attach Message Callback */ | |
//WebSerial.onMessage(recvMsg); | |
server.begin(); | |
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__); | |
epochTime = compiled.Epoch32Time(); /*+ 946684800*/ //+ (tzDiff * 3600); | |
WebSerial.println(epochTime); | |
// Setup SD card | |
if (!sd.begin(SD_CONFIG)) { | |
WebSerial.print("SD card mount failed\n"); | |
return; | |
} | |
// open csv file | |
if (!file.open("GPS_filtered.csv", O_READ)) { | |
WebSerial.print("Opening file failed!\n"); | |
return; | |
} | |
initData(data); | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= 1000) { | |
previousMillis = currentMillis; | |
epochTime++; | |
if (currentMillis - printTimeTimer >= 10000) { | |
printTimeTimer = currentMillis; | |
printUTCTime(); | |
} | |
// read data from SD card | |
char buf[128]; | |
if (file.fgets(buf, sizeof(buf)) <= 0) { | |
file.rewind(); | |
file.fgets(buf, sizeof(buf)); | |
} | |
sscanf(buf, "%lf,%lf,%f,%lf", &data.lat, &data.lng, &data.altitude, &data.heading); | |
data.pm1 = random(0, 100); | |
data.pm25 = random(0, 100); | |
data.pm10 = random(0, 100); | |
data.sumBins = random(0, 500); | |
data.temp = random(0, 40); | |
//data.altitude = random(0, 1000); | |
data.hum = random(0, 95); | |
data.xtra = random(0, 100); | |
data.co2 = random(0, 2000); | |
data.year = year(epochTime); | |
data.month = month(epochTime); | |
data.day = day(epochTime); | |
data.hour = hour(epochTime); | |
data.minute = minute(epochTime); | |
data.second = second(epochTime); | |
//data.lat = random(0, 100); | |
//data.lng = random(0, 100); | |
Serial.write('<'); | |
Serial.write((const char*)&data, sizeof(data)); | |
Serial.write('>'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment