The Twenty Percent

Goal

Goal

Program an Arduino WiFi r2 to post sensor data to a discord bot.

Repos

Uploading Code to the Board

The board is outputting a weird warning when I upload a sketch (blink.ino):

avrdude: WARNING: invalid value for unused bits in fuse “fuse5”, should be set to 1 according to datasheet This behaviour is deprecated and will result in an error in future version You probably want to use 0xcd instead of 0xc9 (double check with your datasheet first).

but according to these posts it is normal and expected.

Using the Serial Port(s)

I had some issues initially communicating on the USB serial port interface. The board uses Serial for USB, and Serial1 for the RX/TX pins on the board. There’s a Serial2 (connected to something called u-blox NINA-W13 module) as well, but I don’t know what the use case is for that.

Baud Rate

The SerialMonitor doesn’t work properly until I explicitly set the baud rate (bits per second). I wonder if I can set that as default somewhere. Update: Seems like it’s hard coded into this file:

vscode\extensions\vsciot-vscode.vscode-arduino-0.4.0\out\src\serialmonitor\serialMonitor.js

After some testing, it seems there’s a bug, which causes the Serial Monitor to mangle output from the Arduino until I use the changeBaudRate() function. After that, the serial monitor works as desired.

Programming the Board

I tried using the included examples in the Arduino extension, but they use the WiFi.h library, did not seem to fit my model. Instead, I’m going to use WiFiNINA.h. It was very easy adding the library through the Arduino Library Manager.

Updating Firmware

I updated the WiFiNINA Firmware using this guide.

Even after updating to 1.3.0, I’m getting reminders that there’s a newer version (1.4.1). This forum post states that this will become available with the next Arduino IDE version.

It looks like there’s an option to add certificates to the board as well (though I did not do so at this time).

Bot

Previous post

Back in the discord bot side of things, I’m continuing to follow the guide, where I left off in this post. I’m tackling commands, and JS (it’s a blast!).

Found out there’s a difference between the in and of keyworlds in javascript:

const collection = ['a', 'b', 'c']

for (const item in collection) {
    // item is an enumerable (0, 1, 2)
}

for (const item of collection) {
    // item is an object (a, b, c)
}

It turns out it’s not necessary to join a channel with the bot. I just had to add the bot through my regular Discord client.

Events

Finished the event handler guide. Now, adding events is easy as pie!

HttpClient

Using needle for http requests, so far so good.

Arduino JSON server

Arduino can now respond with simple text, but next up is sending actual JSON, then to hook up the sensors

Commands

Added some placeholder commands, that I can implement later.

  • !graph <ph|ec|hum|all> default:all
  • !read <ph|ec|hum|all> default:all
  • !configure <event_name> <params>

Todo

  • Bot
    • Upgrage to slash commands
    • Read from database.
    • Add interesting commands
      • !graph <ph|ec|hum|all> default:all (can I use grafana for this?)
      • !read <ph|ec|hum|all> default:all
      • !configure <event_name> <params>
    • Add interesting events
      • alert (on sensor levels)
  • Arduino