Hi all!
New to Arduino!
I am using Teensyduino IDE 1.56on MAC OSX Monterey.
I have a Node MCU V0.9 ESP8266 which connects as a client to my phone's hotspot. I use it to read the RSSI value (signal strength) of the connection and map this value to a new range (0-255).
I want to send that value every 200 millis to my Teensy board (Teensy 3.2), read it and use it to modulate the frequency of waveform 1.
What i tried: I set up an I2C communication where the NODE MCU is a Master - Sender and the Teensy is the Slave - Receiver.
That works, however, I get no sound coming out while the I2C is running. Seems like the I2C blocks my void loop() and is not the appropriate method to achieve this. I am therefore looking for another way to send this value.
What could work? I read that PWM is too eratic (https://arduino.stackexchange.com/qu...via-analog-pin)
Could this be done using software Serial?
What is the most reliable way to do this?
My code below:
1 - On the Node MCU side (I2C master + Sender for now)
//CODE FOR THE NODE MCU = NODE MCU is a Master - Sender
#include <ESP8266WiFi.h>
#include <Wire.h>
const byte SlaveDeviceId = 1;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(SIG, OUTPUT);
pinMode(ON, OUTPUT);
digitalWrite(ON, HIGH);
WiFi.mode(WIFI_STA);
WiFi.begin("SSID", "pw"); // Try to connect to a given WiFi network
Wire.begin(D2,D1);
}
void loop() {
printWiFiState();
int map_sig = dbmtoper();
int input = map_sig;
// Send two bytes to slave.
Wire.beginTransmission(SlaveDeviceId);
Wire.write(input >> 8);
Wire.write(input & 255);
Wire.endTransmission();
delay(200);
}
void printWiFiState() {
static boolean printed = false;
if (WiFi.status() == WL_CONNECTED) {
if (printed)
return;
printed = true;
} else { // if WiFi is not connected
if (!printed)
return;
printed = false;
}
}
int dbmtoper() {
int best_rssi = -20;
int worst_rssi = -70;
if (WiFi.status() != WL_CONNECTED)
return 0;
int dBm = WiFi.RSSI();
// Serial.println(dBm);
if (dBm <= worst_rssi)
return 0;
if (dBm >= best_rssi)
return 255;
return map(dBm, worst_rssi, best_rssi, 0, 255);
}
And on the Teensy (receiver - slave for now)
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=186,238
AudioOutputAnalog dac1; //xy=523,243
AudioConnection patchCord1(waveform1, dac1);
const byte SlaveDeviceId = 1;
int receivedValue;
void setup() {
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(12);
waveform1.begin(0.1, 440, WAVEFORM_SINE);
// Start I²C bus as a slave
Wire.setSCL(19);
Wire.setSDA(18);
Wire.begin(SlaveDeviceId);
Serial.begin(115200);
Serial.println("uploaded");
}
void loop() {
int freq = map(receivedValue, 0, 255, 100, 500);
waveform1.frequency (freq);
delay(200);
Wire.onReceive(receiveCallback);
}
// aCount is the number of bytes received.
void receiveCallback(int aCount)
{
if(aCount == 2)
{
receivedValue = Wire.read() << 8;
receivedValue |= Wire.read();
Serial.println(receivedValue);
}
else
{
Serial.print("Unexpected number of bytes received: ");
Serial.println(aCount);
}
}
Thank you!