Close

Demo Code from the Semifinals Video

A project log for analog.io - A full stack IoT platform

A full stack project dedicated to easily collecting, analyzing and sharing IoT sensor data.

luke-benoLuke Beno 09/21/2015 at 20:450 Comments

915 MHz Sensor Node:

#include <SPI.h>
#include <AIR430BoostFCC.h>
#include "I2C_SoftwareLibrary.h"

#define SCL_PIN P2_6 ///< pin for SCL
#define SDA_PIN P2_7 ///< pin for SDA
SoftwareWire Wire(SDA_PIN, SCL_PIN); 

#define ADDRESS_LOCAL    0x02
#define ADDRESS_REMOTE   0x01
#define DROPLET
#define _address 0x40

//#define NODE_NUM 4

struct sPacket
{
  int   node;
  float temp;       // Local node address that message originated from
  float rh;    // Local node message [MAX. 59 bytes]
};

struct sPacket txPacket;

void setup()
{
  txPacket.node = 9;
  
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);
  
  pinMode(RED_LED, OUTPUT);
  digitalWrite(RED_LED, LOW);
  Radio.begin(ADDRESS_LOCAL, CHANNEL_1, POWER_MAX);
  digitalWrite(RED_LED, HIGH);
}

void loop()
{
  Wire.beginTransmission(_address);
  Wire.write(0xE5);
  Wire.endTransmission(false);
  //Wire.endTransmission();
  //delay(20);
  Wire.requestFrom(_address, 2);
  while (Wire.available() < 2);
  uint16_t rhMS = Wire.read();
  uint16_t rhLS = Wire.read();
  
  Wire.beginTransmission(_address);
  Wire.write(0xE0);
  Wire.endTransmission(false);
  //Wire.endTransmission();
  //delay(20);
  Wire.requestFrom(_address, 2);
  while (Wire.available() < 2);
  uint16_t MS = Wire.read();
  uint16_t LS = Wire.read();
  
  //Serial.println(MS<<8LS,DEC);
  //Serial.println(MS,BIN);
  
  txPacket.temp = (MS<<8)+LS;
  txPacket.rh = (rhMS<<8)+rhLS;
  //Serial.println(t,0);
  
  txPacket.temp=((175.72*txPacket.temp)/65536-46.85)*9/5+32;
  txPacket.rh = (125*txPacket.rh)/65536-6;
  
  digitalWrite(GREEN_LED, LOW);
  Radio.transmit(ADDRESS_REMOTE, (unsigned char*)&txPacket, sizeof(txPacket));
  digitalWrite(GREEN_LED, HIGH);
  sleepSeconds(4);
}
2.4 GHz Sensor Node:

#include <analog_io.h>
#include <SPI.h>
#include "I2C_SoftwareLibrary.h"

analog_io radio(P3_5, P3_6, P2_5);  // P2.0=CE, P2.1=CSN, P2.2=IRQ
const uint8_t txaddr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01 };

SoftwareWire Wire(P3_0, P2_1); 
//SoftwareWire Wire(P3_1, P1_3); 

int node_num = 4;

#define _address 0x40

void setup() {
  pinMode(RED_LED, OUTPUT); 
  digitalWrite(RED_LED, LOW);
  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);

  radio.begin();  // Defaults 1Mbps, channel 0, max TX power

  radio.setTXaddress((void*)txaddr);
}

