ESP32/ ESP8266 Board – Sample Code of SDK
# | Date | Name | Download |
---|---|---|---|
1 | 02/09/2020 | ESP32/ESP8266 SDK 2.2.0 | Download |
2 | 02/09/2020 | ESP32/ESP8266 Gateway SDK 2.2.0 | Download |
How to run a C sample code ESP32/ ESP8266 Board ?
Prerequisite tools:
- Arduino IDE 1.8.5 or higher (Download)
- ESP board installed
- Open Arduino IDE and click on File > Preferences. In “Additional Boards Manager URLs”
add below line and click on “OK”: - ESP32 device package link “https://dl.espressif.com/dl/package_esp32_index.json”
- ESP8266 device package link
“http://arduino.esp8266.com/stable/package_esp8266com_index.json”
- Open Arduino IDE and click on File > Preferences. In “Additional Boards Manager URLs”
- Now go to Tools > Board > Boards Manager
- Enter “ESP32” and click on Install button “esp32 by Espressif System version 1.0.4 ” or
higher version - Enter “ESP8266” and click on install button “esp8266 by ESP8266 community version
2.4.2” or higher version
- Enter “ESP32” and click on Install button “esp32 by Espressif System version 1.0.4 ” or
- Selection of board
- ESP32 : Tools > Board > DOIT ESP32 DEVKITV1 (or else which you are using )
- ESP8266 : Tools > Board > Nodemcu 1.0 (or else which you are using )
- Libraries installation : You can open Sketch > include library > manage libraries.. in Arduino IDE. When you
install ESP board in IDE mortally all required libraries will install as per device. You need to install
ArduinoJson version(6.13.0)
Installation:
- Download and Extract the “iotconnect-C-sdk-ESP-2.2.0.zip” file
- Using Arduino IDE goto respective ESP board folder.
- iotconnect-C-sdk-ESP-2.2.0/ESP_SDK/ESP32SDK/ESP32
- iotconnect-C-sdk-ESP-2.2.0/ESP_SDK/ESP8266SDK/ESP8266
- Configuration guide documentation for Arduino and ESP boards. On above path you can find the reference document
which is as below:- ESP32-With-the-Arduino-IDE.doc
- ESP8266-With-the-Arduino-IDE.doc
- You can take the firmware file from step #2 file path
- ESP32 : ESP32.ino
- ESP866 : ESP8266.ino
- Add prerequisite input data in above file as explained in the usage section as below. Also enter proper
attribute/sensor name which has associated to the device. - Ready to go with following step.
- Using Arduino IDE click on “Upload” button or else press “ctrl + u”
Usage:
Prerequisite input data
String ENV = "IoTConnectENV"; // IoTConnect platform Environment String cpId = "yourCpid"; // your CPID name can grab it from IoTConnect portal String uniqueId = "yourUniqueID"; // your UniqueID name can grab it from IoTConnect portal char *ssid = "yourNetwork"; // your network SSID (name) char *pass = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
To get the device information and connect to the device
client.InitializationSDK(cpId, uniqueId, CallBack, TwinCallBack, ENV);
To receive the command from Cloud to Device(C2D)
void CallBack(String &topic, String &payload) { String cmd_ackID=""; String Cmd_value=""; int Status = 0,mt=0; Serial.println("Cmd_msg >> " + payload); StaticJsonDocument<512> in_cmd; deserializeJson(in_cmd, payload); cmd_ackID = in_cmd["ackId"].as(); Cmd_value = in_cmd["cmdType"].as(); if(Cmd_value == "0x01" ){Status = 6; mt = 5;} else {Status = 7; mt = 11;} StaticJsonDocument<400> Ack_Json; Ack_Json["ackId"] = cmd_ackID; Ack_Json["st"] = Status; Ack_Json["msg"] = ""; Ack_Json["childId"] = ""; String Ack_Json_Data; serializeJson(Ack_Json, Ack_Json_Data); // Sending ACk of command with Json(String), msg Type(int) and Current Time(String) client.SendAck(Ack_Json_Data, GetTime(), mt); in_cmd.clear(); Ack_Json.clear(); }
Data input format
DynamicJsonDocument Attribute_json(2048); JsonArray Device_data = Attribute_json.to(); JsonObject Device_01 = Device_data.createNestedObject(); Device_01["uniqueId"] = uniqueId; Device_01["time"] = GetTime(); JsonObject D01_Sensors=Device_01.createNestedObject("data"); D01_Sensors["Humidity"] = random(20, 80); D01_Sensors["Temperature"] = random(12, 32); JsonObject D01_SensorsV = D01_Sensors.createNestedObject("vibration"); D01_SensorsV["x"] = random(12, 50); D01_SensorsV["y"] = random(-20, 30); D01_SensorsV["z"] = random(10, 35); String Attribute_json_Data; serializeJson(Attribute_json, Attribute_json_Data);
To send the data from Device To Cloud(D2C)
client.SendData(Attribute_json_Data);
To receive the twin from Cloud to Device(C2D)
void TwinCallBack(String &topic, String &payload) { Serial.println("Twin_msg >> " +topic + " " + payload); //TO update the twinproperty client.UpdateTwin(key,value); }
Get all the Twin property from the cloud to your device.
client.getAllTwins();
Disconnects IoTConnect SDK using Dispose Method.
client.dispose();