Sending Multiplexed Analog Sensors Data Over UDP
Posted: Sat Mar 07, 2020 11:12 pm
I have a HUZZAH feather and I multiplexed the analog pin for 16 analog sensors via the CD74HC4067 mux breakout. I have it working the way I want it over serial and now I'm trying to get it to send the sensor data to my computer through UDP, and that's where I'm struggling. I haven't found any examples similar to what I'm doing.
I've seen the examples from CNMAT that are called "UDPsendBundle" and UDPsendMessage". The bundle example only sends analog data from a single sensor hooked up to each pin.
Any suggestions?
Code that prints the sensor data over serial:
My failed attempt to make it wireless. It has stuff commented out as I was trying different things:
I've seen the examples from CNMAT that are called "UDPsendBundle" and UDPsendMessage". The bundle example only sends analog data from a single sensor hooked up to each pin.
Any suggestions?
Code that prints the sensor data over serial:
Code: Select all
//https://kevinsaye.wordpress.com/2018/01/07/adding-a-16-channel-multiplexor-to-your-esp8266-using-arduino/
//data prints out with 2 decimal points. Not sure how to change that because I don't need decimals
int MUXPinS0 = 16;
int MUXPinS1 = 0;
int MUXPinS2 = 15;
int MUXPinS3 = 13;
// wire (multiplexor)EN to (ESP)GND, SIG to A0, VCC to 3v3 and GND to GND
void setup()
{
Serial.begin(115200);
pinMode(MUXPinS0, OUTPUT);
pinMode(MUXPinS1, OUTPUT);
pinMode(MUXPinS2, OUTPUT);
pinMode(MUXPinS3, OUTPUT);
}
void loop()
{
while (true) {
for (int i = 0; i < 16; i++)
{
Serial.print(getAnalog(i));
Serial.print(" ");
delay(5); //has to have a delay or else it doesn't work with the feather
}
Serial.println(" ");
}
}
float getAnalog(int MUXyPin) {
//MUXyPin must be 0 to 15 representing the analog pin you want to read
//MUXPinS3 to MUXPinS0 are the Arduino pins connected to the selector pins of this board.
digitalWrite(MUXPinS3, HIGH && (MUXyPin & B00001000));
digitalWrite(MUXPinS2, HIGH && (MUXyPin & B00000100));
digitalWrite(MUXPinS1, HIGH && (MUXyPin & B00000010));
digitalWrite(MUXPinS0, HIGH && (MUXyPin & B00000001));
return (float)analogRead(A0); //changing the float to an int doesn't knock out the decimals
}
My failed attempt to make it wireless. It has stuff commented out as I was trying different things:
Code: Select all
/*---------------------------------------------------------------------------------------------
/*
Make an OSC bundle and send it over UDP
OSCBundles allow OSCMessages to be grouped together
to preserve the order and completeness of related messages.
They also allow for timetags to be carried to represent the presentation time
of the messages.
Open Sound Control (OSC) library for the ESP8266/ESP32
Example for sending messages from the ESP8266/ESP32 to a remote computer
The example is sending "hello, osc." to the address "/test".
This example code is in the public domain.
Multiplexing example over serial: https://kevinsaye.wordpress.com/2018/01/07/adding-a-16-channel-multiplexor-to-your-esp8266-using-arduino/
--------------------------------------------------------------------------------------------- */
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#else
#include <WiFi.h>
#endif
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCBundle.h>
//EthernetUDP Udp;
//the Arduino's IP
IPAddress ip(999, 999, 99, 999); //put actual ip here
char ssid[] = "*********"; // your network SSID (name)
char pass[] = "********"; // your network password
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
const IPAddress outIp(999,999,99,999); // remote IP of your computer
const unsigned int outPort = 9999; // remote port to receive OSC
const unsigned int localPort = 8888; // local port to listen for OSC packets (actually not used for sending)
int pinReading = 0;
// wire (multiplexor)EN to (ESP)GND, SIG to A0, VCC to 3v3 and GND to GND
int MUXPinS0 = 16;
int MUXPinS1 = 0;
int MUXPinS2 = 15;
int MUXPinS3 = 13;
void setup() {
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
}
void mux_setup() {
pinMode(MUXPinS0, OUTPUT);
pinMode(MUXPinS1, OUTPUT);
pinMode(MUXPinS2, OUTPUT);
pinMode(MUXPinS3, OUTPUT);
}
void loop() {
//declare the bundle
OSCBundle bndl;
for (int i = 0; i < 16; i++)
{
Serial.print(getAnalog(i));
bndl.add(getAnalog(i)); //keep getting an error here
Serial.print(" ");
delay(10);
}
Serial.println(" ");
Udp.beginPacket(outIp, outPort);
bndl.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
bndl.empty(); // empty the bundle to free room for a new one
// OSCMessage bndl;
// bndl.add(getAnalog(i)); //keep getting an error here
// Serial.print(" ");
// Udp.beginPacket(outIp, outPort);
// bndl.send(Udp);
// Udp.endPacket();
// bndl.empty();
delay(1000);
}
float getAnalog(int MUXyPin) {
//MUXyPin must be 0 to 15 representing the analog pin you want to read
//MUXPinS3 to MUXPinS0 are the Arduino pins connected to the selector pins of this board.
digitalWrite(MUXPinS3, HIGH && (MUXyPin & B00001000));
digitalWrite(MUXPinS2, HIGH && (MUXyPin & B00000100));
digitalWrite(MUXPinS1, HIGH && (MUXyPin & B00000010));
digitalWrite(MUXPinS0, HIGH && (MUXyPin & B00000001));
return (float)analogRead(A0);
}