ESP_IOT v2.5
IOT ESP Coding
MainModule.h
Go to the documentation of this file.
1
2#ifndef MainModule_h
3#define MainModule_h
4
5//#include "../../Defines.h"
6
7//!added 9.29.22 to support wildcards #196
8#include "TokenParser.h"
9
10//! always avaialble to others.. the model controller part of MVC. The View is in DisplayModule. The other part of Controller is the button clicks in ButtonProcess.h (but with this, there is no logic there..)
11#include "ModelController.h"
12
13//! preferences
15
16//!THIS IS the setup() and loop() but using the "component" name, eg MQTTNetworking()
17//!This will perform preference initializtion as well
18//! called from the setup()
19void setup_mainModule();
20
21//! called for the loop() of this plugin
22void loop_mainModule();
23
24//!cleans the main module EPROM
26
27//!stop all loops... while OTA working..
29//! if stopped
31//!restart all loops... while OTA working..
33
34#define PT_SERVICE_UUID "b0e6a4bf-cccc-ffff-330c-0000000000f0" //Pet Tutor feeder service for feed NOTE: Lower case for GEN3 compatability
35#define PT_CHARACTERISTIC_UUID "b0e6a4bf-cccc-ffff-330c-0000000000f1" //Pet Tutor feeder characteristic NOTE: Lower case for GEN3 compatability
36
37#ifdef ESP_M5
38#define LED 2
39#endif
40
41#define TOPIC_TO_SEND (char*)"usersP/bark"
42
43//!10000 == no poweroff
44#define NO_POWEROFF_AMOUNT_MAIN 10000
45#define NO_POWEROFF_AMOUNT_STRING_MAIN (char*)"10000"
46
47
48
49//! processes a message that might save in the EPROM.. the cmd is still passed onto other (like the stepper module)
50//! returns true if finished processing, otherwise this can be sent onto other modules (like stepper)
51//void processClientCommand_mainModule(String message);
52//! single character version of processClientCommand (since many used that already)
54//! New RegisterCallback that works across a number of callback modules
55//!
56//! callbacksModuleId
57#define CALLBACKS_MQTT 0
58#define CALLBACKS_BUTTON_MODULE 1
59#define CALLBACKS_BLE_CLIENT 2
60#define CALLBACKS_BLE_SERVER 3
61#define CALLBACKS_MODULE_MAX 4
62
63//!register the callback based on the callbackType. use the callbacksModuleId for which one..
64void registerCallbackMain(int callbacksModuleId, int callbackType, void (*callback)(char*));
65//!performs the indirect callback based on the callbackType
66void callCallbackMain(int callbacksModuleId, int callbackType, char *message);
67
68//!adding a synchronous call to send a message over the network (assuming MQTT but not specified), this tacks on {device} and {t:time}
69void sendMessageString_mainModule(char *messageString);
70
71#ifdef USE_MQTT_NETWORKING
72//!example callback: but the scope would have the pCharacteristic defined, etc..
73//!This is pased just before the setupMQTTNetworking() is called..
74void feedMessageCallback(char *message);
75#endif
76
77//!supports turning on the solid light..
78void solidLightOnOff(boolean flag);
79
80//COMMON CALLBACKS
81//NOTE: This is where the true Object Oriented dynamic plug-in approaches would come in..
82// We are using #ifdef to bound the plug-in but there is still too much knowledge about wiring events to methods
83// in this code.. TODO, make more dynmaic event based..
84
85//*** The callback for "onWrite" of the bluetooth "onWrite'
86void onWriteBLEServerCallback(char *message);
87
88/*******************************BLE Server*************************************/
89//*** The callback for "onWrite" of the bluetooth "onWrite'
90void onBLEServerCallback(char *message);
91
92//! ** The callback for "status messages" of the bluetooth
93void onStatusMessageBLEServerCallback(char *message);
94
95//!prints the module configuration by looking at defines
96//! Eventually this might be an object returned letting the code
97//!know a capability is included for runtime (vs compile time) decisions
99
100//!called on single click
101//!NOTE: with BLE_CLIENT_NETWORKING, the right button and top button send a BLE command for feeding..
102void singleClickTouched(char *whichButton);
103
104//!callback for SOLID blinking led
105void solidLight(char *message);
106
107//!callback for blinking led
108void blinkMessageCallback(char *message);
109
110
111//!take a picture (calls the camera module).. what to do with picture??? TODO
113
114//!clean the SSID eprom (MQTT_CLEAN_SSID_EPROM)
115void cleanSSID_EPROM_MessageCallback(char *message);
116
117//!callback for SOLID blinking led
118void solidLightMessageCallback(char *message);
119
120//!clean the saved WIFI credential, otherwise the AP mode doesn't work (6.3.22)
122
123//!TESTING for saying credentials are filled in via BLE (while in AP mode), so kick out of AP and continue..
124//! set that the credentials are set. Called from the MQTT after processJSONMessage() found things were good..
125//! April 8, 2022
127
128//!retrieve a JSON string for the ssid and ssid_password: {'ssid':<ssid>,'ssidPassword':<pass>"}
130
131//!! cycle through the next WIFI saved credential - return next one that isn't our current one..
133
134//! sets the WIFI and MQTT user/password. It's up to the code to decide who needs to know (currently this calls APmodule..)
135void main_updateMQTTInfo(char *ssid, char *ssid_password, char *username, char *password, char *guestPassword, char *deviceName, char * host, char * port, char *locationString);
136
137//! TODO: make this a registeration approach
138
139//! 3.21.22 these are to setup for the next time the main loop() runs to call these commands..
140//! The implementation is hard coded in the ESP_IO.ino
141#define ASYNC_CALL_OTA_UPDATE 0
142//! cleans out the credentials and restarts in AP (Access Point) mode.
143#define ASYNC_CALL_CLEAN_CREDENTIALS 1
144//!cleans the EPROM totally, and reboots
145#define ASYNC_CALL_CLEAN_EPROM 2
146//!sends a 'c' to the BLE end of the code (assuming a feeder is connected). Need to morph if a real feeder
147#define ASYNC_CALL_FEED_COMMAND 3
148//!sends a 'B' to the BLE end of the code (assuming a feeder is connected).
149#define ASYNC_CALL_BUZZ_ON 4
150//!sends a 'b' to the BLE end of the code (assuming a feeder is connected).
151#define ASYNC_CALL_BUZZ_OFF 5
152//!sends a message (like #FEED) on the users topic
153#define ASYNC_SEND_MQTT_FEED_MESSAGE 6
154//!sends the status from the main module URL
155#define ASYNC_SEND_MQTT_STATUS_URL_MESSAGE 7
156
157//!sets the GATEWAY mode on
158#define ASYNC_SET_GATEWAY_ON 8
159//!sets the GATEWAY mode off
160#define ASYNC_SET_GATEWAY_OFF 9
161
162//!sets the GATEWAY mode off
163#define ASYNC_REBOOT 10
164//!sets the GATEWAY mode off
165#define ASYNC_POWEROFF 11
166//!blank the screen
167#define ASYNC_BLANKSCREEN 12
168
169//! swaps WIFI 'w'
170#define ASYNC_SWAP_WIFI 13
171//! next WIFI 'n'
172#define ASYNC_NEXT_WIFI 14
173//! restarts the WIFI (after BLE interrupt over)
174#define ASYNC_RESTART_WIFI_MQTT 15
175//! the max one greater than last one
176#define ASYNC_CALL_MAX 16 //don't forget to update this MAX (last one + 1)
177
178//!these are the async with a string parameter. This sends a BLE command
179#define ASYNC_CALL_BLE_CLIENT_PARAMETER 0
180//!these are the async with a string parameter
181#define ASYNC_CALL_OTA_FILE_UPDATE_PARAMETER 1
182//!these are the async with a string parameter
183#define ASYNC_JSON_MESSAGE_PARAMETER 2
184//! the max one greater than last one
185#define ASYNC_CALL_PARAMETERS_MAX 3
186
187//! dispatches a call to the command specified. This is run on the next loop()
188void main_dispatchAsyncCommand(int asyncCallCommand);
189
190//!send an async call with a string parameter. This will set store the value and then async call the command (passing the parameter)
191//!These are the ASYNC_CALL_PARAMETERS_MAX
192void main_dispatchAsyncCommandWithString(int asyncCallCommand, char *parameter);
193
194//!storage for asyncCallCommands
195void initAsyncCallFlags();
196
197//!checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to false
199
200// **** Helper Methods
201//!If nil it create one with just the null, so strlen = 0
202//!NOTE: the strdup() might be used later..
203char* createCopy(char * stringA);
204//!a char* version of startsWith (after skipping spaces)
205boolean startsWithChar(char *str, char c);
206//! if an empty string
207boolean isEmptyString(char *stringA);
208//!check if the string contains the other string (if substring is "" then no match)
209bool containsSubstring(String message, String substring);
210//!check if the string matches
211bool stringMatch(String message, String substring);
212//gets unix time..
214
215//!returns a string in JSON format, such that {'battery':'84'}, {'buzzon':'off'} .. etc
217
218//!returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
220
221#ifdef UNUSED
222//!sets status parts cia a a string in JSON format, such that {'battery':'84'}, {'buzzon':'off'} .. etc
223void main_setStatusJSON(const char* JSONString);
224//!sets status parts cia a a string in JSON format, such that {'battery':'84'}, {'buzzon':'off'} .. etc
225void main_setStatusKeyValue(const char* key, const char* value);
226#endif
227
228//!retrieves the temperature in F.
230
231//!start of the sensor updates ... TODO: tie these to the MQTT messaging as well..
233
234#ifdef NOT_USED
235//!see the external IP .. try 2.21.22
236void getExternalIP();
237#endif
238
239//!feedcount info..
241//increments .. and if MAX goes to 0 -- and sends a message on MQTT
243//! sets the feed count max
245
246//! called by the feed operation to say the device is still running.. and count it as a button click.
247//! Issue #145 8.8.22
249
250/*STEPPER feederType*/
251/*
252#define STEPPER_IS_UNO 1
253#define STEPPER_IS_MINI 2
254#define STEPPER_IS_TUMBLER 3
255 */
256//!get the feeder type (Sepper 1,2,3 ...)
258
259//!returns the max for this feeder
261
262//!SemanticMarker events
263//!This would see a DOCFollow message, and set the value.. then SM10 can display it..
264void setSemanticMarkerDocFollow_mainModule(char* SMDocFollowAddress);
265//!get the latest semantic marker for doc follow
267
268//!sends the SM on the DOCFOLLOW channel (publish it..)
269void sendSemanticMarkerDocFollow_mainModule(const char* SMDocFollowAddress);
270
271//!gets the device name
273
274//!power of devices
275//!reboot
277
278//!power off
280
281//!Semantic Marker definitions
282//! defines the semantic markers for now..
283//! Minimal 0..6 (max = 7)
284#define SM_home_simple 0 //tilt
285#define SM_home_simple_1 1 //buzz
286#define SM_home_simple_2 2 //FEED PAGE
287#define SM_home_simple_3 3 //advanced
288#define SM_HOME_SIMPLE_LAST SM_home_simple_3
289// update: MAX_SM_MIN_MODES when adding one..
290// and update the MINI-1 .. passed from MQTT to BUTTON
291
292//! //! homepage
293#define SM_smart_clicker_homepage 4
294//! //status
295#define SM_status 5
296//!WIFI ssid
297#define SM_WIFI_ssid 6
298//!guest page
299#define SM_guest_page 7
300
301//!AP MODE
302#define SM_ap_mode 8
303
304
305//!guest feed
306#define SM_guest_feed 9
307//!guest feed device
308#define SM_pair_dev 10
309//!HELP
310#define SM_help 11
311
312//! docfollow
313#define SM_doc_follow 12
314//! timer .. todo
315#define SM_timer 13
316//!REboot
317#define SM_reboot 14
318#define SM_LAST SM_reboot
319
320//!note for now, heep the max the same -- so only goes through zoom or not zoom
321//#define MAX_SM_MIN_MODES (SM_help + 1)
322#define MAX_SM_MIN_MODES (SM_HOME_SIMPLE_LAST + 1)
323#define MAX_SM_EXPANDED_MODES (SM_LAST + 1)
324
325
326//! This is needed now as message from external (not button pressing) causes a state change,
327//! but the button processing isn't knowing about it...
328//!sets the current screen mode .. which can be used by Button and Display processing
329//! whichSMMode 0..SM_LAST
330void setCurrentSMMode_mainModule(int whichSMMode);
331//!returns the current SM Mode
333//!increment the currentSMMode, wrapping and using the max menu
335//!increment the currentSMMode, wrapping and using the max menu
337
338//! returns the current max of the menu modes (using the setting of min or expanded) to determine
340//! returns the current max of the MIN menu modes (using the setting of min or expanded) to determine
342//!returns an index from 0..max of SM matching cmd, or -1 if none
343int whichSMMode_mainModule(char *cmd);
344//!returns if a match the mode. whichSMMode is 0..12 and == sm0 .. smn
345boolean matchesSMMode_mainModule(char *cmd, int whichSMMode);
346//!returns string form whichSMMode, sg "sm0", sm1 ...
347char* charSMMode_mainModule(int whichSMMode);
348//!returns which mode in (min or expanded)
350//!toggles the menu mode
352
353//! BLE Discovery Methods
354//! Connected to a BLE device with the advertised name. The syntax can include (PTFeeder:NAME) or just PTFeeder
355//! Being disconnected is already a flag isConnectedBLE ...
356void setConnectedBLEDevice_mainModule(char *deviceName, boolean isGEN3);
357//!whether the connected is a GEN3 (so the name isn't valid).
359//! returns the connected BLE Device name (the :NAME of advertisment, Address: 01:39:3f:33 part of name, or "" if nothing (not null)
361//!returns full name and address, and service.
363//!returns address part of name.
365
366//! BUTOTN PROCESSING abstraction
367//!short press on buttonA (top button)
369//!long press on buttonA (top button)
371//!the long press of the side button
373//!the short press of the side button
375
376//!restarts all the menu states to the first one .. useful for getting a clean start. This doesn't care if the menu is being shown
378
379#endif /* MainModule_h */
void sendSemanticMarkerDocFollow_mainModule(const char *SMDocFollowAddress)
sends the SM on the DOCFOLLOW channel (publish it..)
void cleanEPROM_mainModule()
cleans the main module EPROM
void main_cleanSavedWIFICredentials()
clean the saved WIFI credential, otherwise the AP mode doesn't work (6.3.22)
Definition: MainModule.cpp:726
void takePicture_MainModule()
take a picture (calls the camera module).. what to do with picture??? TODO
Definition: MainModule.cpp:528
void main_updateMQTTInfo(char *ssid, char *ssid_password, char *username, char *password, char *guestPassword, char *deviceName, char *host, char *port, char *locationString)
sets the WIFI and MQTT user/password. It's up to the code to decide who needs to know (currently this...
Definition: MainModule.cpp:741
void setConnectedBLEDevice_mainModule(char *deviceName, boolean isGEN3)
char * connectedBLEDeviceNameAddress_mainModule()
returns address part of name.
void cleanSSID_EPROM_MessageCallback(char *message)
clean the SSID eprom (MQTT_CLEAN_SSID_EPROM)
void toggleMinimalMenuMode_mainModule()
toggles the menu mode
char * charSMMode_mainModule(int whichSMMode)
returns string form whichSMMode, sg "sm0", sm1 ...
bool stringMatch(String message, String substring)
check if the string matches
void loop_mainModule()
called for the loop() of this plugin
Definition: MainModule.cpp:114
char * getSemanticMarkerDocFollow_mainModule()
get the latest semantic marker for doc follow
void registerCallbackMain(int callbacksModuleId, int callbackType, void(*callback)(char *))
register the callback based on the callbackType. use the callbacksModuleId for which one....
Definition: MainModule.cpp:316
char * deviceName_mainModule()
gets the device name
Definition: MainModule.cpp:607
boolean connectedBLEDeviceIsGEN3_mainModule()
whether the connected is a GEN3 (so the name isn't valid).
int minMenuModesMax_mainModule()
returns the current max of the MIN menu modes (using the setting of min or expanded) to determine
void processClientCommandChar_mainModule(char cmd)
single character version of processClientCommand (since many used that already)
int getFeedCount_mainModule()
feedcount info..
Definition: MainModule.cpp:155
void onStatusMessageBLEServerCallback(char *message)
** The callback for "status messages" of the bluetooth
Definition: MainModule.cpp:411
char * main_currentStatusURL()
returns a string in in URL so: status&battery=84'&buzzon='off' } .. etc
char * main_JSONStringForWIFICredentials()
retrieve a JSON string for the ssid and ssid_password: {'ssid':<ssid>,'ssidPassword':<pass>"}
Definition: MainModule.cpp:663
int feedCountMax_mainModule()
returns the max for this feeder
Definition: MainModule.cpp:137
void solidLight(char *message)
callback for SOLID blinking led
void main_credentialsUpdated()
moved here 4.25.22 (entirely from ESP_IOT.ino)
Definition: MainModule.cpp:392
void setup_mainModule()
added 9.29.22 to support wildcards #196
Definition: MainModule.cpp:72
boolean stopAllProcesses_mainModule()
if stopped
Definition: MainModule.cpp:21
void restartAllMenuStates_mainModule()
restarts all the menu states to the first one .. useful for getting a clean start....
void setSemanticMarkerDocFollow_mainModule(char *SMDocFollowAddress)
sed the address to follow
int whichSMMode_mainModule(char *cmd)
returns an index from 0..max of SM matching cmd, or -1 if none
void main_dispatchAsyncCommandWithString(int asyncCallCommand, char *parameter)
Definition: MainModule.cpp:807
void initAsyncCallFlags()
storage for asyncCallCommands
Definition: MainModule.cpp:770
void solidLightMessageCallback(char *message)
callback for SOLID blinking led
boolean isEmptyString(char *stringA)
if an empty string
void rebootDevice_mainModule()
Definition: MainModule.cpp:616
void poweroff_mainModule()
power off
Definition: MainModule.cpp:623
int maxMenuModes_mainModule()
returns the current max of the menu modes (using the setting of min or expanded) to determine
void buttonB_ShortPress_mainModule()
the short press of the side button
bool containsSubstring(String message, String substring)
check if the string contains the other string (if substring is "" then no match)
Definition: MainModule.cpp:196
void decrementSMMode_mainModule()
increment the currentSMMode, wrapping and using the max menu
void callCallbackMain(int callbacksModuleId, int callbackType, char *message)
performs the indirect callback based on the callbackType
Definition: MainModule.cpp:333
int getCurrentSMMode_mainModule()
returns the current SM Mode
void stopProcessesForOTAUpdate_mainModule()
stop all loops... while OTA working..
Definition: MainModule.cpp:11
void feedMessageCallback(char *message)
Definition: MainModule.cpp:352
void resetFeedCount_mainModule()
sets the feed count max
Definition: MainModule.cpp:190
void sendMessageString_mainModule(char *messageString)
adding a synchronous call to send a message over the network (assuming MQTT but not specified),...
char * main_currentStatusJSON()
returns a string in JSON format, such that {'battery':'84'}, {'buzzon':'off'} .. etc
char * getFullBLEDeviceName_mainModule()
returns full name and address, and service.
int getFeederType_mainModule()
get the feeder type (Sepper 1,2,3 ...)
Definition: MainModule.cpp:161
void buttonA_LongPress_mainModule()
long press on buttonA (top button)
void invokeAsyncCommands()
checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to ...
Definition: MainModule.cpp:829
boolean startsWithChar(char *str, char c)
a char* version of startsWith (after skipping spaces)
void setCurrentSMMode_mainModule(int whichSMMode)
sets the current screen mode .. which can be used by Button and Display processing
void onWriteBLEServerCallback(char *message)
The callback for "onWrite" of the bluetooth "onWrite'.
Definition: MainModule.cpp:511
void main_dispatchAsyncCommand(int asyncCallCommand)
dispatches a call to the command specified. This is run on the next loop()
Definition: MainModule.cpp:789
boolean matchesSMMode_mainModule(char *cmd, int whichSMMode)
returns if a match the mode. whichSMMode is 0..12 and == sm0 .. smn
void refreshDelayButtonTouched_MainModule()
called by the feed operation to say the device is still running.. and count it as a button click.
Definition: MainModule.cpp:536
char * connectedBLEDeviceName_mainModule()
returns the connected BLE Device name (the :NAME of advertisment, Address: 01:39:3f:33 part of name,...
boolean isMinimalMenuMode_mainModule()
returns which mode in (min or expanded)
int getTimeStamp_mainModule()
void buttonB_LongPress_mainModule()
the long press of the side button
void blinkMessageCallback(char *message)
callback for blinking led
void restartProcessesForOTAUpdate_mainModule()
restart all loops... while OTA working..
Definition: MainModule.cpp:16
char * createCopy(char *stringA)
void main_printModuleConfiguration()
void solidLightOnOff(boolean flag)
supports turning on the solid light..
void singleClickTouched(char *whichButton)
float getTemperature_mainModule()
retrieves the temperature in F.
void buttonA_ShortPress_mainModule()
char * main_nextJSONWIFICredential()
! cycle through the next WIFI saved credential - return next one that isn't our current one....
Definition: MainModule.cpp:669
void onBLEServerCallback(char *message)
The callback for "onWrite" of the bluetooth "onWrite'.
Definition: MainModule.cpp:404
void incrementSMMode_mainModule()
increment the currentSMMode, wrapping and using the max menu
void incrementFeedCount_mainModule()
increments .. and if MAX goes to 0 – and sends a message on MQTT
Definition: MainModule.cpp:168
float getBatPercentage_mainModule()
start of the sensor updates ... TODO: tie these to the MQTT messaging as well..