I'm trying to implement a sketch on an MKR Wifi 1010 board but I'm not succeeding. I used the same code on an MKR 1000 board and had no problems. I have done several tests, updated the firmware of the board but still no success. When I access the serial monitor it doesn't write anything. Can anyone give me a hand?
The code that I am trying to implement is the following:
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h> // library para UDP por wifi
#include <OSCMessage.h>
#include<Wire.h> // Biblioteca para o sensor AG
////////////////////////////////////////////////////////////////////////////SENSOR DIREÇÂO VENTO
#define echoPin 2 // Echo Pin
#define trigPin 3 // Trigger Pin
int maximumRange = 8; //Maximum range needed
int minimumRange = 0; //Mimimum range needed
long duration, distance; //duration to calculate distance
//////////////////////////////////////////////////////////////////////////// SENSOR AG
const int MPU = 0x68; // Endereço I2C do MPU6050.
int AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
//////////////////////////////////////////////////////////////////////////// SENSOR WIN SPEED
int pino_D0 = 0;
int rpm;
volatile byte pulsos;
unsigned long timeold;
//Altere o numero abaixo de acordo com o seu disco encoder
unsigned int pulsos_por_volta = 16;
/////////////////////////////////////////////////////////////////////////// CONFIG WIFI
char ssid[] = "xxxxxxxxxx"; // your network SSID (name)
char pass[] = "xxxxxxxxxx"; // your network password
WiFiUDP Udp;// A UDP instance to let us send and receive packets over UDP
/////////////////////////////////////////////////////////////////////////// CONFIG OSC
const IPAddress outIp(192, 168, 1, 100); // 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)
/////////////////////////////////////////////////////////////////////////// SENSOR WIN SPEED
void contador()
{
//Incrementa contador
pulsos++;
}
void setup()
{
/////////////////////////////////////////////////////////////////////////// SENSOR WIN DIRECTION
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
/////////////////////////////////////////////////////////////////////////// SENSOR AG
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
//Inicializa o MPU-6050
Wire.write(0);
Wire.endTransmission(true);
/////////////////////////////////////////////////////////////////////////// OSC
// 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);
///////////////////////////////////////////////////////////////////////// SENSOR WIN SPEED
pinMode(pino_D0, INPUT);
attachInterrupt(0, contador, FALLING);
pulsos = 0;
rpm = 0;
timeold = 0;
}
void loop()
{
////////////////////////////////////////////////////////////////////////// SENSOR WIN DIRECTION
/* The folling trigPin/echoPin cycle is used to determine the
distance of the nearest objects by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on speed of sound
distance = duration / 20.2;
////////////////////////////////////////////////////////////////////////// SENSOR AG
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true);
AcX = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); //0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); //0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); //0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); //0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); //0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); //0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
////////////////////////////////////////////////////////////////////////// SENSOR WIND SPEED
if (millis() - timeold >= 15);
{
detachInterrupt(0);
rpm = (60 * 1000 / pulsos_por_volta ) / (millis() - timeold) * pulsos;
timeold = millis();
pulsos = 0;
attachInterrupt(0, contador, FALLING);
}
////////////////////////////////////////////////////////////////////////// SERIAL MONITOR
Serial.print(" "); Serial.print(AcX);
Serial.print(" "); Serial.print(AcY);
Serial.print(" "); Serial.print(AcZ);
Serial.print(" "); Serial.print(GyX);
Serial.print(" "); Serial.print(GyY);
Serial.print(" "); Serial.print(GyZ);
Serial.print(" ");//acrescenta um espaço em branco
Serial.print(distance);//imprime o valor
Serial.print(" ");
Serial.print(rpm, DEC);
Serial.println();
////////////////////////////////////////////////////////////////////////// OSC
OSCMessage msg("valor");
msg.add((AcX));///////////////////////////////////// SENSOR AcX
msg.add((AcY));///////////////////////////////////// SENSOR AcY
msg.add((AcZ));///////////////////////////////////// SENSOR AcZ
msg.add((GyX));///////////////////////////////////// SENSOR GyX
msg.add((GyY));///////////////////////////////////// SENSOR GyY
msg.add((GyZ));///////////////////////////////////// SENSOR GyZ
msg.add((distance));//////////////////////////////// SENSOR WIND DIRECTION
msg.add((rpm));///////////////////////////////////// SENSOR WIND SPEED
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
delay(150);
}
THANXS !
