ESP_IOT v2.5
IOT ESP Coding
ESP_IOT.ino
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//#define useMAIN
11#ifdef USE_PLATFORM_IO
12//! nothing .. it's all in src/main.cpp
13#else
14export void mainLoop();
15export void mainSetup();
16void loop()
17{
18 mainLoop();
19}
20void setup()
21{
22 mainSetup();
23}
24#endif
25
26//! 8.12.26 moved to src/main.cpp
27#ifdef useMAIN
28
29#ifdef ESP_M5
30//NEED to fix the port to use... /dev/cu.usbserial-61CEEBBFAD
31//todo..
32//#define HIGH 1
33#define LED 2
34//#include "src/ButtonModule/ButtonProcessing.h"
35//NOTE: the Board has to be modified to M5Stick-C-Plus
36
37//called from BLEServerNetworking, MainModule and StepperModule(Dispense.cpp)
39{
40 SerialError.printf("UNDEFINED: stepperModule_processClientCmd: %c\n", cmd);
41}
42#else
43
44#endif //ESP_M5
45
46
47//NOTE: there might be an issue that the REGISTERED methods aren't done yet .. so cannot call them..
48boolean _finishSetupCalledAlready = false;;
49
50//forward definition
51void finishSetup();
52
53#include "nvs_flash.h"
54// ******************************************
55//!note: setup only called one time...
56//! so, the first time the doneWIFIModuleCredentials is set.. call setup()
57//!main setup
58void setup()
59{
60 //! start begin here
62
63#ifdef ERASE_ALL
64 //https://github.com/espressif/arduino-esp32/issues/2144
65 ESP_ERROR_CHECK(nvs_flash_erase());
66 nvs_flash_init();
67
68#endif
69 //#define TRY_NEW_SERIAL_WAIT
70 //! https://forum.arduino.cc/t/do-i-really-need-while-serial/1183292/16
71 //! trying to get the debug to printout always, not just on REBOOT
72 Serial.begin(115200);
73
74#ifdef TRY_NEW_SERIAL_WAIT
75 while (!Serial){};
76#endif
77 Serial.println("ESP_IOT Startup");
78
79#ifdef M5UNIFIED
80 //! added 8.29.23 .. figure if this is right!
81 //! 9.25.23 .. took out .. might be cause of crash..
82 pinMode(LED, OUTPUT);
83 //! 5.3.25 register our PIN use
84 registerPinUse_mainModule(LED, "LED_OUTPUT", "ESP_IOT", false);
85#endif
86
87 //This must be first .. as it inializes the display and Serial Ports..
88 //! called for the loop() of this plugin
90
91 SerialInfo.println("******************");
92 SerialMin.println(VERSION);
93
94 //Timestamp the compile
95 SerialDebug.print("Compiled ");
96 SerialDebug.print(__DATE__);
97 SerialDebug.print(" @ ");
98 SerialDebug.println(__TIME__);
99
100 //not good time .. since no WIFI to get global time..
101 // printInt_SPIFFModule(getTimeStamp_mainModule());
102 // println_SPIFFModule((char*)" time");
103
104 //called in setup() to init the async calls
106
107 //!4.17.22
108 //! MainModule does all the preferences, etc..
109 //! ***** MUST BE CALLED BEFORE CODE BELOW..
111
113 SerialInfo.println("******************");
114
115 //! 8.17.25 register when SSID completed
116 //registerCallbackMain(CALLBACKS_MQTT, )
117
118#ifdef USE_SPIFF_MODULE
119 //!SPIFF must be after main_setup() since it reads preferences .. is first so debug can be saved
121 //println_SPIFFModule((char*)VERSION);
122 println_SPIFFModule_JSON((char*)"version",(char*)VERSION);
123
124#endif
125
126 //! 2.4.24 (2.4.68 lisa birthday). Mine 2.5.58 tomorrow
127 //! The setup_Buttons_mainModule() is really a setup_Sensors so
128 //! take away the #ifdef
129 //calls the setup() for ButtonModule
131#ifdef USE_BUTTON_MODULE
132 //register the single click
134#endif
135
136
137 //!12.8.22 add audio
138#ifdef USE_AUDIO_MODULE
140#endif
141
142#ifdef USE_STEPPER_MODULE
143 //reads credentials, etc..
145
146#ifdef FEED_ON_STARTUP
147 //! 6.6.25 this is because the M5Atom on power-on will send current to the feeder
148 //! so when we get here .. we try to break the "feed" by actually feeding for a fraction of a second 0.0
149 // setup_StepperModule();
150 {
151 SerialDebug.println("stopMotor ..");
153 }
154#endif //FEED ON STARTUP
155
156#endif //USE_STEPPER_MODULE
157
158#ifdef USE_UI_MODULE
159 //setup the stepper for the feeder
161#endif
162
163 SerialDebug.println("Starting PetTutor_Server");
164 SerialDebug.printf("M5STACK VERSION = %s\n", M5STACK_VERSION);
165
166 //APril 8, refactoring. Moved up to here..
167 //! 8.16.25 MQTT
168
169 //pass the callbacks, (1) the message callback, and the (blinkLed) implementation
170 //setMessageCallbacks(&feedMessageCallback, &blinkMessageCallback);
171 //register the 2 callbacks for now..
175
176 //3.25.22 -- register the cleanSSID_EPROM
178
179 //NOTE: 6.3.22 .. this is breaking the AP mode, preSetup_WIF_APModule must be called first..
180 //try call from loop():
181 // setup_MQTTNetworking();
182
183 //the tilt 5.2.22 (no one calls this ...)
185
186 //! 8.16.25 WIFI AP
187 //called to validate the doneWIFI() call...
189
190 //init the BLE here.
191 //! 8.16.25 BLE SERVER
192 //yes
193 //!if the BLE_SERVER is turned on..
194 boolean useBLEServer = true;
195#ifdef ESP_M5
197 //hardwire #### FIX
198 //useBLEServer = false;
199#endif
200 if (useBLEServer)
201 {
202 //Initialize the BLE Server, using the service name PTFeeder
203 //https://stackoverflow.com/questions/20944784/why-is-conversion-from-string-constant-to-char-valid-in-c-but-invalid-in-c
204 //! 8.16.25 MQTT
205
206 //*** The callback for "onWrite" of the bluetooth "onWrite'
208 //*** The callback for "onWrite" of the bluetooth "onWrite'
210
211 {
212 //! 10.10.25 #405 #406
213 //! see if the device is a PTClicker if the M5Atom class is one..
214 char *serverServiceName = getServerServiceName_mainModule();
215
216 //strdup() get away from the
218 SerialDebug.printf("done setupBLEServerNetworking: %s:%s\n", serverServiceName, getDeviceNameMQTT());
219 }
220 }
221
222 //BLE Client --
223 //! 8.16.25 BLE CLIENT
224 setup_BLEClientNetworking(MAIN_BLE_CLIENT_SERVICE, (char*) PT_SERVICE_UUID, (char*) PT_CHARACTERISTIC_UUID);
225
226 //MAJOR ISSUE: if the port/server are wrong .. can never reset unless we can get a BUTTON..
227 //If BOOTSTRAP (a compile time ifdef in Defines.h is set, then bypass the AP mode, and use the constants in MQTTNetworking.cpp
228#ifdef BOOTSTRAP
229#else
231 {
232 //Turn on light until out of the AP mode..
233 solidLightOnOff(true);
234 //test
236
237 //set flag ..
239
241 }
242#endif //BOOTSTRAP
243
244}
245
246// **************** finishSetup()
247//! called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode..
248void finishSetup()
249{
250 //! 8.16.25 WIFI AP
251 //basically after the AP module finished, the function WIFI_APModule_JsonOutputString() stores the credentials (versus them calling here ..)
253 {
254 //Turn on light until out of the AP mode..
255 solidLightOnOff(false);
256
257 String jsonOutputString = WIFI_APModule_JsonOutputString();
258 // now this could be passed onto the MQTT processor..
259 SerialDebug.print("From WIFI -> JSON = ");
260 SerialDebug.println(jsonOutputString);
261
262 //! 8.16.25 MQTT
263 //moved here 6.3.22
265
266 //! 8.16.25 WIFI AP
267 //pass the json string onto the MQTT code..
268 // 3.26.22
269 SerialDebug.println("calling .. processJSONMessage");
270
271 //NOTE: if done was because credentials .. then don't call this..
272 //basically if MQTT running .. don't call this..
273 processJSONMessageMQTT(&jsonOutputString[0], NULL);
274
275 //NOTE: we are still in the BLE callback .. and then we call setup_MQTTNetworking..
276 //LETS try setting state in MQTT that is waiting for WIFI..
277
278 //perform the setup() in the MQTTNetworking
279 //NOTE: this is called 2 times if processJSONMessage() sets the eprom data .. since the network was already setup
280 //there is a state flag internally that won't setup 2 times (unless MQTT was kicked out via AP mode, or cleaned credentials)
281 //try call from loop():
282 //setup_MQTTNetworking();
283
284 // sets the _chipName (from MQTT Module)
285 char *deviceName = getDeviceNameMQTT();
286
287 //THIS should output the device name, user name, etc...
288 // SerialDebug.print("pFoodCharacteristic->setValue: ");
289 // SerialDebug.println(_fullJSONString);
290 SerialDebug.print("DeviceName: ");
291 SerialDebug.println(deviceName);
292
293 //! 8.16.25 WIFI AP
294 } //doneWIFI_APModule_Credentials()
295
296 //! try a call.. NOTE, if the WIFI isn't working, this won't work either..
297#ifdef USE_REST_MESSAGING
298 setupSecureRESTCall();
299#endif
300
301 //! 1.20.24 setup the camera after the WIFI is working ..
302 //! 8.16.25 do this now with the OO module in main..
303#ifdef USE_CAMERA_MODULE_x
304 setup_CameraModule();
305#endif
306
307}
308
309// ****************************************
310//! main loop() of the Arduino runtime
311// ****************************************
312void loop() {
313 //#define USE_STOP_ALL
314
315 //! try a call..
316#ifdef USE_REST_MESSAGING
317 // sendSecureRESTCall("/help");
318#endif
319
320#ifdef USE_STOP_ALL
322 {
323 delay(100);
324 return;
325 }
326#endif
327
328 //!4.17.22
330
331 //! 8.16.25
332 //! TODO: change these loops to the M5Sensor plugin model..
333#ifdef USE_AUDIO_MODULE
334 //!12.8.22 add audio
336#endif
337
338 //! 8.16.25 do this now with the OO module in main..
339#ifdef USE_CAMERA_MODULE_x
340 loop_CameraModule();
341#endif
342
343 //! 8.16.25 WIFI AP
345 {
346 //since the MQTT and BLE were not started, the first time the WIFI credentials are finished (after submit)
347 // finish the setup (which are the MQTT and WIFI.
348 //NOTE: this needs to get the credentials in JSON format and store ..
350 {
352 SerialDebug.println("doneWIFI_APModuleCredentials, and now calling finishSetup()");
353 finishSetup();
354 }
355 }
356
357 //! called to check if any Async Commands are in dispatch mode
359
360 //! note: displayModule() musts be called before buttonModule (as the "model" is updated (initiated) in the display module
361 //! 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 ??
363
364 //! loop on the buttons
366
367 /** Do your thing here, this just spams notifications to all connected clients */
368#ifdef USE_STEPPER_MODULE
369 loop_StepperModule(); //was FeederStateMachine
370#endif //USE_STEPPER_MODULE
371
372#ifdef USE_UI_MODULE
374#endif
375
376 //BLE Client --
377 //! 8.16.25 BLE CLIENT
379
380
381 //** 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)
382 //! 8.16.25 WIFI AP
383 //if the APmodule credentials are finished .. then continue normal loop
384 // Otherwise just BLE and AP (for now)
386 {
387 /*******************************MQTT*************************************/
388 //! 8.16.25 MQTT
390
391
392 //! 8.16.25 BLE SERVER
393 //!if the BLE_SERVER is turned on..
394 boolean useBLEServer = true;
395#ifdef ESP_M5
397#endif
398 if (useBLEServer)
399 //! old ESP32 didn't have the "server" turned on..
401
402
403 //! 8.16.25 WIFI AP
404 } //!doneWIFI_APModuleCredentials
405
406 //! 8.16.25 WIFI AP
408 {
410 }
411
412 //APril 8, 2022
413 //loop always, we just don't have WIFI working yet...?
414 //NOTE: the loopBLE will call the register for a message (and call processJSONMessage on BLE commands)
415 //NEED: to set the doneWIFI_APModuleCredentials iff a processJSONMessage resulted in credential updates,
416 // not all JSON are credentials. Others are setting options, like OTA. -- and kill the AP module..
417 //UNFORTUNATELY .. the WIFI and MQTT might fail .. and slow things down. NEED a better way to
418 // run the MQTT loop (so others can get some time, like BLE).
419
420#ifdef ESP_32
421 delay(100); //this delays the feed trigger response
422#else
423 delay(50); // 1.26.24 try a smaller loop delay
424#endif
425} //loop()
426
427#endif
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
export void mainSetup()
Definition: main.cpp:54
export void mainLoop()
defines.h defines the #ifdef USE_MODULE statements, as they are included elsewhere....
Definition: main.cpp:314
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()
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