Skip to content

Commit

Permalink
Merge branch 'develop'. Manual commit for v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
James Hughes authored and James Hughes committed Apr 14, 2022
2 parents 564df3d + 082b946 commit 87c3233
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 103 deletions.
139 changes: 58 additions & 81 deletions mqtt.ino
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ void SendDataMQTT (struct sensorData *environment)
delay(1000);
}
}
MQTTPublishInt("boot/", (int)bootCount, true);
MQTTPublishLong("rssi/", rssi, true);
MQTTPublishInt("temperatureF/", (int)environment->temperatureF, true);
MQTTPublishInt("temperatureC/", (int)environment->temperatureC, true);
MQTTPublishFloat("windSpeed/", environment->windSpeed, true);
MQTTPublishInt("windDirection/", (int)environment->windDirection, true);
MQTTPublishString("windCardinalDirection/", environment->windCardinalDirection, true);
MQTTPublishInt("photoresistor/", (int)environment->photoresistor, true);
MQTTPublish("boot/", (int)bootCount, true);
MQTTPublish("rssi/", rssi, true);
MQTTPublish("temperatureF/", (int)environment->temperatureF, true);
MQTTPublish("temperatureC/", (int)environment->temperatureC, true);
MQTTPublish("windSpeed/", environment->windSpeed, true);
MQTTPublish("windDirection/", (int)environment->windDirection, true);
MQTTPublish("windCardinalDirection/", environment->windCardinalDirection, true);
MQTTPublish("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);
MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011), true);
MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011), true);
MQTTPublish("rainfall24/", (float) (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);
MQTTPublish("rainfallInterval/", (float) (rainfall.intervalRainfall * 0.011 * 25.4), true);
MQTTPublish("rainfall/", (float) (rainfall.hourlyRainfall[hourPtr] * 0.011 * 25.4), true);
MQTTPublish("rainfall24/", (float) (last24() * 0.011 * 25.4), true);
#endif

MQTTPublishFloat("batteryVoltage/", environment->batteryVoltage, true);
MQTTPublishFloat("lux/", environment->lux, true);
MQTTPublishFloat("UVIndex/", environment->UVIndex, true);
MQTTPublishFloat("relHum/", environment->humidity, true);
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);
MQTTPublish("batteryVoltage/", environment->batteryVoltage, true);
MQTTPublish("lux/", environment->lux, true);
MQTTPublish("UVIndex/", environment->UVIndex, true);
MQTTPublish("relHum/", environment->humidity, true);
MQTTPublish("pressure/", environment->barometricPressure, true);
MQTTPublish("caseTemperature/", environment->BMEtemperature, true);
MQTTPublish("batteryADC/", (int)environment->batteryADC, true);
MQTTPublish("ESPcoreF/", (int)environment->coreF, true);
MQTTPublish("ESPcoreC/", (int)environment->coreC, true);
MQTTPublish("timeEnabled/", (int)elapsedTime, true);
MQTTPublish("lowBattery/", lowBattery, true);
MonPrintf("Issuing mqtt disconnect\n");
client.disconnect();
MonPrintf("Disconnected\n");
Expand All @@ -72,113 +72,75 @@ void SendDataMQTT (struct sensorData *environment)
//=======================================================================
// MQTTPublishString: routine to publish string
//=======================================================================
void MQTTPublishString(const char topic[], char *value, bool retain)
void MQTTPublish(const char topic[], char *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, "%s", 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++;
}
MQTTSend(topicBuffer, payload, retain);
}

//=======================================================================
// MQTTPublishInt: routine to publish int values as strings
//=======================================================================
void MQTTPublishInt(const char topic[], int value, bool retain)
void MQTTPublish(const char topic[], int 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, "%i", 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++;
}
MQTTSend(topicBuffer, payload, retain);
}


//=======================================================================
// MQTTPublishLong: routine to publish int values as strings
// MQTTPublish Long: routine to publish int values as strings
//=======================================================================
void MQTTPublishLong(const char topic[], long value, bool retain)
void MQTTPublish(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++;
}
MQTTSend(topicBuffer, payload, retain);
}

//=======================================================================
// MQTTPublishFloat: routine to publish float values as strings
//=======================================================================
void MQTTPublishFloat(const char topic[], float value, bool retain)
void MQTTPublish(const char topic[], float 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, "%6.3f", 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++;
}
MQTTSend(topicBuffer, payload, retain);
}

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

strcpy(topicBuffer, mainTopic);
strcat(topicBuffer, topic);
Expand All @@ -192,15 +154,9 @@ void MQTTPublishBool(const char topic[], bool value, bool retain)
{
sprintf(payload, "false");
}
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++;
}
MQTTSend(topicBuffer, payload, retain);
}

//=======================================================================
// reconnect: MQTT reconnect
//=======================================================================
Expand All @@ -221,3 +177,24 @@ void reconnect() {
}
}
}

//=======================================================================
// MQTTSend: MQTT send topic with value to broker
//=======================================================================
void MQTTSend(char *topicBuffer, char *payload, bool retain)
{
int status = 0;
int retryCount = 0;
#ifdef ExtendedMQTT
MonPrintf("%s: %s\n", topicBuffer, payload);
#endif
while (!status && retryCount < 5)
{
status = client.publish(topicBuffer, payload, retain);
#ifdef ExtendedMQTT
MonPrintf("MQTT status: %i\n", status);
#endif
delay(50);
retryCount++;
}
}
6 changes: 6 additions & 0 deletions sec.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//Controls supression of the MonPrintf function to serial
//===========================================
#define SerialMonitor
#define ExtendedMQTT

