Making a digital twin from scratch.

Lucía Sánchez 45 Reputation points
2023-04-13T10:39:50.7633333+00:00

Hi! I have been doing the tutorials on digital twins but how can I generate one myself from scratch?

Azure Digital Twins
Azure Digital Twins
An Azure platform that is used to create digital representations of real-world things, places, business processes, and people.
236 questions
0 comments No comments
{count} votes

Accepted answer
  1. LeelaRajeshSayana-MSFT 17,766 Reputation points Moderator
    2023-04-13T15:09:36.8866667+00:00

    Hi @Lucía Sánchez Greetings! Welcome to Microsoft Q&A forum. Thank you for posting your question here. We would be glad to help you on this. To get started with creating you custom Azure Digitial Twins, you would first need create a Digital Twin model which acts as template for your Digital Twin. Azure Digital Twins models are represented in the JSON-LD-based Digital Twin Definition Language (DTDL). An Azure Digital Twin model has different fields associated with it. Please find the below image for more details User's image

    Please refer the documentation on Model Overview which provides details on different sections of the Azure Digital Twin model and DTDL repository which provides several examples on how you can design a model compatible to your needs.

    Once you have the model created, you can follow the steps in the Code a Client App article and use the code sample from Upload the model section to create the model on your Azure Digital Twin instance. After uploading the model, you can generate Digital Twins based off the model using the code sample provided in Create Digital Twins section. Here is the code snippet for your reference.

    // <Model_code>
    Console.WriteLine();
    Console.WriteLine($"Upload a model");
    string dtdl = File.ReadAllText("SampleModel.json");
    var models = new List<string> { dtdl };
    
    // Upload the model to the service
    // <Model_try_catch>
    try
    {
         await client.CreateModelsAsync(models);
         Console.WriteLine("Models uploaded to the instance:");
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"Upload model error: {e.Status}: {e.Message}");
    }
    
    // <Initialize_twins>
    var twinData = new BasicDigitalTwin();
    twinData.Metadata.ModelId = "dtmi:example:SampleModel;1";
    twinData.Contents.Add("data", $"Hello World!");
    
    string prefix = "sampleTwin-";
    for (int i = 0; i < 3; i++)
    {
        try
        {
            twinData.Id = $"{prefix}{i}";
            await client.CreateOrReplaceDigitalTwinAsync<BasicDigitalTwin>(twinData.Id, twinData);
            Console.WriteLine($"Created twin: {twinData.Id}");
        }
        catch(RequestFailedException e)
        {
            Console.WriteLine($"Create twin error: {e.Status}: {e.Message}");
        }
    }
    

    Hope this helps! Please let us know if you have any further questions or need further clarification in the comments below. We would be glad to assist you further. If the response helped, please do click Accept Answer and Yes. Doing so would help other community members with similar issue identify the solution. I highly appreciate your contribution to the community.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.