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