Skip to content

Instantly share code, notes, and snippets.

@roboter
Created June 19, 2022 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roboter/6613dc33e6e37c10d89d4a671d758fd4 to your computer and use it in GitHub Desktop.
Save roboter/6613dc33e6e37c10d89d4a671d758fd4 to your computer and use it in GitHub Desktop.
#define HAS_WIFI
using Iot.Device.Ssd13xx;
using nanoFramework.Hardware.Esp32;
using nanoFramework.Networking;
using System;
using System.Device.I2c;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
namespace NFApp1
{
public class Program
{
private static Ssd1306 device;
private static HttpClient _httpClient;
public static void Main()
{
Debug.WriteLine("Waiting for network up, IP address and valid date & time...");
bool success;
CancellationTokenSource cs = new(60000);
// if the device doesn't have the Wifi credentials stored
success = WifiNetworkHelper.ConnectDhcp(Settings.WifiNetworkName, Settings.WifiNetworkPasswork, requiresDateTime: true, token: cs.Token);
if (!success)
{
Debug.WriteLine($"Can't get a proper IP address and DateTime, error: {NetworkHelper.Status}.");
if (NetworkHelper.HelperException != null)
{
Debug.WriteLine($"Exception: {NetworkHelper.HelperException}");
}
return;
}
while (true)
{
_httpClient = new HttpClient
{
BaseAddress = new Uri(Settings.AzurePortal)
};
Debug.WriteLine($"Getting data from: {_httpClient.BaseAddress.AbsoluteUri}");
var response = _httpClient.Get(Settings.AzureFunction);
Debug.WriteLine(response.Content.ReadAsString());
//LCD ========================
//configure the I2C GPIOs
Configuration.SetPinFunction(Settings.DisplayDataPpin, DeviceFunction.I2C1_DATA);
Configuration.SetPinFunction(Settings.DisplayClockPin, DeviceFunction.I2C1_CLOCK);
//Init LCD
device = new Ssd1306(I2cDevice.Create(new I2cConnectionSettings(1, Ssd1306.DefaultI2cAddress)), Ssd13xx.DisplayResolution.OLED128x64);
device.ClearScreen();
device.Font = new BasicFont();
device.DrawString(0, 0, "channel:", 1, true);//centered text
device.DrawString(0, 14, Settings.ChannelName, 2);//large size 2 font
device.DrawString(0, 49, response.Content.ReadAsString(), 2, true);//centered text
device.Display();
_httpClient.Dispose();
Thread.Sleep(1000 * 60 * 60);
}
}
}
public static class Settings
{
public const string ChannelName = "lunokhod1";
public const string ApiKey = "";
public const int DisplayDataPpin = 5;
public const int DisplayClockPin = 4;
public const string WifiNetworkName = "";
public const string WifiNetworkPasswork = "";
public const string ChannelId = "";
public const string YouTubeApiKey = "";
public const string AzureFunctionKey = "";
public static string Url
=> $"https://www.googleapis.com/youtube/v3/channels?part=statistics&id={ChannelId}&fields=items/statistics/subscriberCount&key={YouTubeApiKey}";
public const string AzurePortal = "http://getyoutubecounter20220619202622.azurewebsites.net";
public static string AzureFunction => $"/api/Counter?code={AzureFunctionKey}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment