Quantcast
Channel: MultiTech Developer Resources » All Posts
Viewing all articles
Browse latest Browse all 7111

MDOT I2C communication with Arduino

$
0
0

I’m struggling with this task, I have an MDOT with a Development Board. I’ve successfully managed to flash the firmware to an mDot and receive on the gateway some dummy sensor data (just based on this example: https://github.com/ARMmbed/mbed-os-example-lorawan).

What I want now is to replace the DummySensor.h C header with my own I2CArduino.h which will read from i2c bus data from the Arduino. The problem I’m facing is that there’s little to no documentation for doing something like this for the ARMBed platform.

I leave my github project link: https://github.com/cipiasentini/lora_mdot

And my Arduino sketch:



#include <DHT.h>
#include <Wire.h>

// Definimos el pin digital donde se conecta el sensor
#define DHTPIN 10
// Dependiendo del tipo de sensor
#define DHTTYPE DHT11

// Inicializamos el sensor DHT11
DHT dht(DHTPIN, DHTTYPE);

// Para pasar float (son 4 bytes) a byte
const byte mDotAddress = 2;
const byte dataCount = 1;
typedef union {
 float floatData;
 byte  rawData [dataCount * sizeof (float)];
} myData;
unsigned long lastSerialPrint = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin(); // join i2c bus (address optional for master)
  // Comenzamos el sensor DHT
  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(5000);
  // Leemos la humedad relativa
  myData h;
  h.floatData = dht.readHumidity();
  // Leemos la temperatura en grados centígrados (por defecto)
  myData t;
  t.floatData = dht.readTemperature();

// Comprobamos si ha habido algún error en la lectura
  if (isnan(h.floatData) || isnan(t.floatData)) {
    Serial.println("Error obteniendo los datos del sensor DHT11");
    return;
  }

  Serial.print("Humedad: ");
  Serial.print(h.floatData);
  Serial.print(" %\t");
  Serial.print("Temperatura: ");
  Serial.print(t.floatData);
  Serial.println(" *C ");

  Wire.beginTransmission(8); // transmit to device #8
  Wire.write("/Humedad:");        // sends 9 bytes
  for (int i=0; i<4; i++) {
    Wire.write(h.rawData[i]); // Print the hex representation of the float
  }
  Wire.write("/Temperatura:"); // 13 bytes
  for (int i=0; i<4; i++) {
    Wire.write(t.rawData[i]); // Print the hex representation of the float
  }
  Wire.endTransmission();

}


Viewing all articles
Browse latest Browse all 7111

Trending Articles