Evan Rust
Published © GPL3+

Getting Started with IoTConnect Dashboards for ElephantEdge

See how data can be aggregated and displayed in one place using Avnet's IoTConnect, and then utilize it for the ElephantEdge contest.

BeginnerFull instructions provided1 hour682
Getting Started with IoTConnect Dashboards for ElephantEdge

Things used in this project

Hardware components

SparkFun ESP32 Thing
SparkFun ESP32 Thing
×1

Software apps and online services

Avnet IoTConnect

Story

Read more

Code

IoTConnect_ESP_SDK_2.2.0.ino

C/C++
You also need the files from the downloaded C SDK in the same folder!
/*
  SDK for IoTConnect
  
  This IoTConnect SDK will help you to update your Sensors data on IoTConnect cloud(Azure)
  In this example file Humidity, Temperature and vibration(x,y,z) random data published on our cloud at real time

  For run this example you have to include/import "IoTConnect.cpp" or "IoTConnect.h"
  you will need wifi connection for publishing data to IoTCOnnect ..
  For the secure MQTT connection here we are using "SAS Token key" 
  
  for more help and informationvisit https://help.iotconnect.io SDK section

    modified 02/Sept/2020
*/

// Connection with IoTConnect 
#include "IoTConnect.h"
// for getting real time from NTP server
#include "time.h"
// making IOTConnectClient client for use IOTConnectClient functions  
IOTConnectClient client;      
  
// Prerequisite input data
//"Get your ENV and CPID from the portal key vaults module or visit https://help.iotconnect.io SDK section."

   String ENV     	= "your environment (probably Avnet)";
   String cpId    	= "your company ID";						// your CPID (Company ID)								
   String uniqueId	= "your unique ID";					// your UniqueID (Device name )
   char *ssid        = "SSID here";               // your network SSID (name)
   char *pass        = "PSK here";            // your network password (use for WPA, or use as key for WEP)                                         
   

unsigned long lastMillis = 0;
int count = 0;

String Months(String in){                                     
       if(in == "Jan")  return "01";  else if(in == "Feb")  return "02";
  else if(in == "Mar")  return "03";  else if(in == "Apr")  return "04";
  else if(in == "May")  return "05";  else if(in == "Jun")  return "06";
  else if(in == "Jul")  return "07";  else if(in == "Aug")  return "08";
  else if(in == "Sep")  return "09";  else if(in == "Oct")  return "10";
  else if(in == "Nov")  return "11";  else if(in == "Dec")  return "12";
}

    // Get time From NTP and setup in SDK format
String GetTime(){                                             // Setup UCT time from NTP server 
    String Y,M,D1,D2,Ti;
    time_t now = time(nullptr);       
    String T= (ctime(&now));
    //Serial.println(T);
    if (T.substring(20,22) == "20") {
        D1 = T.substring(8,9);          if (D1 == " ")D1 = "0";
        D2 = T.substring(9,10);
        M = Months(T.substring(4,7));   Y = T.substring(20,24);
        Ti = T.substring(11,19);
        return (Y+"-"+M+"-"+D1+D2+"T"+Ti+".000Z");
    }
      return (Y+"-"+M+"-"+D1+D2+"T"+Ti+".000Z");
}


void setup() {
    delay(500);
    Serial.begin(115200);                                      // setup Serial Monitor 
    WiFi.mode(WIFI_STA);    WiFi.begin(ssid, pass);
    Serial.println("\n\tConnecting WiFi ");
    while (WiFi.status() != WL_CONNECTED) {
        delay(50);
        Serial.print("-");
    }
    Serial.println("\n\t WiFi Connected");
    Serial.print("Device_IP: "); Serial.println(WiFi.localIP());
    configTime(0, 0, "pool.ntp.org");           // getting UCT time from NTP server
    // (timezone*3600,dst,"pool.ntp.org","time.nist.gov");      
	
	if(cpId || uniqueId){
        if(cpId == 0){
            Serial.println("cpId can not be blank");
			return ;
		}
        if(uniqueId == 0){
            Serial.println("uniqueId can not be blank");
			return ;
		}
    }
    client.InitializationSDK(cpId, uniqueId, CallBack, TwinCallBack, ENV);         // initialization SDK for IoTConnect
	
    delay(30);
    client.GetAllTwins();
}

void loop(){
    client.MQTT_loop();           // SDK connection in infinite loop
    delay(100);
    //////////////////////////////////////////////////////////////////////////////////
    DynamicJsonDocument  Attribute_json(2048);                                      //
        JsonArray Device_data = Attribute_json.to<JsonArray>();                     //   Send data input json format 
        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);                             // 
    //////////////////////////////////////////////////////////////////////////////////
    
// publish a message roughly every 10000 millisecond.
    if (millis() - lastMillis > 10000) {
        lastMillis = millis();
        //Sending all data to IoTConnect with SendDataINput
        client.SendData(Attribute_json_Data); 
        count = count + 1;
    } 
    Attribute_json.clear();

    if (count > 10){
      // After this SDK will Stop you need to reboot your device or SDK
      client.Dispose();
      for (;;);
      
      }

}


    // this function will Give you TwinCallBack payload
void TwinCallBack(String &topic, String &payload) {          
    Serial.println("Twin_msg >>  " +topic + " " + payload);       
    //TO update the twinproperty use UpdateTwin function                             
    //client.UpdateTwin(key,value);
}


    // this function will Give you Device CallBack payload
    // You can send command ACK by SendAck(DataBuffer, GetTime(), mt) function 
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<String>();  
    Cmd_value = in_cmd["cmdType"].as<String>();
    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();  
 }

Credits

Evan Rust

Evan Rust

120 projects • 1054 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments