Skip to content

Instantly share code, notes, and snippets.

@RFullum
Last active February 2, 2023 17:33
Show Gist options
  • Save RFullum/b47bb54a7f3cfb170573cea4a893f727 to your computer and use it in GitHub Desktop.
Save RFullum/b47bb54a7f3cfb170573cea4a893f727 to your computer and use it in GitHub Desktop.
Arduino Nano 33 IoT sensor values over UDP
/**
* Sketch for Arduino Nano 33 IoT
*
* Connects to WiFi network and uses UDP to send
* onboard sensor data.
*
* Sensors: 3D Gyro and 3D accelerometer (6 values)
* Sensor values are Floats cast to a byte array.
* They must be recast to Float on the remote device
* receiving the UDP data.
*
* This is to connect with a JUCE application running on my laptop:
* https://github.com/RFullum/MastersProject1
*/
#include <WiFiNINA.h> // Wifi library
#include <WiFiUdp.h> // UDP library
#include <Arduino_LSM6DS3.h> // IMU accelerometer library
// WiFi variables
char ssid[] = " "; // ENTER Wifi SSID (network name) BETWEEN QUOTES
char pass[] = " "; // ENTER Wifi password BETWEEN QUOTES
int status = WL_IDLE_STATUS; // Status of WiFi connection
WiFiSSLClient client; // Instantiate the Wifi client
// UDP Variables
unsigned int localPort = 2390; // local port to listen on
const char* computerIP = " "; // ENTER YOUR COMPUTER'S IP BETWEEN QUOTES
const unsigned int gyroXPort = 65013; // Destination Ports
const unsigned int gyroYPort = 65014;
const unsigned int gyroZPort = 65015;
const unsigned int accelXPort = 65016;
const unsigned int accelYPort = 65017;
const unsigned int accelZPort = 65018;
WiFiUDP Udp; // Instantiate UDP class
// Byte Arrays to send to JUCE
byte gyroXBuff[4];
byte gyroYBuff[4];
byte gyroZBuff[4];
byte accelXBuff[4];
byte accelYBuff[4];
byte accelZBuff[4];
//
// ***********************************************************
//
void setup()
{
// Check for Wifi Module. If no module, don't continue
if (WiFi.status() == WL_NO_MODULE)
{
while (true);
}
// Connect to Wifi Access Point
connectToAP();
// UDP Connect with report via serial
Udp.begin(localPort);
// Check IMU (accellerometer and gyro unit) is active
if(!IMU.begin())
{
while(1);
}
}
void loop()
{
// Sensor variables
float accelX, accelY, accelZ, gyroX, gyroY, gyroZ;
// Read Sensors
if (IMU.accelerationAvailable())
{
// Get values from sensor
IMU.readAcceleration(accelX, accelY, accelZ);
// Convert values to byte arrays
floatToBuff(accelXBuff, accelX);
floatToBuff(accelYBuff, accelY);
floatToBuff(accelZBuff, accelZ);
}
if (IMU.gyroscopeAvailable())
{
// Get values from sensor
IMU.readGyroscope(gyroX, gyroY, gyroZ);
// Convert values to byte arrays
floatToBuff(gyroXBuff, gyroX);
floatToBuff(gyroYBuff, gyroY);
floatToBuff(gyroZBuff, gyroZ);
}
// Send data over UDP
Udp.beginPacket(computerIP, gyroXPort);
Udp.write(gyroXBuff, 4);
Udp.endPacket();
Udp.beginPacket(computerIP, gyroYPort);
Udp.write(gyroYBuff, 4);
Udp.endPacket();
Udp.beginPacket(computerIP, gyroZPort);
Udp.write(gyroZBuff, 4);
Udp.endPacket();
Udp.beginPacket(computerIP, accelXPort);
Udp.write(accelXBuff, 4);
Udp.endPacket();
Udp.beginPacket(computerIP, accelYPort);
Udp.write(accelYBuff, 4);
Udp.endPacket();
Udp.beginPacket(computerIP, accelZPort);
Udp.write(accelZBuff, 4);
Udp.endPacket();
}
//
// Functions ************************************************
//
void floatToBuff(byte udpBuffer[4], float sensorVal)
{
byte *sensorValByte = reinterpret_cast<byte*>(&sensorVal);
memcpy(udpBuffer, sensorValByte, sizeof(sensorValByte));
}
// Connect to wifi network
void connectToAP()
{
// Try to connect to Wifi network
while ( status != WL_CONNECTED)
{
status = WiFi.begin(ssid, pass);
// wait 1 second for connection:
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment