Hello,
I would appreciate some help in writing the correct program for my Arduino Mega.
My hardware setup:
Sending Unit: Arduino Uno + Sparkfun Weather Shield and sensors + Xbee Pro S2C
Receiving Unit: Arduino Mega + W5100 Ethernet Shield + Xbee Pro S2
Sending Unit code come from Sparkfun weather shield website and is attached.
The attached program will print to my serial monitor via USB.
It also prints to my serial monitor via XBee when there is no code loaded onto the receiving unit (Mega).
Note that I wired my receiving unit so that the Xbee is connect to the TX1/RX1 pins on the Mega and was successful in getting it to print to the serial monitor using the following code:
if (Serial.available() > 0) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
I have also been successful in using the WriteVoltage example from the ThingSpeak library. So I'm connecting to the internet and able to write to ThingSpeak.
I have scoured these forums and other sources on the internet to find the correct code for the receiving unit (Mega). I finally decided to try what I can using some of the examples. I would sincerely appreciate any guidance. I'm still relative newbie to coding and trying to get an electrical engineering degree at age 39 (they say it's never too late!).
The receiving unit code is:
#include<SPI.h>
#include <Ethernet.h>
#include<Wire.h>
#include<ThingSpeak.h>
/*#define winddir 1
#define windspeedmph 2
#define humidity 3
#define tempf 4
#define rainin 5
#define dailyrainin 6
#define pressure 7*/
int DEBUG = 0;
byte mac[] = {0x90, 0xA2, 0XDA, 0x0F, 0x72, 0xFF };//for shield in the system
IPAddress ip(192, 168, 1, 13); //LOCAL IP address not used by other devices on the network
EthernetClient client;
unsigned long myChannelNumber = 225397; //ThingSpeak Channel number
const char * myWriteAPIKey = "KE8HNNPHCH8FEDAH";//ThingSpeak Write API Key
void setup() {
Serial.begin(9600); // connect to the serial port
Serial1.begin(9600);
// Turn the internet on
Serial.print("\nInitializing...");
if (Ethernet.begin(mac) ){
Serial.println("Initialization complete");
}
else {
Serial.println("Something went wrong during ethernet startup!");
}
#ifdef ARDUINO_AVR_YUN
Bridge.begin();
#else
#if defined(ARDUINO_ARCH_ESP8266) || defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000)
WiFi.begin(ssid, pass);
#else
Ethernet.begin(mac);
#endif
#endif
ThingSpeak.begin(client);
}
void loop() {
if (Serial.available() > 0) {
int inByte = Serial1.read();
Serial.write(inByte);
}
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
Serial.print(char(Serial1.read()));
}
int winddir = Serial.parseInt();
float windspeedmph = Serial.parseFloat();
float humidity = Serial.parseFloat();
float tempf = Serial.parseFloat();
float rainin = Serial.parseFloat();
float dailyrainin = Serial.parseFloat();
float pressure = Serial.parseFloat();
if (Serial.read() == '\n') {
ThingSpeak.writeField(myChannelNumber, 1, winddir, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 2, windspeedmph, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 3, humidity, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 4, tempf, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 5, rainin, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 6, dailyrainin, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 7, pressure, myWriteAPIKey);
delay(20000);
}
}
When I open the serial monitor I get the "Initializing...Initialization Complete" and nothing else.
Note, I don't have much error recognition built in and am open to suggestions.
Credit will be giving within the program if so desired.
Please let me know if any additional information is needed.
Many, many thanks!
_4482_Weather_Full_Test.ino (14.6 KB)