The python code to support Arduino Manager is based mainly on some callback functions:
- doWork – This function is called periodically, and here goes your code to read sensors, command tractors and so on
- processIncomingMessages – This function is called when data is received from the iOS device
- processOutgoingMessages – Regularly called to send data to the iOS device
- doSync – This function is called immediately after the iOS device is connected and used for to send status information to the
- process Alarm – Calls when an alarm is fired
- deviceConnected ̵
- deviceDisconnected – Calls when the iOS device disconnected from the ESP32 board
Let's see each function.
The doWork function reads the position of the potentiometer (pa35.read ()) which reads the value of the analog to digital converter connected to the analog pin 32. Similarly, the temperature sensor is read and the conversion from voltage to temperature is performed. The temperature is then saved in a log file (iosController.sdLog).
async def doWork (): global pot global temperature global todaySamplingCounter global todayTNumberOfSamples pot = pa35.read () temperature = 100 * getVoltage (pa32) about todaySamplingCounter> = 10: todaySamplingCounter = 0 todayTNumberOfSamples + = 1 whether iosController.sdFileSize (& # 39; TodayT & # 39;) == 0 or todayTNumberOfSamples == 100: iosController.sdPurgeLogData (& # 39; TodayT & # 39;) iosController.sdLogLabels (& # 39; TodayT & # 39 ;, & # 39; V1 & # 39;) todayTNumberOfSamples = 0 iosController.sdLog (& # 39; TodayT & # 39; Temperature) return
ProcessIncomingMessages is called for each received message and has two parameters:
- variable – the name of the widget that sends data
- value – data associated with the widget.
async def processIncomingMessages (variable, value): global servoPos about variable == & # 39; S1 & # 39 ;: p5.value (int (value)) about variable == & # 39; Knob1 & # 39 ;: print (& # 39; Button1% d & # 39;% int (value)) servoPos = map (int (value), 0, 1023, 40, 115); servo.duty (int (servoPos)) about variable == & # 39; Push1 & # 39; and int (value) == 1: # only when the button is pressed wait for iosController.temporaryDigitalWrite (p16, 0, 500) about variable == & # 39; Cmd_01 & # 39 ;: wait for iosController.consoleLog ("Command:") wait for iosController.consoleLogLn (value) if value == & # 39; blink & # 39 ;: wait for iosController.temporaryDigitalWrite (p16, 0, 200) return
When the S1 switch widget S1 sends "1", the p5 pin is set to 1 [p5.value(int(value))] and the LED connected to it lights up. When Knob Widget Knob1 sends a value (in the range 0.1023), this value is converted in the range 40, 115 to control the servo motor [servo.duty(int(servoPos))]
ProcessOutgingMessages is periodically called to send data to the connected iOS device.
async def processOutgingMessages (): wait for iosController.writeMessage (& # 39; Pot & # 39 ;, pot) wait for iosController.writeMessage (& # 39; T & # 39 ;, temperature) wait for iosController.writeMessage (& # 39; Led13 & # 39 ;, p5.value ()) return
The DoSync feature is called only once when the iOS device connects to the ESP32 card and sends information to initialize certain widgets such as Switch Widget and Knob Widget and Display Widget configured as Text Only.
async def doSync (variable): about variable == & # 39; S1 & # 39 ;: wait for iosController.writeMessage (variable, p5.value ()) about variable == & # 39; Knob1 & # 39 ;: wait for iosController.writeMessage (variable, map (servo.duty (), 40, 115, 0, 1023)) about variable == & # 39; Msg & # 39 ;: wait for iosController.writeTxtMessage (variable, "Hi, I'm your ESP32 card running micropython") return