Here is code for 1:
#include <RFM69.h>
#include <Wire.h>
#include <RFM69_ATC.h>
#define NODEID 999
#define NETWORKID 100
#define FREQUENCY RF69_433MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ)
#define KEY "iBooriqueinaenae" //has to be same 16 characters/bytes on all nodes, not more not less!
#define SERIAL_BAUD 115200
#define IS_RFM69HW_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW!
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
#ifdef ENABLE_ATC
RFM69_ATC radio;
#else
RFM69 radio;
#endif
typedef struct {
float temp;
float humi;
float volts;
int nodeId;
int counter;
int16_t rssi;
} Payload;
Payload theData;
void setup() {
Serial.begin(SERIAL_BAUD);
delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW_HCW
radio.setHighPower(); //must include this only for RFM69HW/HCW!
#endif
radio.encrypt(KEY);
Wire.begin(); // join i2c bus (address optional for master)
}
void loop() {
if (radio.receiveDone())
{
if (radio.DATALEN != sizeof(Payload))
Serial.print("Invalid payload received, not matching Payload struct!");
else
{
theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else
theData.rssi=radio.RSSI;
}
if (radio.ACKRequested())
{
radio.sendACK();
}
Wire.beginTransmission(8); // transmit to device #8
Wire.write((byte *)&theData, sizeof(theData));
Wire.endTransmission(); // stop transmitting
}
}
And here is the code for 2:
#include <UIPEthernet.h>
#include <Wire.h>
#include <avr/wdt.h>
typedef struct {
float temp;
float humi;
float volts;
int nodeId;
int counter;
int16_t rssi;
} Payload;
Payload payload;
char universal_buff[8];
bool havedata = false;
bool sent = 0;
unsigned int retries = 0; // variable for controlling retrying of sending via ethernet
#define MAX_RETRIES 3 //max amount of attempts for ethernet sending
//
// Ethernet config
//
byte mac[] = { 0x6a, 0xce, 0x22, 0x09, 0xb8, 0x25 };
IPAddress ip(192,168,2,2);
IPAddress gateway(192, 168, 2, 106);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
char pageAdd[94];
//
// End - Ethernet config
//
void setup() {
// Start ethernet
Ethernet.begin(mac, ip, gateway, subnet);
wdt_enable(WDTO_8S); //watchdog enable, 8 seconds
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
//Serial.begin(115200); // start serial for output
wdt_reset();
}
void loop() {
wdt_reset();
if (havedata == true) {
//prepare and send the data via eth
dtostrf(payload.temp,1,2,universal_buff);
sprintf(pageAdd, "/add.php?temp=%s&", universal_buff);
dtostrf(payload.humi,1,2,universal_buff);
sprintf(pageAdd + strlen(pageAdd), "humid=%s&", universal_buff);
dtostrf(payload.volts,1,4,universal_buff);
sprintf(pageAdd + strlen (pageAdd), "voltage=%s&counter=%d&auth=12345678&s=%d&rssi=%d", universal_buff, payload.counter,payload.nodeId,payload.rssi);
//
// Ethernet send part
//
sent = false;
retries = 0;
do
{
sent = sendData(pageAdd);
if ( retries++ > MAX_RETRIES )
sent = true;
}
while ( sent == 0 );
//
// END - thernet send part
//
havedata = false;
}
}
void receiveEvent(int howMany) {
// read into structure
byte * p = (byte *) &payload;
for (byte i = 0; i < sizeof(payload); i++)
*p++ = Wire.read ();
havedata = true;
}
bool sendData(char *page)
{
char outBuf[107];
bool cconn=false;
unsigned int connectLoop=0;
do //try connecting until connected or timeout
{
connectLoop++;
cconn = client.connect(IPAddress(192,168,2,106),80);
if ( connectLoop > 10000 ) return 0; //timeout if too many not succsssfull attempts, return 0
}
while ( cconn == false );
if ( cconn == 1 )
{
sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
client.println(F("Host: 192.168.2.106"));
client.println(F("Connection: close\r\n"));
connectLoop = 0;
while (!client.available())
{
connectLoop++;
if ( connectLoop > 10000 ) return 0; //timeout if client was not available for long time, return 0
}
connectLoop = 0;
while (client.read() > 0)
{
connectLoop++;
if ( connectLoop > 10000 ) return 0; //timeout if reading data took too long, return 0
}
client.stop();
return 1;
}
}