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..
88
90 SerialInfo.println("******************");
91
92 //! 8.17.25 register when SSID completed
93 //registerCallbackMain(CALLBACKS_MQTT, )
94
95#ifdef USE_SPIFF_MODULE
96 //!SPIFF must be after main_setup() since it reads preferences .. is first so debug can be saved
98 //println_SPIFFModule((char*)VERSION);
99 println_SPIFFModule_JSON((char*)"version",(char*)VERSION);
100
101#endif
102
103 //! 2.4.24 (2.4.68 lisa birthday). Mine 2.5.58 tomorrow
104 //! The setup_Buttons_mainModule() is really a setup_Sensors so
105 //! take away the #ifdef
106 //calls the setup() for ButtonModule
108
109#ifdef USE_BUTTON_MODULE
110 //register the single click
112#endif
113
114
115 //!12.8.22 add audio
116#ifdef USE_AUDIO_MODULE
118#endif
119
120#ifdef USE_STEPPER_MODULE
121 //reads credentials, etc..
123
124#ifdef FEED_ON_STARTUP
125 //! 6.6.25 this is because the M5Atom on power-on will send current to the feeder
126 //! so when we get here .. we try to break the "feed" by actually feeding for a fraction of a second 0.0
127 // setup_StepperModule();
128 {
129 SerialDebug.println("stopMotor ..");
131 }
132#endif //FEED ON STARTUP
133
134#endif //USE_STEPPER_MODULE
135
136#ifdef USE_UI_MODULE
137 //setup the stepper for the feeder
139#endif
140
141 SerialDebug.println("Starting PetTutor_Server");
142 SerialDebug.printf("M5STACK VERSION = %s\n", M5STACK_VERSION);
143
144 //APril 8, refactoring. Moved up to here..
145 //! 8.16.25 MQTT
146
147 //pass the callbacks, (1) the message callback, and the (blinkLed) implementation
148 //setMessageCallbacks(&feedMessageCallback, &blinkMessageCallback);
149 //register the 2 callbacks for now..
153
154 //3.25.22 -- register the cleanSSID_EPROM
156
157 //NOTE: 6.3.22 .. this is breaking the AP mode, preSetup_WIF_APModule must be called first..
158 //try call from loop():
159 // setup_MQTTNetworking();
160
161 //the tilt 5.2.22 (no one calls this ...)
163
164 //! 8.16.25 WIFI AP
165 //called to validate the doneWIFI() call...
167
168 //init the BLE here.
169 //! 8.16.25 BLE SERVER
170 //yes
171 //!if the BLE_SERVER is turned on..
172 boolean useBLEServer = true;
173#ifdef ESP_M5
175#endif
176 if (useBLEServer)
177 {
178 //Initialize the BLE Server, using the service name PTFeeder
179 //https://stackoverflow.com/questions/20944784/why-is-conversion-from-string-constant-to-char-valid-in-c-but-invalid-in-c
180 //! 8.16.25 MQTT
181
182 //*** The callback for "onWrite" of the bluetooth "onWrite'
184 //*** The callback for "onWrite" of the bluetooth "onWrite'
187
188 //strdup() get away from the
189 setup_BLEServerNetworking(MAIN_BLE_SERVER_SERVICE_NAME, getDeviceNameMQTT(), strdup(PT_SERVICE_UUID), strdup(PT_CHARACTERISTIC_UUID));
190
191 SerialDebug.println("done setupBLEServerNetworking");
192 }
193
194 //BLE Client --
195 //! 8.16.25 BLE CLIENT
196 setup_BLEClientNetworking(MAIN_BLE_CLIENT_SERVICE, (char*) PT_SERVICE_UUID, (char*) PT_CHARACTERISTIC_UUID);
197
198 //MAJOR ISSUE: if the port/server are wrong .. can never reset unless we can get a BUTTON..
199 //If BOOTSTRAP (a compile time ifdef in Defines.h is set, then bypass the AP mode, and use the constants in MQTTNetworking.cpp
200#ifdef BOOTSTRAP
201#else
203 {
204 //Turn on light until out of the AP mode..
205 solidLightOnOff(true);
206 //test
208
209 //set flag ..
211
213 }
214#endif //BOOTSTRAP
215
216}
217
218// **************** finishSetup()
219//! called to finsh the setup.. this is because the MQTT and WIFI aren't run if in AP mode..
221{
222 //! 8.16.25 WIFI AP
223 //basically after the AP module finished, the function WIFI_APModule_JsonOutputString() stores the credentials (versus them calling here ..)
225 {
226 //Turn on light until out of the AP mode..
227 solidLightOnOff(false);
228
229 String jsonOutputString = WIFI_APModule_JsonOutputString();
230 // now this could be passed onto the MQTT processor..
231 SerialDebug.print("From WIFI -> JSON = ");
232 SerialDebug.println(jsonOutputString);
233
234 //! 8.16.25 MQTT
235 //moved here 6.3.22
237
238 //! 8.16.25 WIFI AP
239 //pass the json string onto the MQTT code..
240 // 3.26.22
241 SerialDebug.println("calling .. processJSONMessage");
242
243 //NOTE: if done was because credentials .. then don't call this..
244 //basically if MQTT running .. don't call this..
245 processJSONMessageMQTT(&jsonOutputString[0], NULL);
246
247 //NOTE: we are still in the BLE callback .. and then we call setup_MQTTNetworking..
248 //LETS try setting state in MQTT that is waiting for WIFI..
249
250 //perform the setup() in the MQTTNetworking
251 //NOTE: this is called 2 times if processJSONMessage() sets the eprom data .. since the network was already setup
252 //there is a state flag internally that won't setup 2 times (unless MQTT was kicked out via AP mode, or cleaned credentials)
253 //try call from loop():
254 //setup_MQTTNetworking();
255
256 // sets the _chipName (from MQTT Module)
257 char *deviceName = getDeviceNameMQTT();
258
259 //THIS should output the device name, user name, etc...
260 // SerialDebug.print("pFoodCharacteristic->setValue: ");
261 // SerialDebug.println(_fullJSONString);
262 SerialDebug.print("DeviceName: ");
263 SerialDebug.println(deviceName);
264
265 //! 8.16.25 WIFI AP
266 } //doneWIFI_APModule_Credentials()
267
268 //! try a call.. NOTE, if the WIFI isn't working, this won't work either..
269#ifdef USE_REST_MESSAGING
270 setupSecureRESTCall();
271#endif
272
273 //! 1.20.24 setup the camera after the WIFI is working ..
274 //! 8.16.25 do this now with the OO module in main..
275#ifdef USE_CAMERA_MODULE_x
276 setup_CameraModule();
277#endif
278
279}
280
281// ****************************************
282//! main loop() of the Arduino runtime
283// ****************************************
284void loop() {
285 //#define USE_STOP_ALL
286
287 //! try a call..
288#ifdef USE_REST_MESSAGING
289 // sendSecureRESTCall("/help");
290#endif
291
292#ifdef USE_STOP_ALL
294 {
295 delay(100);
296 return;
297 }
298#endif
299
300 //!4.17.22
302
303 //! 8.16.25
304 //! TODO: change these loops to the M5Sensor plugin model..
305#ifdef USE_AUDIO_MODULE
306 //!12.8.22 add audio
308#endif
309
310 //! 8.16.25 do this now with the OO module in main..
311#ifdef USE_CAMERA_MODULE_x
312 loop_CameraModule();
313#endif
314
315 //! 8.16.25 WIFI AP
317 {
318 //since the MQTT and BLE were not started, the first time the WIFI credentials are finished (after submit)
319 // finish the setup (which are the MQTT and WIFI.
320 //NOTE: this needs to get the credentials in JSON format and store ..
322 {
324 SerialDebug.println("doneWIFI_APModuleCredentials, and now calling finishSetup()");
325 finishSetup();
326 }
327 }
328
329 //! called to check if any Async Commands are in dispatch mode
331
332 //! note: displayModule() musts be called before buttonModule (as the "model" is updated (initiated) in the display module
333 //! 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 ??
335
336 //! loop on the buttons
338
339 /** Do your thing here, this just spams notifications to all connected clients */
340#ifdef USE_STEPPER_MODULE
341 loop_StepperModule(); //was FeederStateMachine
342#endif //USE_STEPPER_MODULE
343
344#ifdef USE_UI_MODULE
346#endif
347
348 //BLE Client --
349 //! 8.16.25 BLE CLIENT
351
352
353 //** 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)
354 //! 8.16.25 WIFI AP
355 //if the APmodule credentials are finished .. then continue normal loop
356 // Otherwise just BLE and AP (for now)
358 {
359 /*******************************MQTT*************************************/
360 //! 8.16.25 MQTT
362
363
364 //! 8.16.25 BLE SERVER
365 //!if the BLE_SERVER is turned on..
366 boolean useBLEServer = true;
367#ifdef ESP_M5
369#endif
370 if (useBLEServer)
371 //! old ESP32 didn't have the "server" turned on..
373
374
375 //! 8.16.25 WIFI AP
376 } //!doneWIFI_APModuleCredentials
377
378 //! 8.16.25 WIFI AP
380 {
382 }
383
384 //APril 8, 2022
385 //loop always, we just don't have WIFI working yet...?
386 //NOTE: the loopBLE will call the register for a message (and call processJSONMessage on BLE commands)
387 //NEED: to set the doneWIFI_APModuleCredentials iff a processJSONMessage resulted in credential updates,
388 // not all JSON are credentials. Others are setting options, like OTA. -- and kill the AP module..
389 //UNFORTUNATELY .. the WIFI and MQTT might fail .. and slow things down. NEED a better way to
390 // run the MQTT loop (so others can get some time, like BLE).
391
392#ifdef ESP_32
393 delay(100); //this delays the feed trigger response
394#else
395 delay(50); // 1.26.24 try a smaller loop delay
396#endif
397} //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:220
void loop()
main loop() of the Arduino runtime
Definition: ESP_IOT.ino:284
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:420
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:278
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:512
#define CALLBACKS_BLE_SERVER
Definition: MainModule.cpp:423
void onStatusMessageBLEServerCallback(char *message)
The callback for "status messages" of the bluetooth.
Definition: MainModule.cpp:659
void solidLightOnOff(boolean onOff)
callback for SOLID blinking led
void setup_mainModule()
called from the setup()
Definition: MainModule.cpp:211
boolean stopAllProcesses_mainModule()
if stopped
Definition: MainModule.cpp:157
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:117
void feedMessageCallback(char *message)
Definition: MainModule.cpp:603
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'.
Definition: MainModule.cpp:756
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:421
#define SINGLE_CLICK_BM
Definition: MainModule.h:93
#define PT_CHARACTERISTIC_UUID
Definition: MainModule.h:55
#define PT_SERVICE_UUID
Definition: MainModule.h:54
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()