Skip to content

Commit

Permalink
Basic sample sketches moved to its own repository
Browse files Browse the repository at this point in the history
  • Loading branch information
dberenguer committed Jun 25, 2015
1 parent 2e69a32 commit bd1f52f
Show file tree
Hide file tree
Showing 9 changed files with 823 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Basic applications for panStamp.

1. simpletest : basic "blink a LED" application.

2. basicbeacon : Basic wireless beacon transmitter.

3. basicradio : Basic bi-directional application that pairs with another
basicradio node and toggles an LED whenever a packet is received.

4. modem : Serial wireless modem that interfaces between a network of panStamps
and a serial port (RS232, USB, ...). This application is used as the modem for
all computer applications developed for panStamp.

Description of the serial (ASCII) protocol:
https://github.com/panStamp/panstamp/wiki/Modem%20and%20serial%20protocol

5. aesencryption : use of the hadware AES-128 encryption engine available
on the CC430 MCU (panStamp NRG only).

6. ctrencryption : CTR-cypher algorithm on top of the basic AES-128 encryption
(panStamp NRG only).


63 changes: 63 additions & 0 deletions aesencryption/aesencryption.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Copyright (c) 2014 Daniel Berenguer <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 10/17/2014
*/

#include "HardwareSerial.h"
#include "cc430aes.h"

CC430AES aes;

void setup()
{
byte i;

Serial.begin(57600);

// 128-bit key
byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
byte data[] = "Data to be encrypted/decrypted needs to be aligned to 16 bytes..";

Serial.print("Input string: ");
Serial.println((char*) data);
Serial.println("Setting AES-128 key");

// Set key
aes.setKey(key);

Serial.println("Encripting...");
// Encrypt
aes.aesEncrypt(data, sizeof(data)-1);

Serial.println("Data encrypted");
Serial.println("Now decripting...");

// Decrypt
aes.aesDecrypt(data, sizeof(data)-1);

Serial.print("Data decrypted: ");
Serial.println((char*) data);
}

void loop()
{
}
67 changes: 67 additions & 0 deletions basicbeacon/basicbeacon.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 07/23/2014
*/

/**
* Basic beacon example
*
* This application sleeps (ultra-low-current consumption) most of the time and
* wakes-up every five seconds to transmit a packet and then enters the sleep
* mode again
*/

#define RFCHANNEL 0 // Let's use channel 0
#define SYNCWORD1 0xB5 // Synchronization word, high byte
#define SYNCWORD0 0x47 // Synchronization word, low byte
#define SOURCE_ADDR 5 // Device address

CCPACKET packet;

void setup()
{
byte i;

// Setup LED output pin
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

panstamp.radio.setChannel(RFCHANNEL);
panstamp.radio.setSyncWord(SYNCWORD1, SYNCWORD0);
panstamp.radio.setDevAddress(SOURCE_ADDR);
panstamp.radio.setCCregs();
panstamp.setHighTxPower();

packet.length = 10;

for(i=0 ; i<packet.length ; i++)
packet.data[i] = i;
}

void loop()
{
digitalWrite(LED, HIGH);
panstamp.radio.sendData(packet);
digitalWrite(LED, LOW);
panstamp.sleepSec(5);
}

86 changes: 86 additions & 0 deletions basicradio/basicradio.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 07/23/2014
*/

/**
* Basic radio example
*
* Each node acts simultaneously as sender and receiver. Each node sends a packet
* every five senconds and toggles a LED each time a packet is received
*/

#define RFCHANNEL 0 // Let's use channel 0
#define SYNCWORD1 0xB5 // Synchronization word, high byte
#define SYNCWORD0 0x47 // Synchronization word, low byte
#define SOURCE_ADDR 4 // Sender address
#define DESTINATION_ADDR 5 // Receiver address

CCPACKET txPacket; // packet object
byte count = 0;

/**
* This function is called whenever a wireless packet is received
*/
void rfPacketReceived(CCPACKET *packet)
{
// The LED will toggle when a new packet is received
digitalWrite(LED, !digitalRead(LED));

if (packet->length > 1)
{
// packet.data[0]; // Our source address
// packet.data[1]; // Self-incrementing value generated by the sender
}
}

