Hello User,
I am trying to make a system that has a interface where you can interact with my Meadow. I had the idea to create an Api call (on the meadow ) whenever you press a button. So for instance, when I click on a button called “Start”, my Meadow recieves a Json object from an Api call.
So I started thinking and I thought “In order for my Meadow to know if someone clicked the button, it needs to be searching for a Api response” So if it get’s a response of a json object, than store the object in a variable. If not, keep on searching. So I created a while loop that keeps on searching till it has success. Something like this:
public MeadowApp()
{
Initialize().Wait();
var service = new Service();
while (true)
{
using (var result = service.GetData())
{
if (result.Result.ClientIp == "123") // I am using client ip for example
{
Console.WriteLine("Json found!");
break;
}
result.Dispose();
Console.WriteLine("Nothing found yet...");
}
}
}
But after a while, maybe 2/3 minuts, I get one of these errors:
Or
Could not allocate 8192 bytes
So the infinite loop idea wasn’t a really good think I guess. The Meadow memory couldn’t keep up even though I dispose the result.
But I had a plan B, using a Maple server. The Maple server is pinging every 2 seconds and that’s something I need. So when I click the “Start” button, the maple server starts and when I get a Json result, the Maple server stops. I started creating this code and again I came up with the exact same error or this error:
You probably have a broken mono install. if you see other errors or faults after this message they are probably related and you need to fix your mono install first.
This is the code I have:
public MeadowApp()
{
Console.WriteLine("Starting initialization...");
Initialize().Wait();
Console.WriteLine("Initialization Completed");
server.Start();
if (server.Running)
{
onboardLed.StartPulse(Color.Green);
}
else
{
onboardLed.StartPulse(Color.Red);
}
}
async Task Initialize()
{
onboardLed = new RgbPwmLed(device: Device,
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue,
3.3f, 3.3f, 3.3f,
Meadow.Peripherals.Leds.IRgbLed.CommonType.CommonAnode);
// initialize the wifi adpater
if (!Device.InitWiFiAdapter().Result)
{
throw new Exception("Error: Kan de WiFi adapter niet vinden.");
}
// connnect to the wifi network.
Console.WriteLine($"Connecting to Wifi...");
var connectionResult = await Device.WiFiAdapter.Connect(Secrets.WIFI_NAME, Secrets.WIFI_PASSWORD);
if (connectionResult.ConnectionStatus != ConnectionStatus.Success)
{
throw new Exception($"Error: Kan niet connecten met het WiFi netwerk: {connectionResult.ConnectionStatus}");
}
Console.WriteLine("Connected!");
// create our maple web server
Console.WriteLine("Starting Maple server...");
onboardLed.StartPulse(Color.Yellow);
try
{
server = new Meadow.Foundation.Web.Maple.Server.MapleServer(
Device.WiFiAdapter.IpAddress,
advertise: true,
processMode: RequestProcessMode.Parallel
);
}
catch (OutOfMemoryException e)
{
onboardLed.StartPulse(Color.Red);
Console.WriteLine("Terminating application unexpectedly...");
Environment.FailFast(String.Format("Out of Memory: {0}",
e.Message));
}
onboardLed.StartPulse(Color.Green);
}
And DataRequestHandler:
public async void Hello()
{
var serverUri = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam";
var body = @"";
using (HttpClient client = new HttpClient())
{
client.Timeout = new TimeSpan(0, 1, 0);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(serverUri),
Content = new StringContent(body, Encoding.UTF8, RestSharp.Serialization.ContentType.Json),
};
Console.WriteLine("Adding headers");
/*client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Token.access_token);*/
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Accept header
Console.WriteLine("Done!");
Console.WriteLine("Waiting for response..."); // The code stops here
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(true);
Console.WriteLine(response.ReasonPhrase + " = ReasonPhrase");
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
obj = JObject.Parse(json);
}
this.Context.Response.ContentType = ContentTypes.Application_Json;
this.Context.Response.StatusCode = 200;
this.Send(obj).Wait();
}
After
Console.WriteLine(“Waiting for response…”); // The code stops here
I don’t get any response anymore…
Sorry for the long post and hopefully someone understands what I am saying, I have a sample project that I can upload to Github. If their is any explaination needed, let me know!