Hello,
First time trying to communicate wirelessly so I'm sure I'm doing something wrong. I need a sensor attached to the ESP32 Thing to communicate wirelessly. I can send random OSC data wirelessly from the ESP32, and I can read the sensor data from ESP32 via USB. However, when I try to smush the two together and read the sensor data via OSC, it puts out the same value over and over again. Any help would be appreciated. Here is my code:
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
const char * networkName = "myopennetwork";
const char * networkPswd = NULL;
//IP address to send UDP data to:
const char * udpAddress = "192.168.1.101";
const int udpPort = 3333;
//Are we currently connected?
boolean connected = false;
int ledPin = 5;
//The udp library class
WiFiUDP udp;
/* Modern Device Wind Sensor Sketch for Rev C Wind Sensor
This sketch is only valid if the wind sensor if powered from
a regulated 5 volt supply. An Arduino or Modern Device BBB, RBBB
powered from an external power supply should work fine. Powering from
USB will also work but will be slightly less accurate in our experience.
When using an Arduino to power the sensor, an external power supply is better. Most Arduinos have a
polyfuse which protects the USB line. This fuse has enough resistance to reduce the voltage
available to around 4.7 to 4.85 volts, depending on the current draw.
The sketch uses the on-chip temperature sensing thermistor to compensate the sensor
for changes in ambient temperature. Because the thermistor is just configured as a
voltage divider, the voltage will change with supply voltage. This is why the
sketch depends upon a regulated five volt supply.
Other calibrations could be developed for different sensor supply voltages, but would require
gathering data for those alternate voltages, or compensating the ratio.
Hardware Setup:
Wind Sensor Signals Arduino
GND GND
+V 5V
RV A1 // modify the definitions below to use other pins
TMP A0 // modify the definitions below to use other pins
Paul Badger 2014
Hardware setup:
Wind Sensor is powered from a regulated five volt source.
RV pin and TMP pin are connected to analog inputs.
*/
#define analogPinForRV 13 // change to pins you the analog pins are using
#define analogPinForTMP 1
// to calibrate your sensor, put a glass over it, but the sensor should not be
// touching the desktop surface however.
// adjust the zeroWindAdjustment until your sensor reads about zero with the glass over it.
const float zeroWindAdjustment = .2; // negative numbers yield smaller wind speeds and vice versa.
int TMP_Therm_ADunits; //temp termistor value from wind sensor
float RV_Wind_ADunits; //RV output from wind sensor
float RV_Wind_Volts;
unsigned long lastMillis;
int TempCtimes100;
float zeroWind_ADunits;
float zeroWind_volts;
float WindSpeed_MPH;
void setup(void) {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);
//set in sta mode
WiFi.mode(WIFI_STA);
delay(100);
//register event handler
WiFi.onEvent(WiFiEvent);
delay(100);
}
void loop(void) {
if (connected) {
digitalWrite(ledPin, !digitalRead(ledPin));
OSCMessage msg("/bno");
msg.add((int) millis());
msg.add((int) value);
//msg.add((float) RV_Wind_Volts);
udp.beginPacket(udpAddress, udpPort);
msg.send(udp);
udp.endPacket();
} else {
for (int i = 0 ; i < 5; i++) {
delay(2000);
digitalWrite(ledPin, !digitalRead(ledPin));
}
if (!connected) {
connectToWiFi(networkName, networkPswd);
}
}
//100 Hz
delay(1000);
tempDetect();
}
void connectToWiFi(const char * ssid, const char * pwd) {
Serial.println("Connecting to WiFi network: " + String(ssid));
// delete old config
WiFi.disconnect(true);
delay(500);
//Initiate connection
WiFi.begin(ssid, pwd);
Serial.println("Waiting for WIFI connection...");
}
//wifi event handler
void WiFiEvent(WiFiEvent_t event) {
switch (event) {
case SYSTEM_EVENT_STA_GOT_IP:
//When connected set
Serial.print("WiFi connected! IP address: ");
Serial.println(WiFi.localIP());
//initializes the UDP state
//This initializes the transfer buffer
udp.begin(WiFi.localIP(), udpPort);
digitalWrite(ledPin, HIGH);
connected = true;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
connected = false;
digitalWrite(ledPin, LOW);
break;
}
}
void tempDetect() {
if (millis() - lastMillis > 200) { // read every 200 ms - printing slows this down further
TMP_Therm_ADunits = analogRead(analogPinForTMP);
RV_Wind_ADunits = analogRead(analogPinForRV);
RV_Wind_Volts = (RV_Wind_ADunits * 0.0048828125);
// these are all derived from regressions from raw data as such they depend on a lot of experimental factors
// such as accuracy of temp sensors, and voltage at the actual wind sensor, (wire losses) which were unaccouted for.
TempCtimes100 = (0.005 * ((float)TMP_Therm_ADunits * (float)TMP_Therm_ADunits)) - (16.862 * (float)TMP_Therm_ADunits) + 9075.4;
zeroWind_ADunits = -0.0006 * ((float)TMP_Therm_ADunits * (float)TMP_Therm_ADunits) + 1.0727 * (float)TMP_Therm_ADunits + 47.172; // 13.0C 553 482.39
zeroWind_volts = (zeroWind_ADunits * 0.0048828125) - zeroWindAdjustment;
// This from a regression from data in the form of
// Vraw = V0 + b * WindSpeed ^ c
// V0 is zero wind at a particular temperature
// The constants b and c were determined by some Excel wrangling with the solver.
WindSpeed_MPH = pow(((RV_Wind_Volts - zeroWind_volts) / .2300) , 2.7265);
Serial.print(" TMP volts ");
Serial.print(TMP_Therm_ADunits * 0.0048828125);
Serial.print(" RV volts ");
Serial.print((float)RV_Wind_Volts);
Serial.print("\t TempC*100 ");
Serial.print(TempCtimes100 );
Serial.print(" ZeroWind volts ");
Serial.print(zeroWind_volts);
Serial.print(" WindSpeed MPH ");
Serial.println((float)WindSpeed_MPH);
lastMillis = millis();
} }