Skip to content

Commit

Permalink
renamed file from .ino to .cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoostenveld committed Feb 18, 2019
1 parent 70d6ea2 commit 9ac05c4
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 104 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions rfm12b_recv_xxxx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

This is the sketch for the Arduino pro mini with the RFM12b module.

This is a sketch for an Arduino pro mini attached to a RFM12b module. It operates in conjunction with the "rfm12b_thingspeak" sketch, which is running on an Arduino Uno with an Ethershield. The two Arduino's exchange sensor data over a i2c connection.

See http://robertoostenveld.nl/arduino-relay-rfm12b-thingspeak/ for details.
58 changes: 13 additions & 45 deletions rfm12b_recv_xxxx/rfm12b_recv_xxxx.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ typedef struct payload_t {

payload_t payload;

/*****************************************************************************/
/*******************************************************************************************************************/

void setup () {
#ifdef BLIP_DEBUG
Expand All @@ -44,7 +44,7 @@ void setup () {
rf12_initialize(BLIP_NODE, RF12_868MHZ, BLIP_GRP);
}

/*****************************************************************************/
/*******************************************************************************************************************/

void loop () {

Expand All @@ -54,22 +54,22 @@ void loop () {
// volatile byte rf12_crc - CRC of the received packet, zero indicates correct reception. If != 0 then rf12_hdr, rf12_len, and rf12_data should not be relied upon.

if (rf12_recvDone()) {
if(RF12_WANTS_ACK){
rf12_sendStart(RF12_ACK_REPLY,0,0);
if (RF12_WANTS_ACK) {
rf12_sendStart(RF12_ACK_REPLY, 0, 0);
}

if (rf12_crc!=0) {
if (rf12_crc != 0) {
Serial.println("Invalid crc");
}
else if (rf12_len!=sizeof payload) {
else if (rf12_len != sizeof payload) {
Serial.println("Invalid len");
}
else {
memcpy(&payload, (void *)rf12_data, sizeof payload);

unsigned long check = crc_buf((char *)&payload, sizeof(payload_t) - sizeof(unsigned long));
if (payload.crc==check) {


if (payload.crc == check) {
write_i2c((byte *)&payload.id, sizeof(unsigned long));
write_i2c((byte *)&payload.counter, sizeof(unsigned long));
write_i2c((byte *)&payload.value1, sizeof(float));
Expand Down Expand Up @@ -106,39 +106,15 @@ void loop () {
}
} // loop

/*****************************************************************************/

void transmitfloat(float x) {
byte *b = (byte *)&x;
Wire.beginTransmission(9); // transmit to device #9
Wire.write(b[0]);
Wire.write(b[1]);
Wire.write(b[2]);
Wire.write(b[3]);
Wire.endTransmission(); // stop transmitting
delay(5); // otherwise transmission hangs
}

void transmitlong(long x) {
byte *b = (byte *)&x;
Wire.beginTransmission(9); // transmit to device #9
Wire.write(b[0]);
Wire.write(b[1]);
Wire.write(b[2]);
Wire.write(b[3]);
Wire.endTransmission(); // stop transmitting
delay(5); // otherwise transmission hangs
}
/*******************************************************************************************************************/

void write_i2c(byte *b, int n) {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(b[0]);
Wire.write(b[1]);
Wire.write(b[2]);
Wire.write(b[3]);
for (i = 0; i < n; i++)
Wire.write(b[i]);
Wire.endTransmission(); // stop transmitting
delay(5); // otherwise the next transmission hangs
}
}

/*******************************************************************************************************************/

Expand All @@ -162,7 +138,7 @@ unsigned long crc_update(unsigned long crc, byte data)
unsigned long crc_buf(char *b, long l)
{
unsigned long crc = ~0L;
for (unsigned long i=0; i<l; i++)
for (unsigned long i = 0; i < l; i++)
crc = crc_update(crc, ((char *)b)[i]);
crc = ~crc;
return crc;
Expand All @@ -176,11 +152,3 @@ unsigned long crc_string(char *s)
crc = ~crc;
return crc;
}








2 changes: 1 addition & 1 deletion rfm12b_thingspeak/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Arduino relay between RFM12b and ThingSpeak

This is the sketch for the Arduino Uno with the Ethershield.
This is a sketch for an Arduino Uno with an Ethershield. It operates in conjunction with the "rfm12b_recv_xxxx" sketch, which is running on an Arduino pro mini with a RFM12b module attached. The two Arduino's exchange sensor data over a i2c connection.

See http://robertoostenveld.nl/arduino-relay-rfm12b-thingspeak/ for details.
103 changes: 45 additions & 58 deletions rfm12b_thingspeak/rfm12b_thingspeak.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@
#include <Ethernet.h>
#include <Wire.h>
#include <String.h>
#include <Syslog.h>
#include <MemoryFree.h>
#include "secret.h"

#define DEBUG_PRINT(x)
#define DEBUG_PRINT2(x, y)
#define DEBUG_PRINTLN(x)
#include "secret.h"

/*
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINT2(x, y) Serial.print (x, y)
#define DEBUG_PRINTLN(x) Serial.println (x)
*/
The write API keys for ThingSpeak are defined in secret.h like this
/*
#define DEBUG_PRINT(x) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
#define DEBUG_PRINT2(x, y) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
#define DEBUG_PRINTLN(x) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
*/
#define APIKeyChannel2 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel3 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel4 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel5 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel6 "XXXXXXXXXXXXXXXX"
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x2A, 0x4A };
byte loghost[] = { 192, 168, 1, 7 };
*/

#ifndef DEBUG
#define DEBUG_PRINT(x)
#define DEBUG_PRINT2(x, y)
#define DEBUG_PRINTLN(x)
#elif DEBUG == SERIAL
#define DEBUG_PRINT(x) Serial.print (x)
#define DEBUG_PRINT2(x, y) Serial.print (x, y)
#define DEBUG_PRINTLN(x) Serial.println (x)
#elif DEBUG == LOGGER
#include <Syslog.h>
#define DEBUG_PRINT(x) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
#define DEBUG_PRINT2(x, y) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
#define DEBUG_PRINTLN(x) Syslog.logger(1, 6, "rfm12b_thingspeak:", "", x);
#else
#error "Unexpected value of DEBUG."
#endif

byte buf[32]; // ring buffer
unsigned int bufptr = 0; // pointer into ring buffer
Expand All @@ -38,16 +45,8 @@ unsigned int bufblk = 0; // boolean to block buffer updates
IPAddress server(184, 106, 153, 149); // IP Address for the ThingSpeak API
EthernetClient client;

/*
The write API keys for ThingSpeak are defined in secret.h like this
#define APIKeyChannel2 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel3 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel4 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel5 "XXXXXXXXXXXXXXXX"
#define APIKeyChannel6 "XXXXXXXXXXXXXXXX"
*/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x2A, 0x4A };
byte loghost[] = { 192, 168, 1, 7 };

typedef struct message_t {
unsigned long id;
Expand All @@ -64,9 +63,6 @@ typedef struct message_t {
unsigned long lastConnectionTime = 0;
boolean lastConnectionStatus = false;
unsigned int resetCounter = 0;
unsigned int keepalive_count = 0;
unsigned int keepalive_time = 0;


/*******************************************************************************************************************/

Expand Down Expand Up @@ -99,8 +95,10 @@ void setup() {
Wire.begin(9); // Start I2C Bus as a Slave (Device Number 9)
Wire.onReceive(receiveEvent); // register event

#if defined(DEBUG) && DEBUG == LOGGER
Syslog.setLoghost(loghost);
Syslog.logger(1, 5, "", "rfm12b_thingspeak:", "setup finished");
#endif

} // setup

Expand All @@ -118,49 +116,38 @@ void loop() {
resetCounter = 0;
}

if ((millis() - keepalive_time) > 30000) {
keepalive_count++;
keepalive_time = millis();
String postString = "&field1=" + String(keepalive_count) + "&field2=" + String(int(keepalive_time / 1000));
// updateThingSpeak(postString, APIKeyChannel1);
Serial.println(postString);
}

if (bufblk) {
// the I2C buffer is full and needs to be processed
message_t *message = (message_t *)buf;

// DEBUG_PRINTLN("packet received");
// DEBUG_PRINT(message->id);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT(message->counter);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT2(message->value1, 2);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT2(message->value2, 2);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT2(message->value3, 2);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT2(message->value4, 2);
// DEBUG_PRINT(",\t");
// DEBUG_PRINT2(message->value5, 2);
// DEBUG_PRINT(",\t");
// DEBUG_PRINTLN(message->crc);
DEBUG_PRINTLN("packet received");
DEBUG_PRINT(message->id);
DEBUG_PRINT(",\t");
DEBUG_PRINT(message->counter);
DEBUG_PRINT(",\t");
DEBUG_PRINT2(message->value1, 2);
DEBUG_PRINT(",\t");
DEBUG_PRINT2(message->value2, 2);
DEBUG_PRINT(",\t");
DEBUG_PRINT2(message->value3, 2);
DEBUG_PRINT(",\t");
DEBUG_PRINT2(message->value4, 2);
DEBUG_PRINT(",\t");
DEBUG_PRINT2(message->value5, 2);
DEBUG_PRINT(",\t");
DEBUG_PRINTLN(message->crc);

if (message->crc == crc_buf((char *)message, sizeof(message_t) - sizeof(unsigned long))) {

DEBUG_PRINTLN("CRC valid.");

// String postString = "id=" + String(message->id) + " counter=" + String(message->counter);
// DEBUG_PRINTLN(postString);

String postString = "&field1=" + String(message->value1) + "&field2=" + String(message->value2) + "&field3=" + String(message->value3) + "&field4=" + String(message->value4) + "&field5=" + String(message->value5) + "&field6=" + String(message->counter);
DEBUG_PRINTLN(postString);

// connect to ThingSpeak and forward the received message
switch (message->id) {
case 1:
// node 1 is the receiving node, which does not have sensors
updateThingSpeak(postString, APIKeyChannel1);
break;
case 2:
updateThingSpeak(postString, APIKeyChannel2);
Expand Down

0 comments on commit 9ac05c4

Please sign in to comment.