1.2.69. fejezet, Manchester kódolás

Kapcsolódó hivatkozások

typedefs.h

struct package
{
  float temperature ;
  float humidity ;
  uint8_t sensorId;
};
 
typedef struct package Package;
 
typedef union I2CPackage_t {
  Package sensorData;
  uint8_t I2CPackage[sizeof(package)];
};

Adó (TX) kód

#include <Manchester.h>
#include "typedefs.h"
#include <DHT.h>
 
#define TX_PIN  10  //pin where your transmitter is connected
#define LED_PIN 13 //pin for blinking LED
#define DHTTYPE DHT22 
#define DHTPIN 5
 
DHT dht(DHTPIN, DHTTYPE);
 
boolean moo = true; //last led status
#define BUFFER_SIZE 10
 
uint8_t buffer[BUFFER_SIZE];
I2CPackage_t pdata;
 
void setup() 
{
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, moo);
  man.setupTransmit(TX_PIN, MAN_1200);
  Serial.begin(9600);
}
 
void loop() 
{
  readSensor();
 
  buffer[0] = sizeof pdata;
  for(int i=0; i<sizeof pdata; i++) {
    buffer[i+1] = pdata.I2CPackage[i];
  }
  writeDataToSerial(pdata);  
  man.transmitArray(sizeof buffer, buffer);
  moo = !moo;
  digitalWrite(LED_PIN, moo);
  delay(800);
}
 
void readSensor() {
  dht.begin();
  pdata.sensorData.sensorId=1;
  pdata.sensorData.humidity=dht.readHumidity();
  pdata.sensorData.temperature=dht.readTemperature();
}
 
void printArray() {
  for(int i=0; i<sizeof buffer; i++) {
    if (buffer[i] < 16) {Serial.print("0");}
    Serial.print(buffer[i],HEX);
  }
}
void writeDataToSerial(I2CPackage_t data) {
  Serial.print("{\"buffer\":\"");
  printArray();
  Serial.print("\",\"sensorid\":\"");
  Serial.print(data.sensorData.sensorId);
  Serial.print("\",\"temperature\":\"");
  Serial.print(data.sensorData.temperature);
  Serial.print("\",\"humidity\":\"");
  Serial.print(data.sensorData.humidity);
  Serial.println("\"}");
}

Vevő (RX) kód

#include <Manchester.h>
#include "typedefs.h"
#define RX_PIN 2
#define LED_PIN 13
 
boolean moo = true;
#define BUFFER_SIZE 10
uint8_t buffer[BUFFER_SIZE];
I2CPackage_t pdata;
 
void setup() {
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, moo);
  Serial.begin(9600);
  man.setupReceive(RX_PIN, MAN_1200);
  man.beginReceiveArray(BUFFER_SIZE, buffer);
}
 
void loop() {
  if (man.receiveComplete()) {
    uint8_t datalength = buffer[0];
    if (datalength > 0) {
      for (uint8_t i = 0; i < datalength; i++) {
        pdata.I2CPackage[i] = buffer[i + 1];
      };
 
      writeDataToSerial(pdata);
    }
    man.beginReceiveArray(BUFFER_SIZE, buffer);
    moo = !moo;
    digitalWrite(LED_PIN, moo);
  }
}
 
void printArray() {
  for(int i=0; i<sizeof buffer; i++) {
    if (buffer[i] < 16) {Serial.print("0");}
    Serial.print(buffer[i],HEX);
  }
}
void writeDataToSerial(I2CPackage_t data) {
  Serial.print("{\"buffer\":\"");
  printArray();
  Serial.print("\",\"sensorid\":\"");
  Serial.print(data.sensorData.sensorId);
  Serial.print("\",\"temperature\":\"");
  Serial.print(data.sensorData.temperature);
  Serial.print("\",\"humidity\":\"");
  Serial.print(data.sensorData.humidity);
  Serial.println("\"}");
}