void loop() {
  
  Wire.beginTransmission(_address);
  Wire.write(0xE5);
  Wire.endTransmission(false);
  //Wire.endTransmission();
  //delay(20);
  Wire.requestFrom(_address, 2);
  while (Wire.available() < 2);
  uint16_t rhMS = Wire.read();
  uint16_t rhLS = Wire.read();
  
  Wire.beginTransmission(_address);
  Wire.write(0xE0);
  Wire.endTransmission(false);
  //Wire.endTransmission();
  //delay(20);
  Wire.requestFrom(_address, 2);
  while (Wire.available() < 2);
  uint16_t MS = Wire.read();
  uint16_t LS = Wire.read();
  
  //Serial.println(MS<<8LS,DEC);
  //Serial.println(MS,BIN);
  
  float t = (MS<<8)+LS;
  float rh = (rhMS<<8)+rhLS;
  //Serial.println(t,0);
  
  t=((175.72*t)/65536-46.85)*9/5+32;
  rh = (125*rh)/65536-6;
  
  digitalWrite(RED_LED, HIGH);
  radio.print("{\"id\":\"N");
  radio.print(node_num);
  radio.print("\",\"t\":");
  radio.print(t,1);
  radio.print(",\"rh\":");
  radio.print(rh,1);
  radio.print("}");
  radio.flush();  // Force transmit (don't wait for any more data)
  
  /*radio.print(node_num);
  radio.print(":");
  radio.print(t,1);
  radio.print("F ");
  radio.print(rh,1);
  radio.print("%");
  radio.flushBle();  // Force transmit (don't wait for any more data)
 */
  digitalWrite(RED_LED, LOW);
  //radio.deepsleep();
  sleepSeconds(3);
  //sleep(4000);
}

915MHz Hub Code:

#include <SPI.h>
#include <AIR430BoostFCC.h>

#define ADDRESS_LOCAL    0x01
char escape_char = 0xFF;

struct data_packet
{
  int   node;
  float temp;       // Local node address that message originated from
  float rh;    // Local node message [MAX. 59 bytes]
};

struct data_packet rxPacket;

void setup()
{
  // The radio library uses the SPI library internally, this call initializes
  // SPI/CSn and GDO0 lines. Also setup initial address, channel, and TX power.
  pinMode(LNA_EN, OUTPUT);
  pinMode(PA_EN, OUTPUT);
  pinMode(HGM, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(RED_LED, HIGH);
  
  digitalWrite(LNA_EN, LOW);
  digitalWrite(PA_EN, LOW);
  digitalWrite(HGM, LOW);
  
  //pinMode(P3_6, INPUT);
 // pinMode(P3_5, INPUT);
 
  digitalWrite(RED_LED, LOW);
  Serial.begin(38400);
  Radio.begin(ADDRESS_LOCAL, CHANNEL_1, POWER_MAX);
  digitalWrite(RED_LED, HIGH);
}

void loop()
{
  digitalWrite(LNA_EN, HIGH);
  digitalWrite(HGM, HIGH);
  
  unsigned char numbytes = Radio.receiverOn((unsigned char*)&rxPacket, sizeof(rxPacket), 1000);
  if (numbytes>0){
    digitalWrite(GREEN_LED, LOW);
    writeSof();
    Serial.print("{\"id\":\"D");
    Serial.print(rxPacket.node);
    Serial.print("\",\"t\":");
    Serial.print(rxPacket.temp);
    Serial.print(",\"rh\":");
    Serial.print(rxPacket.rh);
    Serial.print("}");
    writeEof();
    digitalWrite(GREEN_LED, HIGH);
  }
}

void writeToHub(char c)
{
  if (c==escape_char){
    Serial.write(escape_char);
  }
  Serial.write(c);
}
void writeEof()
{
  Serial.write(escape_char);
  Serial.write(0x02);
}
void writeSof()
{
  Serial.write(escape_char);
  Serial.write(0x01);
}

2.4GHz Receiver Code:

#include <analog_io.h>
#include <SPI.h>

analog_io radio(P2_6, P2_7, P2_5); //CE csn irq
const uint8_t rxaddr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0x01 };

char escape_char = 0xFF;

void setup() {
  Serial.begin(38400);

  SPI.begin();
  SPI.setDataMode(SPI_MODE0);
  SPI.setBitOrder(MSBFIRST);
  
  radio.begin();  // Defaults 1Mbps, channel 0, max TX power

  radio.setRXaddress((void*)rxaddr);
  
  radio.enableRX();  // Start listening
}

