Skip to content

Commit

Permalink
major rework. added OTA support, added MQTT support, temporary remove…
Browse files Browse the repository at this point in the history
…d Captive Portal
  • Loading branch information
anubisg1 committed Sep 24, 2021
1 parent 24bac33 commit 1327152
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 474 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ You can change the AP's SSID and password-protect it by changing the values in `

Feel free to fork this project and improve it. Some things that would be good to have are:

- [ ] Use of MQTT protocol.
- [x] Use of MQTT protocol.
- [ ] API authentication.
- [x] mDNS (easier to discover your device by using a `.local` domain).
- [ ] Webpage (it only has an API).
- [x] OTA support

## License

Expand Down
78 changes: 39 additions & 39 deletions esp8266-hunter-sprinkler/include/README
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
38 changes: 15 additions & 23 deletions esp8266-hunter-sprinkler/include/global_config.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
/**
* Header file that includes default parameters for all parts of the program
*/

#ifndef GLOBAL_CONFIG_H
#define GLOBAL_CONFIG_H

#define SERIAL_SPEED 9600
#define REM_PIN 16
#include <Arduino.h>

// WIFi
extern const String device_hostname;
#define SSID_AP "WateringSystemAP" // SSID of the AP created to configure WiFi
#define PWD_AP "" // Password of the AP created to configure WiFi

//MQTT
extern const char* mqtt_server;
extern const int mqtt_port; //default is 1883
extern const char *mqtt_user;
extern const char *mqtt_pass;

#endif
#ifndef GLOBAL_CONFIG_H
#define GLOBAL_CONFIG_H

#define SERIAL_SPEED 9600
#define REM_PIN 16

#define HOSTNAME "X-CORE"
#define WIFI_SSID "your_wifi_ssid"
#define WIFI_PASSWORD "your_wifi_password"
#define MQTT_SERVER "server_ip_add"
#define MQTT_PORT 1883
#define MQTT_USER "mqttuser"
#define MQTT_PASSWORD "mqttpassword!"

#endif
18 changes: 18 additions & 0 deletions esp8266-hunter-sprinkler/include/mqtt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef MQTT_H
#define MQTT_H

#include <global_config.h>
#include <PubSubClient.h>

extern String TopicCheckIn;
extern String TopicCommands;
extern String TopicStatus;
extern PubSubClient client;
extern WiFiClient espClient;

void mqtt_connect();
void mqttPublishResult(const char *toSend);
void mqtt_subscribe_topics ();
void callback(char* topic, byte* payload, unsigned int length);

#endif
11 changes: 11 additions & 0 deletions esp8266-hunter-sprinkler/include/ota.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef OTA_H
#define OTA_H

#include <ESPAsyncWebServer.h>

void handleRoot(AsyncWebServerRequest *request);
void handleUpdate(AsyncWebServerRequest *request);
void handleDoUpdate(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final);
void printProgress(size_t prg, size_t sz);

#endif
19 changes: 10 additions & 9 deletions esp8266-hunter-sprinkler/include/web_server.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef WEB_SERVER_H
#define WEB_SERVER_H

#include <ESPAsyncWebServer.h>
extern AsyncWebServer server;

void setupWebServer();
void setupAPIRoutes();

#ifndef WEB_SERVER_H
#define WEB_SERVER_H

#include <ESPAsyncWebServer.h>

extern AsyncWebServer server;

void setup_WebServer();
void setup_APIRoutes();

#endif
21 changes: 10 additions & 11 deletions esp8266-hunter-sprinkler/include/web_server_api.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef WEB_SERVER_API_H
#define WEB_SERVER_API_H

#include <ESPAsyncWebServer.h>

void startZoneRequest(AsyncWebServerRequest *request);
void startProgramRequest(AsyncWebServerRequest *request);
void stopZoneRequest(AsyncWebServerRequest *request);
int getIdFromURL(AsyncWebServerRequest *request, String root);


#ifndef WEB_SERVER_API_H
#define WEB_SERVER_API_H

#include <ESPAsyncWebServer.h>

void startZoneRequest(AsyncWebServerRequest *request);
void startProgramRequest(AsyncWebServerRequest *request);
void stopZoneRequest(AsyncWebServerRequest *request);
int getIdFromURL(AsyncWebServerRequest *request, String root);

#endif
16 changes: 9 additions & 7 deletions esp8266-hunter-sprinkler/include/web_server_scheduled.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef WEB_SERVER_SCHEDULED_H
#define WEB_SERVER_SHCEDULED_H

void startZone(byte num, byte time, String webhook);
void startProgram(byte num, String webhook);
void stopZone(byte num, String webhook);

#ifndef WEB_SERVER_SCHEDULED_H
#define WEB_SERVER_SCHEDULED_H

#include <ESPAsyncWebServer.h>

void startZone(byte num, byte time, String webhook);
void startProgram(byte num, String webhook);
void stopZone(byte num, String webhook);

#endif
17 changes: 9 additions & 8 deletions esp8266-hunter-sprinkler/include/wifi.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef WIFI_H
#define WIFI_H

void setupWifi();
bool checkWifiConnection(bool firstTimeDisconnected);

#endif

#ifndef WIFI_H
#define WIFI_H

#include <ESP8266WiFi.h>

void setup_wifi();
void reconnect();

#endif
39 changes: 19 additions & 20 deletions esp8266-hunter-sprinkler/platformio.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:d1_mini_pro]
platform = espressif8266
framework = arduino
board = d1_mini_pro
board_build.filesystem = littlefs
lib_deps =
ESPAsyncWiFiManager
ArduinoJson
ESP Async WebServer
ESPAsyncTCP
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:d1_mini_pro]
platform = espressif8266
board = d1_mini_pro
framework = arduino
lib_deps =
knolleary/PubSubClient@^2.8
me-no-dev/ESPAsyncTCP@^1.2.2
me-no-dev/ESP Async WebServer@^1.2.3
bblanchon/ArduinoJson@^6.18.4
12 changes: 4 additions & 8 deletions esp8266-hunter-sprinkler/src/global_config.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#include <global_config.h>

const String device_hostname = "Hunter";

const char* mqtt_server = "MQTT_SERVER_IP";
const int mqtt_port = 1883; //default is 1883
const char *mqtt_user = "MQTT_USERNAME";
const char *mqtt_pass = "MQTT_PASSWORD";
#include <global_config.h>

const String device_hostname = "X-CORE";

60 changes: 28 additions & 32 deletions esp8266-hunter-sprinkler/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
/**
* First version: July 2020 - Eloi Codina Torras
*/
#include <wifi.h>
#include <FS.h>
#include <ESP8266mDNS.h>

#include <web_server.h>
#include <global_config.h>

AsyncWebServer server = AsyncWebServer(80);
bool wifiDisconnected = false;

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

setupWifi();
setupWebServer();

/* if (!SPIFFS.begin()) {
Serial.println("Error mounting the filesystem.");
} else {
Serial.println("Filesystem is now ready.");
}
*/

}

void loop() {
wifiDisconnected = checkWifiConnection(wifiDisconnected);
MDNS.update();
}
#include <ESP8266mDNS.h>

#include <wifi.h>
#include <mqtt.h>
#include <web_server.h>

AsyncWebServer server = AsyncWebServer(80);

void setup() {
Serial.begin(SERIAL_SPEED);
WiFi.mode(WIFI_STA);
setup_wifi();
setup_WebServer();
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("Ready! Open http://%s.local in your browser\n", HOSTNAME);
}

void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
MDNS.update();
//timer.run();
}
Loading

0 comments on commit 1327152

Please sign in to comment.