板子到手,计划通过arduino实现传感器读数,屏幕显示读数,并将读数通过udp推送到指定的服务器
屏幕库用了
传感器库直接使用厂家的
代码如下:
#include <LovyanGFX.hpp> #include <M5_DLight.h> #include <WiFi.h> #include <WiFiUdp.h> // Include the WiFiUdp library // WiFi network name and password const char * networkName = "sun"; const char * networkPswd = "mingdemen10"; // UDP server address and port const char * udpAddress = "192.168.0.2"; const int udpPort = 3333; class LGFX : public lgfx::LGFX_Device { lgfx::Panel_ST7789 _panel_instance; lgfx::Bus_SPI _bus_instance; lgfx::Light_PWM _light_instance; public: LGFX(void) { auto cfg = _bus_instance.config(); cfg.freq_write = 40000000; cfg.freq_read = 16000000; cfg.pin_sclk = 36; cfg.pin_mosi = 35; cfg.pin_miso = 37; cfg.pin_dc = 40; _bus_instance.config(cfg); _panel_instance.setBus(&_bus_instance); auto cfg_panel = _panel_instance.config(); cfg_panel.pin_cs = 42; cfg_panel.pin_rst = 41; cfg_panel.pin_busy = -1; cfg_panel.invert = true; cfg_panel.panel_width = 135; cfg_panel.panel_height = 240; cfg_panel.offset_x = 52; cfg_panel.offset_y = 40; _panel_instance.config(cfg_panel); auto cfg_light = _light_instance.config(); cfg_light.pin_bl = 45; cfg_light.invert = false; cfg_light.freq = 200; cfg_light.pwm_channel = 7; _light_instance.config(cfg_light); _panel_instance.setLight(&_light_instance); setPanel(&_panel_instance); } }; LGFX display; static LGFX_Sprite sprite(&display); M5_DLight sensor; uint16_t lux; WiFiUDP udp; // Create a WiFiUDP instance void setup(void) { display.init(); Serial.begin(115200); display.setRotation(1); display.setTextSize(2); display.setTextDatum(lgfx::MC_DATUM); sprite.createSprite(135, 240); Serial.println("Sensor begin..."); sensor.begin(&Wire, 17, 18); // HAT DLight sensor.setMode(CONTINUOUSLY_H_RESOLUTION_MODE); // Connect to WiFi Serial.print("Connecting to "); Serial.println(networkName); WiFi.begin(networkName, networkPswd); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); udp.begin(udpPort); // Start UDP service } char info[80]; void loop(void) { lux = sensor.getLUX(); sprintf(info, "lux: %d", lux); sprite.fillSprite(TFT_BLACK); sprite.drawString(info, 40, 40); sprite.pushSprite(0, 0); sprintf(info, "IP: %s", WiFi.localIP().toString().c_str()); display.drawString(info, 60, 100); // Send UDP packet char udpInfo[64]; sprintf(udpInfo, "lux: %d", lux); // Format the lux value into a string udp.beginPacket(udpAddress, udpPort); // Start a packet to the UDP server udp.println(udpInfo); // Send the lux value as a UDP packet udp.endPacket(); // Finalize the packet Serial.println(info); delay(100); }
效果展示:
功能实现,顺手建模个外壳,保护一下屏幕:
模型地址(记得助力):https://makerworld.com.cn/zh/models/653143
今天就先到这里。