//===========================================
//WiFi connection
Expand Down Expand Up @@ -60,6 +61,11 @@ const char mainTopic[20] = "MainTopic/";
//I see 2 switch pulls to GND per revolation. Not sure what others see
#define WIND_TICKS_PER_REVOLUTION 2

//===========================================
//General defines
//===========================================
#define RSSI_INVALID -9999

//===========================================
//Set how often to wake and read sensors
//===========================================
Expand Down
3 changes: 2 additions & 1 deletion time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ struct tm timeinfo;
//=======================================================================
void printLocalTime()
{
if (!getLocalTime(&timeinfo)) {
if (!getLocalTime(&timeinfo))
{
MonPrintf("Failed to obtain time\n");
return;
}
Expand Down
45 changes: 27 additions & 18 deletions weather.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@
//
//Supporting the following project: https://www.instructables.com/Solar-Powered-WiFi-Weather-Station-V30/

//version 1.3 RC1
#define VERSION 1.3
//version 1.3.1
#define VERSION "1.3.1"

//=============================================
// Changelog
//=============================================
/* v1.3 supports 24h rainfall data, not 23h
/*
* v1.3.1
* 1. Corrects missing quotes on #define VERSION statement
* 2. max retry if 15 connect attempts added and then we bail on WiFi connect. This prevents us from hitting the WDT limit and rebooting
*
*
* v1.3 supports 24h rainfall data, not 23h
supports current 60 min rainfall, not
current "hour" that looses data at top
of the hour.
Expand Down Expand Up @@ -81,14 +87,14 @@
#define WDT_TIMEOUT 60 //watchdog timer

#else
#define WIND_SPD_PIN 26 //reed switch based anemometer count
#define WIND_SPD_PIN 26 //reed switch based anemometer count
#define RAIN_PIN 25 //reed switch based tick counter on tip bucket
#define WIND_DIR_PIN 35 //variable voltage divider output based on varying R network with reed switches
#define PR_PIN 34 //photoresistor pin
#define VOLT_PIN 33 //voltage divider for battery monitor
#define TEMP_PIN 15 // DS18B20 hooked up to GPIO pin 15
#define LED_PIN 14 //Diagnostics using built-in LED
//#define MODE_PIN 12 //Load Switch
#define WIND_DIR_PIN 35 //variable voltage divider output based on varying R network with reed switches
#define PR_PIN 34 //photoresistor pin
#define VOLT_PIN 33 //voltage divider for battery monitor
#define TEMP_PIN 15 // DS18B20 hooked up to GPIO pin 15
#define LED_PIN 14 //Diagnostics using built-in LED
//#define MODE_PIN 12 //Load Switch
#endif

//===========================================
Expand Down Expand Up @@ -206,15 +212,18 @@ void setup()
if (WiFiEnable)
{
rssi = wifi_connect();
sensorEnable();
sensorStatusToConsole();
//Calibrate Clock - My ESP RTC is noticibly fast
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
printTimeNextWake();
if (rssi != RSSI_INVALID)
{
sensorEnable();
sensorStatusToConsole();
//Calibrate Clock - My ESP RTC is noticibly fast
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
printLocalTime();
printTimeNextWake();
processSensorUpdates();
WiFi.disconnect();
esp_wifi_stop();
WiFi.disconnect();
esp_wifi_stop();
}
}

UpdateIntervalModified = nextUpdate - mktime(&timeinfo);
Expand Down
28 changes: 25 additions & 3 deletions wifi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
//=======================================================================
long wifi_connect()
{
<<<<<<< HEAD
long wifi_signal = 0;

MonPrintf("Connecting to %s\n", App);
=======
bool WiFiConnectHalt = false;
int retry = 0;
long wifi_signal = 0;

MonPrintf("Starting wifi for App = %s\n", App);
>>>>>>> develop
if (App == "BLYNK") // for posting datas to Blynk App
{

Expand All @@ -15,15 +23,28 @@ long wifi_connect()
{
MonPrintf("Connecting to WiFi\n");
WiFi.begin(ssid, pass);
<<<<<<< HEAD
while (WiFi.status() != WL_CONNECTED)
=======
while (WiFi.status() != WL_CONNECTED && !WiFiConnectHalt )
>>>>>>> develop
{
delay(500);
retry++;
if (retry > 15)
{
MonPrintf("Max trys to connect to WiFi reached and failed");
WiFiConnectHalt = true;
wifi_signal = RSSI_INVALID;
return wifi_signal;
}
}
MonPrintf("WiFi connected\n");
wifi_signal = WiFi.RSSI();
}
else if (App == "MQTT") // for posting datas to Thingspeak website
{
/*
else if (App == "MQTT") // for posting datas to Thingspeak website
{
MonPrintf("Connecting to WiFi\n");
WiFi.begin(ssid, pass);
Expand All @@ -32,7 +53,8 @@ long wifi_connect()
delay(500);
}
MonPrintf("WiFi connected\n");
}
}
*/
else
{
MonPrintf(" is not a valid application");
Expand Down

0 comments on commit 87c3233

Please sign in to comment.