#include <SPI.h>
#include <Ethernet.h>
#include <ThingSpeak.h>
// Ethernet settings
byte mac[] = {0, 1, 2, 3, 4, 5}; // Ethernet shield MAC address
EthernetClient client;
unsigned long myChannelNumber = 2585435;
const char * myWriteAPIKey = "TQVRT2AYBXYVU3DQ";
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin1 = 12;
const int ledPin2 = 13;
int buttonState1 = 0;
int lastButtonState1 = 0;
bool ledOn1 = false;
int buttonState2 = 0;
int lastButtonState2 = 0;
bool ledOn2 = false;
void setup() {
// Initialize pins
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(9600);
// Start Ethernet
if (Ethernet.begin(mac) == 0) {
//Serial.println("Failed to configure Ethernet using DHCP");
// Try to configure using IP address instead of DHCP:
Ethernet.begin(mac, IPAddress(192, 168, 1, 177));
}
// Give the Ethernet shield a second to initialize:
delay(1000);
// Start ThingSpeak
ThingSpeak.begin(client);
// Initial LED states
digitalWrite(ledPin1, ledOn1 ? HIGH : LOW);
digitalWrite(ledPin2, ledOn2 ? HIGH : LOW);
}
void loop() {
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == LOW && lastButtonState1 == HIGH) {
ledOn1 = !ledOn1;
digitalWrite(ledPin1, ledOn1 ? HIGH : LOW);
updateThingSpeak();
delay(50);
}
lastButtonState1 = buttonState1;
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == LOW && lastButtonState2 == HIGH) {
ledOn2 = !ledOn2;
digitalWrite(ledPin2, ledOn2 ? HIGH : LOW);
updateThingSpeak();
delay(50);
}
lastButtonState2 = buttonState2;
}
void updateThingSpeak() {
ThingSpeak.setField(1, ledOn1 ? 1 : 0);
ThingSpeak.setField(2, ledOn2 ? 1 : 0);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}