locked
Intel Edison - Sending and Receiving Messages in JSON Format Using C RRS feed

  • Question

  • Hi,

    I have managed to follow four tutorials from Microsoft as per below:
    Receive device-to-cloud messages
    Create a simulated device app
    Receiving messages on the simulated device
    Send a cloud-to-device message from the app back end

    Simulated device is able to send and receive messages from Microsoft Visual Studio 2015. All coding above are written in C# programming with JSON Format. I have no issue with these, since all are working properly.

    However, I faced problem on configuring Intel Edison for sending messages (Which will replace simulated device). I wish to send and receive messages in C programming with JSON format, since all peripheral configuration all are written in C programming. 

    My final aim is to send and receive messages between Intel Edison and Microsoft Visual Studio 2015 software.

    Two Questons:
    1) How to send messages from Intel Edison to the software since the sample code (sprintf_s() and IoTHubMessage_CreateFromByteArray()) sending in string format instead of JSON format? I cant access windSpeed variable since it is string format.
    2) How to receive messages from the software to Intel Edison since the sample code (IoTHubMessage_GetByteArray()) receiving in string format instead of JSON format? I cant access windSpeed variable since it is string format.

    Without access to windSpeed variable, I cant do further processing in my Intel Edison.

    C Programming for Intel Edison Sending Messages (Based on sample codes from iothub_client_sample_amqp.c:
     
    sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"Edison01\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2));                     
    
    if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)                  {                         
    (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");  
    }

    C# Programming for Simulated Device Sending Messages: 
    private static async void SendDeviceToCloudMessagesAsync()         
    {             
    Random rand = new Random();              
    
    while (true)             
    {                 
    int variable1 = rand.Next(1, 50);                 
    int variable2 = rand.Next(1, 50);                 
    int variable3 = rand.Next(1, 50);                  
    
    var telemetryDataPoint = new                 
    {                     
    deviceId = "Lenovo01",                     
    nt1 = variable1,                     
    et1 = variable2,                     
    st1 = variable3,                 
    };                  
    
    var messageString = JsonConvert.SerializeObject(telemetryDataPoint);                 
    var message = new Message(Encoding.ASCII.GetBytes(messageString));                  
    await deviceClient.SendEventAsync(message);                 
    Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);              }         
    }

    C Programming for Intel Edison Receiving Messages (Based on sample codes from iothub_client_sample_amqp.c:
    outcome = IoTHubMessage_GetByteArray(messages[i].messageHandle, &buffer, &size);
    (void)printf("Received ByteArray Data: %d\r\n", buffer[i]);

    C# Programming for Simulated Device Receiving Messages: 
    private async Task ReceiveMessagesFromDeviceAsync(string partition)        
    {            
    var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);       
        
    while (true)            
    {                
    EventData eventData = await eventHubReceiver.ReceiveAsync();
                    
    if (eventData == null) continue;                
    
    string data = Encoding.UTF8.GetString(eventData.GetBytes());                
    dynamic test = JsonConvert.DeserializeObject(data);                
    Junction Junction1;            // Declare structures                
    // Junction 1 Variables Initialization                
    Junction1.north_traffic = test.nt1;                
    Junction1.east_traffic = test.et1;                
    Junction1.south_traffic = test.st1;                
    Main();            }        
    }

    Hope someone could solve my doubts.

    Thanks!


    • Edited by cnvery Sunday, July 3, 2016 3:24 PM Edit format
    Sunday, July 3, 2016 3:19 PM

All replies