Skip to content

Commit

Permalink
Develop is now v 1.3 (#11)
Browse files Browse the repository at this point in the history
* testing esp_wifi header file to allow for lower curent when deep sleep happens

* some refactoring and esp_wifi_stop added

* refactor and clean up readBattery. Remove commented WiFi.h #include from weather.ino

* rainfall data now full 24h and minor cleanup of unused code

* updated comments and release to reflect v1.3 work

* 24h rainfall tracking WIP

* 60min rainfall tracking WIP

* 60min rainfall tracking WIP

* 60min rainfall tracking WIP

* ESPcoreF and ESPcoreC added for MQTT publish topics

* 60 min rainfall WIP

* alternate PCB support, low battery flag correction, railfall 60 min WIP

* load switch support and capability to drive IO12 while in deep sleep

* remove deep sleep header file

* #include<> tweaks for clearer identification of system headers. Increased battery_low multiplier from 4 to 10

* added uv.begin to properly initialize sensor

* rainfallData struct rename and mqtt topic added for rainfall interval accumulation

* rssi topic added

* v 1.3 pre-merge/pre-release. Sensor error management better handled (making use of return results from .begin functions

* MQTT checks now point to app variable, not #define

* correct compile error

* %l to %li for format specifier on RSSI topic

* #define MQTT removed

* minor cleanup of debug statements
  • Loading branch information
jhughes1010 committed Apr 11, 2022
1 parent 0c7e791 commit 564df3d
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 93 deletions.
16 changes: 8 additions & 8 deletions eeprom.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
//========================================================================
// readEEPROM: Read historicalData from NVM
//========================================================================
void readEEPROM(struct historicalData *rainfall)
void readEEPROM(struct rainfallData *rainfall)
{
int structSize;
byte buffer[200];
byte c;
bool nonZero = false;
int address = 0x0000;

structSize = sizeof(historicalData);
structSize = sizeof(rainfallData);
memset(buffer, 0, sizeof(structSize));

//Send dummy write to set addpress register of NVM
Expand Down Expand Up @@ -51,7 +51,7 @@ void readEEPROM(struct historicalData *rainfall)
// boot must be >1 in order for a write to take place, array will never
// be written on boot = 1
//========================================================================
void writeEEPROM(struct historicalData *rainfall)
void writeEEPROM(struct rainfallData *rainfall)
{
byte buffer[200];
int structSize;
Expand All @@ -61,7 +61,7 @@ void writeEEPROM(struct historicalData *rainfall)
int page = 0;


structSize = sizeof(historicalData);
structSize = sizeof(rainfallData);
memcpy(buffer, rainfall, structSize);
for (page = 0; (page * pageSize) < structSize; page++)
{
Expand Down Expand Up @@ -91,7 +91,7 @@ void initEEPROM(void)
{
int structSize;
int x;
structSize = sizeof(historicalData);
structSize = sizeof(rainfallData);
MonPrintf("sizeof int: %i\n", sizeof(int));
for (x = 0; x < structSize; x++)
{
Expand All @@ -107,14 +107,14 @@ void initEEPROM(void)
//========================================================================
// conditionalWriteEEPROM: Only write EEPROM if something has changed
//========================================================================
void conditionalWriteEEPROM(struct historicalData *rainfall)
void conditionalWriteEEPROM(struct rainfallData *rainfall)
{
struct historicalData historyBuffer;
struct rainfallData historyBuffer;
bool match = true;
int structSize;
int x;

structSize = sizeof(historicalData);
structSize = sizeof(rainfallData);

readEEPROM(&historyBuffer);

Expand Down
34 changes: 33 additions & 1 deletion mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ void SendDataMQTT (struct sensorData *environment)
}
}
MQTTPublishInt("boot/", (int)bootCount, true);
MQTTPublishLong("rssi/", rssi, true);
MQTTPublishInt("temperatureF/", (int)environment->temperatureF, true);
MQTTPublishInt("temperatureC/", (int)environment->temperatureC, true);
MQTTPublishInt("windSpeed/", (int)environment->windSpeed, true);
MQTTPublishFloat("windSpeed/", environment->windSpeed, true);
MQTTPublishInt("windDirection/", (int)environment->windDirection, true);
MQTTPublishString("windCardinalDirection/", environment->windCardinalDirection, true);
MQTTPublishInt("photoresistor/", (int)environment->photoresistor, true);
#ifndef METRIC
MQTTPublishFloat("rainfallInterval/", rainfall.intervalRainfall * 0.011, true);
MQTTPublishFloat("rainfall/", rainfall.hourlyRainfall[hourPtr] * 0.011, true);
MQTTPublishFloat("rainfall24/", last24() * 0.011, true);
#else
MQTTPublishFloat("rainfallInterval/", rainfall.intervalRainfall * 0.011 * 25.4, true);
MQTTPublishFloat("rainfall/", rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4, true);
MQTTPublishFloat("rainfall24/", last24() * 0.011 * 25.4, true);
#endif
Expand All @@ -57,6 +60,9 @@ void SendDataMQTT (struct sensorData *environment)
MQTTPublishFloat("pressure/", environment->barometricPressure, true);
MQTTPublishFloat("caseTemperature/", environment->BMEtemperature, true);
MQTTPublishInt("batteryADC/", (int)environment->batteryADC, true);
MQTTPublishInt("ESPcoreF/", (int)environment->coreF, true);
MQTTPublishInt("ESPcoreC/", (int)environment->coreC, true);
MQTTPublishInt("timeEnabled/", (int)elapsedTime, true);
MQTTPublishBool("lowBattery/", lowBattery, true);
MonPrintf("Issuing mqtt disconnect\n");
client.disconnect();
Expand Down Expand Up @@ -113,6 +119,32 @@ void MQTTPublishInt(const char topic[], int value, bool retain)
}
}


//=======================================================================
// MQTTPublishLong: routine to publish int values as strings
//=======================================================================
void MQTTPublishLong(const char topic[], long value, bool retain)
{
char topicBuffer[256];
char payload[256];
int retryCount = 0;
int status = 0;

strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
if (!client.connected()) reconnect();
client.loop();
sprintf(payload, "%li", value);
MonPrintf("%s: %s\n", topicBuffer, payload);
while (!status && retryCount < 5)
{
status = client.publish(topicBuffer, payload, retain);
MonPrintf("MQTT status: %i\n", status);
delay(50);
retryCount++;
}
}

//=======================================================================
// MQTTPublishFloat: routine to publish float values as strings
//=======================================================================
Expand Down
97 changes: 95 additions & 2 deletions rainfall.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@ void clearRainfall(void)
memset(&rainfall, 0x00, sizeof(rainfall));
}



//=======================================================================
//
// Hourly accumulation routines
//
//=======================================================================


//=======================================================================
// clearRainfallHour: zero out specific hour element of rainfall structure array
//=======================================================================
void clearRainfallHour(int hourPtr)
{
//Clear carryover if hourPtr is not matching prior hourPtr value (we have a new hour)
if (rainfall.priorHour != hourPtr)
{
rainfall.hourlyCarryover = 0;
}
//move contents of oldest hour to the carryover location and set hour to zero
rainfall.hourlyCarryover += rainfall.hourlyRainfall[hourPtr % 24];
rainfall.hourlyRainfall[hourPtr % 24] = 0;

rainfall.priorHour = hourPtr;
}

//=======================================================================
Expand All @@ -41,7 +59,7 @@ void printHourlyArray (void)
}

//=======================================================================
// last24: return tip counter for last 24h (technically 23h)
// last24: return tip counter for last 24h
//=======================================================================
int last24(void)
{
Expand All @@ -51,10 +69,85 @@ int last24(void)
{
totalRainfall += rainfall.hourlyRainfall[hour];
}
MonPrintf("Total rainfall: %i\n", totalRainfall);
//add carryover value
totalRainfall += rainfall.hourlyCarryover;

MonPrintf("Total rainfall (last 24 hours): %i\n", totalRainfall);
return totalRainfall;
}


//=======================================================================
//
// Minute accumulation routines
//
//=======================================================================
// NOTE: When speaking of minutes and minute array, we use 5 min as
// minimum grouping for minute-by-minute rainfall



//=======================================================================
// clearRainfallMinute: zero out specific minute element of rainfall structure array
//=======================================================================
void clearRainfallMinute(int minutePtr)
{
int minuteIndex;
minuteIndex = (int)minutePtr / 12 + 1;
//Clear carryover if hourPtr is not matching prior hourPtr value (we have a new hour)
if (rainfall.priorHour != minutePtr)
{
rainfall.hourlyCarryover = 0;
}
//move contents of oldest hour to the carryover location and set hour to zero
rainfall.hourlyCarryover += rainfall.hourlyRainfall[minutePtr % 24];
rainfall.hourlyRainfall[minutePtr % 24] = 0;

//rainfall.priorHour = hourPtr;
}

//=======================================================================
// addTipsToMinute: increment current hour tip count
//=======================================================================
void addTipsToMinute(int count)
{
int minute = timeinfo.tm_hour;
rainfall.current60MinRainfall[minute] = rainfall.current60MinRainfall[minute] + count;
}

//=======================================================================
// printMinuteArray: diagnostic routine to print minute rainfall array to terminal
//=======================================================================
void printMinuteArray (void)
{
int minute = 0;
for (minute = 0; minute < 12; minute++)
{
MonPrintf("Minute %i: %u\n", minute * 5, rainfall.current60MinRainfall[minute]);
}
}

//=======================================================================
// last60min: return tip counter for last 60 minutes
//=======================================================================
int last60min(void)
{
int minute;
int totalRainfall = 0;
for (minute = 0; minute < 12; minute++)
{
totalRainfall += rainfall.current60MinRainfall[minute];
}
//add carryover value
totalRainfall += rainfall.minuteCarryover;

MonPrintf("Total rainfall (last 60 minutes): %i\n", totalRainfall);
return totalRainfall;
}




//=======================================================================
// rainTick: ISR for rain tip gauge count
//=======================================================================
Expand Down
13 changes: 8 additions & 5 deletions sec.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ const int UpdateIntervalSeconds = 5 * 60; //Sleep timer (60s) testing
//===========================================
//Battery calibration
//===========================================
//measured battery voltage/ADC reading
#define batteryCalFactor .001167

//batteryCalFactor = measured battery voltage/ADC reading
#define batteryCalFactor .0011804

//===========================================
//Timezone information
//===========================================
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = -7 * 3600;
const int daylightOffset_sec = 3600;

//========================= Enable Blynk, MQTT or Thingspeak ===================================

// configuration control constant for use of either Blynk or Thingspeak
//const String App = "BLYNK"; // alternative is line below
//const String App = "Thingspeak"; // alternative is line above
const String App = "MQTT"; // alternative is line below
Loading

0 comments on commit 564df3d

Please sign in to comment.