void loop() {
  char inbuf[33];
 
  while (!radio.available(true))
    ;
  if (radio.read(inbuf)) {
    writeSof();
    Serial.print(inbuf);
    writeEof();
  }
}

void writeEof()
{
  Serial.write(escape_char);
  Serial.write(0x02);
}
void writeSof()
{
  Serial.write(escape_char);
  Serial.write(0x01);
}
Electric Imp Device Code:

// Create a message buffer
local msg = ""
local msgN = ""
local escape = false
local escapeN = false
local escape_char = 0xFF
local sof = false
local sofN = false
local reconfigs = 0

// A function that is called everytime that the UART receives a byte
function serialRx() {
  local i = 0
  local c = 0
  
  //Pull a byte from the UART FIFO
  local uart_blob = hardware.uart57.readblob()
  
  for (i=0;i<uart_blob.len();i++)
  {
    c = uart_blob[i]
    if (escape)
    {
      switch (c){
        case 0x01:
          sof=true
          msg = "";
          break
        case 0x02:
          //agent.send("receiveData",msg+hardware.getdeviceid())
          agent.send("data",msg)
          //agent.send("log_rssi",imp.rssi())
          msg = "";
          sof=false
          break
        case 0xFF:
          if (sof){
            msg=msg+format("%c",c);
          }
          break
      }
      escape = false
    }
    else
    {
      if (c==escape_char){
        escape = true
      }
      else {
        if (sof){
          msg=msg+format("%c",c);
        }
      }
    }
  }
}

// A function that is called everytime that the UART receives a byte
function serialRxN() {
  local i = 0
  local c = 0
  
  //Pull a byte from the UART FIFO
  local uart_blob = hardware.uart12.readblob()
  
  for (i=0;i<uart_blob.len();i++)
  {
    c = uart_blob[i]
    if (escapeN)
    {
      switch (c){
        case 0x01:
          sofN=true
          msgN = "";
          break
        case 0x02:
          //agent.send("receiveData",msg+hardware.getdeviceid())
          agent.send("dataN",msgN)
          //agent.send("log_rssi",imp.rssi())
          msgN = "";
          sofN=false
          break
        case 0xFF:
          if (sofN){
            msgN=msgN+format("%c",c);
          }
          break
      }
      escapeN = false
    }
    else
    {
      if (c==escape_char){
        escapeN = true
      }
      else {
        if (sofN){
          msgN=msgN+format("%c",c);
        }
      }
    }
  }
}

// Setup hardware UART with serialRx event to receive bytes
hardware.uart57.configure(38400, 8, PARITY_NONE, 1, NO_CTSRTS, serialRx);
hardware.uart12.configure(38400, 8, PARITY_NONE, 1, NO_CTSRTS, serialRxN);

//agent.send("log_msg","device booted: "+hardware.getdeviceid().toupper())
Electric Imp Agent Code:

local private = "..."
local public  = "..."

function processJson(json) {
  local data = http.jsondecode(json)
  //server.log(json)
  if (data.t > 0)
  {
    local node_id = format("%c",data.id[1]).tointeger()-1
    
    // Prepare the phant headers
    local phantHeaders = {"Phant-Private-Key": private, "connection": "close"};
    
    // Create a post request
    local req = http.post("http://lgbeno.analog.io/input/"+public, phantHeaders,http.urlencode(data));
    
    req.sendsync().statuscode;
  }
}

function processJsonN(json) {
  local data = http.jsondecode(json)
  //server.log(json)
  if (data.t > 0) 
  {
    local node_id = format("%c",data.id[1]).tointeger()-1
    
    // Prepare the phant headers
    local phantHeaders = {"Phant-Private-Key": private, "connection": "close"};
    
    // Create a post request
    local req = http.post("http://lgbeno.analog.io/input/"+public, phantHeaders,http.urlencode(data));
    
    req.sendsync().statuscode;
  }
}

device.on("data",processJson)
device.on("dataN",processJsonN)
Javascript Web Page:

Here is the JS Fiddle

Discussions