ESP_IOT v2.5
IOT ESP Coding
main.cpp
Go to the documentation of this file.
1/*
2 Name: NimBLE_PetTutor_Server.ino
3 Created: 8/25/2021 7:49:11 PM
4 Author: wes and scottl
5*/
6
7//!defines.h defines the #ifdef USE_MODULE statements, as they are included elsewhere.. Don't define them here.
8#include "../Defines.h"
9
10//! TRICK: has to compile with Arduino .. and the loop() is in .ino file
11//! MUST: define PLATFORM_IO (or not)
12
13/*
14 Name: ESP_BOOTSTRAP.ino
15 Created: 6.19.25
16 Author: scott
17 */
18
19
20
21
22#ifdef ESP_M5
23//NEED to fix the port to use... /dev/cu.usbserial-61CEEBBFAD
24//todo..
25//#define HIGH 1
26#define LED 2
27//#include "src/ButtonModule/ButtonProcessing.h"
28//NOTE: the Board has to be modified to M5Stick-C-Plus
29
30//called from BLEServerNetworking, MainModule and StepperModule(Dispense.cpp)
32{
33 SerialError.printf("UNDEFINED: stepperModule_processClientCmd: %c\n", cmd);
34}
35#else
36
37#endif //ESP_M5
38
39
40//NOTE: there might be an issue that the REGISTERED methods aren't done yet .. so cannot call them..
42
43//forward definition
44void finishSetup();
45
46#include "nvs_flash.h"
47// ******************************************
48//!note: setup only called one time...
49//! so, the first time the doneWIFIModuleCredentials is set.. call setup()
50//!main setup
51#ifdef USE_PLATFORM_IO
52void setup()
53#else
55#endif
56{
57 //! start begin here
59
60#ifdef ERASE_ALL
61 //https://github.com/espressif/arduino-esp32/issues/2144
62 ESP_ERROR_CHECK(nvs_flash_erase());
63 nvs_flash_init();
64
65#endif
66 //#define TRY_NEW_SERIAL_WAIT
67 //! https://forum.arduino.cc/t/do-i-really-need-while-serial/1183292/16
68 //! trying to get the debug to printout always, not just on REBOOT
69 Serial.begin(115200);
70
71#ifdef TRY_NEW_SERIAL_WAIT
72 while (!Serial){};
73#endif
74 Serial.println("ESP_IOT Startup");
75
76#ifdef M5UNIFIED
77 //! added 8.29.23 .. figure if this is right!
78 //! 9.25.23 .. took out .. might be cause of crash..
79 pinMode(LED, OUTPUT);
80 //! 5.3.25 register our PIN use
81 registerPinUse_mainModule(LED, "LED_OUTPUT", "ESP_IOT", false);
82#endif
83
84 //This must be first .. as it inializes the display and Serial Ports..
85 //! called for the loop() of this plugin
87
88 SerialInfo.println("******************");
89 SerialMin.println(VERSION);
90
91 //Timestamp the compile
92 SerialDebug.print("Compiled ");
93 SerialDebug.print(__DATE__);
94 SerialDebug.print(" @ ");
95 SerialDebug.println(__TIME__);
96
97 //not good time .. since no WIFI to get global time..
98 // printInt_SPIFFModule(getTimeStamp_mainModule());
99 // println_SPIFFModule((char*)" time");
100
101 //called in setup() to init the async calls
103
104 //!4.17.22
105 //! MainModule does all the preferences, etc..
106 //! ***** MUST BE CALLED BEFORE CODE BELOW..
108
110 SerialInfo.println("******************");
111
112 //! 8.17.25 register when SSID completed
113 //registerCallbackMain(CALLBACKS_MQTT, )
114
115#ifdef USE_SPIFF_MODULE
116 //!SPIFF must be after main_setup() since it reads preferences .. is first so debug can be saved
118 //println_SPIFFModule((char*)VERSION);
119 println_SPIFFModule_JSON((char*)"version",(char*)VERSION);
120
121#endif
122
123 //! 2.4.24 (2.4.68 lisa birthday). Mine 2.5.58 tomorrow
124 //! The setup_Buttons_mainModule() is really a setup_Sensors so
125 //! take away the #ifdef
126 //calls the setup() for ButtonModule
128#ifdef USE_BUTTON_MODULE
129 //register the single click
131#endif
132
133
134 //!12.8.22 add audio
135#ifdef USE_AUDIO_MODULE
137#endif
138
139#ifdef USE_STEPPER_MODULE
140 //reads credentials, etc..
142
143#ifdef FEED_ON_STARTUP
144 //! 6.6.25 this is because the M5Atom on power-on will send current to the feeder
145 //! so when we get here .. we try to break the "feed" by actually feeding for a fraction of a second 0.0
146 // setup_StepperModule();
147 {
148 SerialDebug.println("stopMotor ..");
150 }
151#endif //FEED ON STARTUP
152
153#endif //USE_STEPPER_MODULE
154
155#ifdef USE_UI_MODULE
156 //setup the stepper for the feeder
158#endif
159
160 SerialDebug.println("Starting PetTutor_Server");
161 SerialDebug.printf("M5STACK VERSION = %s\n", M5STACK_VERSION);
162
163 //APril 8, refactoring. Moved up to here..
164 //! 8.16.25 MQTT
165
166 //pass the callbacks, (1) the message callback, and the (blinkLed) implementation
167 //setMessageCallbacks(&feedMessageCallback, &blinkMessageCallback);
168 //register the 2 callbacks for now..
172
173 //3.25.22 -- register the cleanSSID_EPROM
175
176 //NOTE: 6.3.22 .. this is breaking the AP mode, preSetup_WIF_APModule must be called first..
177 //try call from loop():
178 // setup_MQTTNetworking();
179
180 //the tilt 5.2.22 (no one calls this ...)
182
183 //! 8.16.25 WIFI AP
184 //called to validate the doneWIFI() call...
186
187 //init the BLE here.
188 //! 8.16.25 BLE SERVER
189 //yes
190 //!if the BLE_SERVER is turned on..
191 boolean useBLEServer = true;
192#ifdef ESP_M5
194 //hardwire #### FIX
195 //useBLEServer = false;
196#endif
197 if (useBLEServer)
198 {
199 //Initialize the BLE Server, using the service name PTFeeder
200 //https://stackoverflow.com/questions/20944784/why-is-conversion-from-string-constant-to-char-valid-in-c-but-invalid-in-c
201 //! 8.16.25 MQTT
202
203 //*** The callback for "onWrite" of the bluetooth "onWrite'
205 //*** The callback for "onWrite" of the bluetooth "onWrite'
208
209 {
210 //! 10.10.25 #405 #406
211 //! see if the device is a PTClicker if the M5Atom class is one..
212 char *serverServiceName = getServerServiceName_mainModule();
213
214 //strdup() get away from the
216 SerialDebug.printf("done setupBLEServerNetworking: %s:%s\n", serverServiceName, getDeviceNameMQTT());
217 }
218 }
219
220 //BLE Client --
221 //! 8.16.25 BLE CLIENT
222 setup_BLEClientNetworking(MAIN_BLE_CLIENT_SERVICE, (char*) PT_SERVICE_UUID, (char*) PT_CHARACTERISTIC_UUID);
223
224 //MAJOR ISSUE: if the port/server are wrong .. can never reset unless we can get a BUTTON..
225 //If BOOTSTRAP (a compile time ifdef in Defines.h is set, then bypass the AP mode, and use the constants in MQTTNetworking.cpp
226#ifdef BOOTSTRAP
227#else
229 {
230 //Turn on light until out of the AP mode..
231 solidLightOnOff(true);
232 //test
234
235 //set flag ..
237
239 }
240#endif //BOOTSTRAP
241
242}
243
244// **************** finishSetup()
245//! called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode..
247{
248 //! 8.16.25 WIFI AP
249 //basically after the AP module finished, the function WIFI_APModule_JsonOutputString() stores the credentials (versus them calling here ..)
251 {
252 //Turn on light until out of the AP mode..
253 solidLightOnOff(false);
254
255 String jsonOutputString = WIFI_APModule_JsonOutputString();
256 // now this could be passed onto the MQTT processor..
257 SerialDebug.print("From WIFI -> JSON = ");
258 SerialDebug.println(jsonOutputString);
259
260 //! 8.16.25 MQTT
261 //moved here 6.3.22
263
264 //! 8.16.25 WIFI AP
265 //pass the json string onto the MQTT code..
266 // 3.26.22
267 SerialDebug.println("calling .. processJSONMessage");
268
269 //NOTE: if done was because credentials .. then don't call this..
270 //basically if MQTT running .. don't call this..
271 processJSONMessageMQTT(&jsonOutputString[0], NULL);
272
273 //NOTE: we are still in the BLE callback .. and then we call setup_MQTTNetworking..
274 //LETS try setting state in MQTT that is waiting for WIFI..
275
276 //perform the setup() in the MQTTNetworking
277 //NOTE: this is called 2 times if processJSONMessage() sets the eprom data .. since the network was already setup
278 //there is a state flag internally that won't setup 2 times (unless MQTT was kicked out via AP mode, or cleaned credentials)
279 //try call from loop():
280 //setup_MQTTNetworking();
281
282 // sets the _chipName (from MQTT Module)
283 char *deviceName = getDeviceNameMQTT();
284
285 //THIS should output the device name, user name, etc...
286 // SerialDebug.print("pFoodCharacteristic->setValue: ");
287 // SerialDebug.println(_fullJSONString);
288 SerialDebug.print("DeviceName: ");
289 SerialDebug.println(deviceName);
290
291 //! 8.16.25 WIFI AP
292 } //doneWIFI_APModule_Credentials()
293
294 //! try a call.. NOTE, if the WIFI isn't working, this won't work either..
295#ifdef USE_REST_MESSAGING
296 setupSecureRESTCall();
297#endif
298
299 //! 1.20.24 setup the camera after the WIFI is working ..
300 //! 8.16.25 do this now with the OO module in main..
301#ifdef USE_CAMERA_MODULE_x
302 setup_CameraModule();
303#endif
304
305}
306
307// ****************************************
308//! main loop() of the Arduino runtime
309// ****************************************
310#ifdef USE_PLATFORM_IO
311//! 8.12.26 need to be named loop() but not
312void loop()
313#else
314 void mainLoop()
315#endif
316{
317 //#define USE_STOP_ALL
318
319 //! try a call..
320#ifdef USE_REST_MESSAGING
321 // sendSecureRESTCall("/help");
322#endif
323
324#ifdef USE_STOP_ALL
326 {
327 delay(100);
328 return;
329 }
330#endif
331
332 //!4.17.22
334
335 //! 8.16.25
336 //! TODO: change these loops to the M5Sensor plugin model..
337#ifdef USE_AUDIO_MODULE
338 //!12.8.22 add audio
340#endif
341
342 //! 8.16.25 do this now with the OO module in main..
343#ifdef USE_CAMERA_MODULE_x
344 loop_CameraModule();
345#endif
346
347 //! 8.16.25 WIFI AP
349 {
350 //since the MQTT and BLE were not started, the first time the WIFI credentials are finished (after submit)
351 // finish the setup (which are the MQTT and WIFI.
352 //NOTE: this needs to get the credentials in JSON format and store ..
354 {
356 SerialDebug.println("doneWIFI_APModuleCredentials, and now calling finishSetup()");
357 finishSetup();
358 }
359 }
360
361 //! called to check if any Async Commands are in dispatch mode
363
364 //! note: displayModule() musts be called before buttonModule (as the "model" is updated (initiated) in the display module
365 //! called for the loop() of this plugin (this is almost the "frames" of the graphic, as fast as the runtime can make this loop! FAST.. 24 fps ??
367
368 //! loop on the buttons
370
371 /** Do your thing here, this just spams notifications to all connected clients */
372#ifdef USE_STEPPER_MODULE
373 loop_StepperModule(); //was FeederStateMachine
374#endif //USE_STEPPER_MODULE
375
376#ifdef USE_UI_MODULE
378#endif
379
380 //BLE Client --
381 //! 8.16.25 BLE CLIENT
383
384
385 //** NOTE: the WIFI_AP_MODULE is tricky. The doneWIFI_APModule_Credentials() is set when done, but also when the MQTT networking is running (meaning the credentials were specified via BLE, or from EPROM)
386 //! 8.16.25 WIFI AP
387 //if the APmodule credentials are finished .. then continue normal loop
388 // Otherwise just BLE and AP (for now)
390 {
391 /*******************************MQTT*************************************/
392 //! 8.16.25 MQTT
394
395
396 //! 8.16.25 BLE SERVER
397 //!if the BLE_SERVER is turned on..
398 boolean useBLEServer = true;
399#ifdef ESP_M5
401#endif
402 if (useBLEServer)
403 //! old ESP32 didn't have the "server" turned on..
405
406
407 //! 8.16.25 WIFI AP
408 } //!doneWIFI_APModuleCredentials
409
410 //! 8.16.25 WIFI AP
412 {
414 }
415
416 //APril 8, 2022
417 //loop always, we just don't have WIFI working yet...?
418 //NOTE: the loopBLE will call the register for a message (and call processJSONMessage on BLE commands)
419 //NEED: to set the doneWIFI_APModuleCredentials iff a processJSONMessage resulted in credential updates,
420 // not all JSON are credentials. Others are setting options, like OTA. -- and kill the AP module..
421 //UNFORTUNATELY .. the WIFI and MQTT might fail .. and slow things down. NEED a better way to
422 // run the MQTT loop (so others can get some time, like BLE).
423
424#ifdef ESP_32
425 delay(100); //this delays the feed trigger response
426#else
427 delay(50); // 1.26.24 try a smaller loop delay
428#endif
429} //loop()
void loop_AudioModule()
called for the loop() of this plugin
void setup_AudioModule()
void loop_BLEClientNetworking()
the loop()
void setup_BLEClientNetworking(char *serviceName, char *serviceUUID, char *characteristicUUID)
the setup() and loop() passing the serviceName to look for..
void loop_BLEServerNetworking()
the 'loop' for this module BLEServerNetworking.
void setup_BLEServerNetworking(char *serviceName, char *deviceName, char *serviceUUID, char *characteristicUUID)
the 'setup' for this module BLEServerNetworking. Here the service name is added (and potentially more...
#define BLE_SERVER_CALLBACK_STATUS_MESSAGE
used to send a string message back (which might be sent to MQTT for example)
#define BLE_SERVER_CALLBACK_ONWRITE
void loop_displayModule()
called for the loop() of this plugin
void setup_displayModule()
for types: String, boolean ..
void setup()
Definition: ESP_IOT.ino:20
void loop()
Definition: ESP_IOT.ino:16
void setup_MQTTNetworking()
setup the MQTT part of networking
void loop_MQTTNetworking()
called for the loop() of this plugin
boolean processJSONMessageMQTT(char *ascii, char *topic)
process the JSON message (looking for FEED, etc). Note: topic can be nil, or if not,...
char * getDeviceNameMQTT()
called for things like the advertisement
#define MQTT_CLEAN_SSID_EPROM
#define MQTT_CALLBACK_BLINK
#define MQTT_CALLBACK_FEED
8.16.25 MQTT
#define MQTT_CALLBACK_SOLID_LIGHT
#define MQTT_CALLBACK_TILT_MOTION_DETECTED
#define CALLBACKS_MQTT
8.16.25 MQTT
Definition: MainModule.cpp:789
boolean registerPinUse_mainModule(long pin, String pinName, String moduleName, boolean isI2C)
void cleanSSID_EPROM_MessageCallback(char *message)
clean the SSID eprom (MQTT_CLEAN_SSID_EPROM)
void loop_mainModule()
called for the loop() of this plugin
Definition: MainModule.cpp:541
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:881
#define CALLBACKS_BLE_SERVER
Definition: MainModule.cpp:792
void onStatusMessageBLEServerCallback(char *message)
The callback for "status messages" of the bluetooth.
void solidLightOnOff(boolean onOff)
callback for SOLID blinking led
void setup_mainModule()
called from the setup()
Definition: MainModule.cpp:412
boolean stopAllProcesses_mainModule()
if stopped
Definition: MainModule.cpp:358
void initAsyncCallFlags()
initialize the async call flags (with and without parameters)
void solidLightMessageCallback(char *message)
callback for SOLID blinking led
void stopMotor_mainModule()
added 9.29.22 to support wildcards #196
Definition: MainModule.cpp:308
void feedMessageCallback(char *message)
Definition: MainModule.cpp:977
void setup_Sensors_mainModule()
1.22.24 setup of buttons
void invokeAsyncCommands()
checks if any async commands are in 'dispatch' mode, and if so, invokes them, and sets their flag to ...
void onWriteBLEServerCallback(char *message)
The callback for "onWrite" of the bluetooth "onWrite'.
char * getServerServiceName_mainModule()
Definition: MainModule.cpp:336
void loop_Sensors_mainModule()
1.22.24 add setup and loop at main so it can call appropriate plugs
void blinkMessageCallback(char *message)
callback for blinking led
void main_printModuleConfiguration()
void singleClickTouched(char *whichButton)
#define CALLBACKS_BUTTON_MODULE
Definition: MainModule.cpp:790
void espBegin_mainModule(int num)
Definition: MainModule.cpp:508
#define SINGLE_CLICK_BM
Definition: MainModule.h:97
#define PT_CHARACTERISTIC_UUID
Definition: MainModule.h:59
#define PT_SERVICE_UUID
Definition: MainModule.h:58
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define PREFERENCE_MAIN_BLE_SERVER_VALUE
void println_SPIFFModule_JSON(char *attribute, char *value)
4.4.24 output a line in JSON format adding timestamp as well
void setup_SPIFFModule()
the setup for this module
void loop_StepperModule()
the main loop fro the StepperModule. This used be called FeederStateMachine()
void setup_StepperModule()
called on setup()
void setup_UIModule()
UIModule
Definition: UI.cpp:13
void loop_UIModule()
Definition: UI.cpp:14
String WIFI_APModule_JsonOutputString()
retrieves the JSON string
void loop_WIFI_APModule()
main loop
void setup_WIFI_APModule()
sets the config in the EPPROM called wifi-config. NOTE: the _done isn't valid until after this code....
void WIFI_APModule_ResetCredentials()
resets the credentials
boolean doneWIFI_APModule_Credentials()
called to see if the WIFIModule has finished bootstrapping..
void preSetup_WIFI_APModule()
void mainLoop()
main loop() of the Arduino runtime
Definition: main.cpp:314
boolean _finishSetupCalledAlready
Definition: main.cpp:41
#define LED
defines.h defines the #ifdef USE_MODULE statements, as they are included elsewhere....
Definition: main.cpp:26
void stepperModule_ProcessClientCmd(char cmd)
Definition: main.cpp:31
void finishSetup()
called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode....
Definition: main.cpp:246
void mainSetup()
Definition: main.cpp:54