In the last tutorial we saw how to use the Arduino ESP32 AWS IOT library to configure an AWS client to subscribe/publish to a topic/thing. At the end of the tutorial we logged the temperature and humidity using DTH11 sensor.
Contents
[hide]Hornbill IO operation
In this tutorial we will see how to control the ESP32 pins using the AWS IOT. For this we will be using the Hornbill_IO library. Hornbill_IO currently supports the below four operation which will be extended in the near future.
- digitalRead
- digitalWrite
- analogRead
- analogWrite
Hornbill IO JSON Format
We have a specific protocol/json format defined for the IO operation. Below is the Json format for sending the data from Control Unit to ESP32
{ | |
"desired":{ | |
"operation":"digitalWrite", | |
"pin":13, | |
"value":1 | |
} | |
} |
Below is the Json format for reporting the data from ESP32 to Server.
{ | |
"reported":{ | |
"operation":"analogRead", | |
"pin":36, | |
"value":1023.00 | |
} | |
} |
Hornbill IO Structure
The above Json format is mapped to internal Hornbill IO control structure as below. This may change in the future as we add more functionality to Hornbill IO library.
typedef struct { | |
float value; // digital/analog value of the pin | |
int pin; // Pin Num to read/write the data | |
char operation[20]; // IO opeartion (digitalRead/digitalWrite/analogRead) | |
}hornbill_IO_type_t; |
Code
#include <AWS_IOT.h> | |
#include <WiFi.h> | |
#include <Hornbill_IO.h> | |
AWS_IOT AWS_CLIENT; | |
char WIFI_SSID[]="your Wifi SSID"; | |
char WIFI_PASSWORD[]="Wifi Password"; | |
char HOST_ADDRESS[]="AWS host address"; | |
char CLIENT_ID[]= "client id"; | |
char TOPIC_NAME[]= "your thing/topic name"; | |
int status = WL_IDLE_STATUS; | |
int tick=0,msgReceived=0, publishMsg=0; | |
char payload[512]; | |
char rcvdPayload[512]; | |
hornbill_IO_type_t ioReq; | |
int returnCode; | |
void mySubCallBackHandler (char *topicName, int payloadLen, char *payLoad) | |
{ | |
strncpy(rcvdPayload,payLoad,payloadLen); | |
rcvdPayload[payloadLen] = 0; | |
msgReceived = 1; | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(2000); | |
while (status != WL_CONNECTED) | |
{ | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(WIFI_SSID); | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
// wait 5 seconds for connection: | |
delay(5000); | |
} | |
Serial.println("Connected to wifi"); | |
if(AWS_CLIENT.connect(HOST_ADDRESS,CLIENT_ID)== 0) | |
{ | |
Serial.println("Connected to AWS"); | |
delay(1000); | |
if(0==AWS_CLIENT.subscribe(TOPIC_NAME,mySubCallBackHandler)) | |
{ | |
Serial.println("Subscribe Successfull"); | |
} | |
else | |
{ | |
Serial.println("Subscribe Failed, Check the Thing Name and Certificates"); | |
while(1); | |
} | |
} | |
else | |
{ | |
Serial.println("AWS connection failed, Check the HOST Address"); | |
while(1); | |
} | |
delay(2000); | |
} | |
void loop() { | |
if(msgReceived == 1) | |
{ | |
msgReceived = 0; // Process the newly received message | |
returnCode = Hornbill_IO.parseRequest(rcvdPayload,&ioReq); | |
switch(returnCode) | |
{ | |
case HORNBILL_IO_REQUEST_VALID: // Valid Json frame, process the request | |
Serial.println("Processing Request"); | |
if(Hornbill_IO.processRequest(&ioReq) == 0) | |
{ | |
Hornbill_IO.createPayload(payload,&ioReq); //Create the response payload | |
publishMsg = 1; // Action taken, now send the response back | |
} | |
break; | |
case HORNBILL_IO_REQUEST_JSON_INVALID: | |
Serial.println("Wrong JSON Format"); | |
break; | |
default: | |
// Do nothing | |
break; | |
} | |
} | |
if(publishMsg == 1) | |
{ | |
Serial.println("Sending Response"); | |
if(AWS_CLIENT.publish(TOPIC_NAME,payload) == 0) | |
{ | |
Serial.print("Publish Message:"); | |
Serial.println(payload); | |
publishMsg = 0; // Publish successfull, clear the flag | |
} | |
else | |
{ | |
Serial.println("Publish failed, Will try again after 1sec"); | |
} | |
} | |
vTaskDelay(1000 / portTICK_RATE_MS); | |
} |