void setup()
{
// Setup LED output pin
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);

panstamp.radio.setChannel(RFCHANNEL);
panstamp.radio.setSyncWord(SYNCWORD1, SYNCWORD0);
panstamp.radio.setDevAddress(SOURCE_ADDR);
panstamp.radio.setCCregs();

// Let's disable address check for the current project so that our device
// will receive packets even not addressed to it.
panstamp.radio.disableAddressCheck();

// Declare RF callback function
panstamp.setPacketRxCallback(rfPacketReceived);
}

void loop()
{
txPacket.length = 2; // Let's send a single data byte plus the destination address

txPacket.data[0] = DESTINATION_ADDR; // First data byte has to be the destination address
txPacket.data[1] = count++; // Self-incrementing value
panstamp.radio.sendData(txPacket); // Transmit packet

delay(5000); // Transmit every 5 seconds
// For low-power applications replace "delay" by "panstamp.sleepWd(WDTO_8S)" for example
}

64 changes: 64 additions & 0 deletions ctrencryption/ctrencryption.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (c) 2014 Daniel Berenguer <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 10/19/2014
*/

/**
* This sketch is only compatible with the NRG platform
*/

#include "HardwareSerial.h"
#include "cc430aes.h"

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

// 128-bit key
byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
byte data[] = "Data to be encrypted/decrypted does not need to be aligned to 16 bytes";
long nonce = 1654;

Serial.print("Input string: ");
Serial.println((char*) data);
Serial.println("Setting AES-128 key");

// Set key
CC430AES::setKey(key);

Serial.println("Encripting...");
// Encrypt
CC430AES::ctrCrypto(data, sizeof(data)-1, nonce);

Serial.println("Data encrypted");
Serial.println("Now decripting...");

// Decrypt
CC430AES::ctrCrypto(data, sizeof(data)-1, nonce);

Serial.print("Data decrypted: ");
Serial.println((char*) data);
}

void loop()
{
}
83 changes: 83 additions & 0 deletions modem/modem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* modem.h
*
* Copyright (c) 2014 panStamp <[email protected]>
*
* This file is part of the panStamp project.
*
* panStamp is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* panStamp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with panStamp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* Author: Daniel Berenguer
* Creation date: 10/12/10
*/

#ifndef _MODEM_H
#define _MODEM_H

#include "Arduino.h"
#include "HardwareSerial.h"
#include "version.h"


#define SERIAL_BUF_LEN 128 // Maximum length for any serial string
#define TIMER1_TICK_PERIOD_US 500000 // Timer1 tick = 500 ms
#define MAX_SERIAL_SILENCE_MS 1000 // Maximum serial silence between serial command characters = 1000 ms
#define MAX_SERIAL_SILENCE_TK MAX_SERIAL_SILENCE_MS / (TIMER1_TICK_PERIOD_US / 1000) // Max serial silence in ticks

/**
* AT command set
*/
#define AT_ATTENTION "AT" // Basic Attention command
#define AT_RESET "ATZ" // Reset modem
#define AT_GOTO_CMDMODE "+++" // Go to command mode
#define AT_GOTO_DATAMODE "ATO" // Go to data mode
#define AT_HVERSION "ATHV" // Hardware version
#define AT_FVERSION "ATFV" // Firmware version
#define AT_FREQCHANNEL "ATCH" // Frequency channel
#define AT_SYNCWORD "ATSW" // Synchronization word
#define AT_DEVADDRESS "ATDA" // Device address
#define AT_ADDRCHECK "ATAC" // Address check

/**
* Serial modes
*/
enum SERMODE
{
SERMODE_DATA,
SERMODE_COMMAND
};

/**
* Type of AT query
*/
enum ATQUERY
{
ATQUERY_COMMAND = 0,
ATQUERY_REQUEST
};

/**
* Global variables
*/
CCPACKET *rxPacket;
char strSerial[SERIAL_BUF_LEN]; // Serial buffer
byte ch;
int len = 0;
SERMODE serMode = SERMODE_DATA; // Serial mode (data or command mode)
boolean packetAvailable = false; // Wireless packet received when true

#endif // _MODEM_H

Loading

0 comments on commit bd1f52f

Please sign in to comment.