Storage intération downlink fail Can't do push windows form

Summary

first time asking on forum in my life…

Hi.
I am m trying to do a Windows form application that can retrieve and send data to my lorawan device so I won’t need to get on the ttn site and checking live data and use Schedule downlink to send data… After long research I manage to retrieve the uplink message but now am stuck on sending downlink… i know i need to use the push méthode but i am stuck
I use the "Storage Integration "
every time i try to send my downlink it faild.

    public class TTNClient
    {
        private readonly HttpClient httpClient;
        private readonly string appId;
        private readonly string devId;
        private readonly string accessKey;
        private const string TTN_BASE_URL = "https://eu1.cloud.thethings.network/api/v3";

        public TTNClient(string appId, string devId, string accessKey)
        {
            httpClient = new HttpClient();
            this.appId = appId;
            this.devId = devId;
            this.accessKey = accessKey;
        }

        public async Task<bool> SendDownlink(string payload)
        {
            try
            {
                var downlinkUrl = $"{TTN_BASE_URL}/applications/{appId}/devices/{devId}/down/push";

                var downlinkPayload = new
                {
                    formatters = new
                    {
                        javascript = "function Decoder(bytes) { return { output: bytes.toString() }; }"
                    },
                    downlinks = new[]
                    {
                new
                {
                    f_port = 1,
                    frm_payload = payload
                }
            }
                };

                var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(downlinkPayload), Encoding.UTF8, "application/json");
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessKey);
                var response = await httpClient.PostAsync(downlinkUrl, content);

                if (response.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred while sending downlink message: " + ex.Message);
                return false;
            }
        }
    }
    private async void ALARMEPIQUET_Click(object sender, EventArgs e)
    {

        TTNClient ttnClient = new TTNClient(appId, devId, apikey);

        string payload = tebMessage.Text; // Remplacez par le message que vous souhaitez envoyer

        bool success = await ttnClient.SendDownlink(payload);

        if (success)
        {
            MessageBox.Show("Downlink message sent successfully.");
        }
        else
        {
            MessageBox.Show("Failed to send downlink message.");
        }
    }

Do you get an error message? If so, what does it say? (The response from TTN, not your code)

You appear to be sending some JavaScript to the end point which isn’t in the API at all - this is the code that runs on the Application Server that allows you to send a JSON object that is turned in to your downlink payload.

I would start with just sending the JSON that is in the documentation first.

This has nothing to do with downlinks. But if you are using it to routinely read data from TTN, please ensure you only request the data since your last fetch to minimise your load on the community servers. DS is meant to be a backup if your web hook or MQTT has a problem.

2 Likes
Summary

speak three language masterise none

so i am sending it to a database (end-POINT) instead of my API… so i can’t use the same method i use to get data that i use to retrive it…

how it would look like if try to send “hello” or"68 65 6c 6c 6f" ?

is the push things the right way ? to send to my device

base adress null witch seems normal if i send it to a end point and not to my API

did some simplification and add a response.

i finaly did it!!!
This code send hello word to my lora anttena

private async void ALARMEPIQUET_Click(object sender, EventArgs e)
        {           
            string payload = "SGVsbG8gV29ybGQ="; // Payload base64
            int port = 1; // Port 
            bool confirmed = false; // downlink  confirm

            string downlinkUrl = $"https://eu1.cloud.thethings.network/api/v3/as/applications/XXX-test/devices/eui-XXXXX/down/push";

            var downlinkPayload = new
            {
                downlinks = new[]
                {
                new
                {
                    frm_payload = payload,
                    f_port = port,
                    confirmed = confirmed
                }
            }
            };

            var json = Newtonsoft.Json.JsonConvert.SerializeObject(downlinkPayload);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", apikey);
            var response = await client.PostAsync(downlinkUrl, content);

            if (response.IsSuccessStatusCode)
            {
                string responseData = await response.Content.ReadAsStringAsync();
                MessageBox.Show("Downlink message sent successfully. Response: " + responseData);
            }
            else
            {
                MessageBox.Show("Failed to send downlink message. Status Code: " + response.StatusCode);
            }
        }

Well done.

Hopefully you’ll be aware that sending text is inefficient but OK if it’s just a course lab. And that there is a limit of 10 downlinks a day under the TTN Fair Use Policy.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.