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.");
}
}