Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- BrokenSidewalkFarm.com - Wicking Bed Garden Controller
- Licensed under Creative Commons NonCommercial Share-Alike with Attribution (BY-NC-SA)
- Louis (Thad) T. Getterman IV, Broken Sidewalk Farm - http://BrokenSidewalkFarm.com/
- @TODO
- * Add Caching to SD Card and then dumps from there
- * Add Remote Firmware Upgrades?! HTF DO YOU DO THAT?
- */
- #if defined(ARDUINO) && ARDUINO > 18
- #include <SPI.h>
- #endif
- #include <Ethernet.h>
- #include <EthernetDHCP.h>
- #include <EthernetDNS.h>
- #include <EthernetBonjour.h>
- #include <Utility.h> // foreach on array (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245256501)
- const byte NUMBER_OF_PINS = 3;
- byte ledPin[NUMBER_OF_PINS] = {10,11,12};
- #include <string.h>
- #undef int() // needed by arduino 0011 to allow use of stdio
- #include <stdio.h> // for function sprintf
- #include <utility/w5100.h> // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287775341
- #define SHARE_FEED_ID 1872 // the Pachube feed ID that shares your sensor data
- #define CONTROL_FEED_ID 1260 // feed ID of the Pachube controller feed
- #define UPDATE_INTERVAL 10000 // if the connection is good wait 10 seconds before updating again - should not be less than 5
- #define RESET_INTERVAL 10000 // if connection fails/resets wait 10 seconds before trying again - should not be less than 5
- #define PACHUBE_API_KEY "ENTER_YOUR_API_KEY_HERE" // fill in your API key
- const char* bonjour_hostname = "WB1";
- // I2C BUS ADDRESS WITH 3 PINS - SEE BACKPLANE ARDUINO EXPERIMENT!
- String dnaMfgr = "Broken Sidewalk Farm";
- String dnaModel = "MS2WB";
- int dnaVer = "201111210140"; // Time Issued
- String dnaRmrks = "Initial Prototype";
- byte mac[] = { // MAC ADDRESS - make sure this is unique on your network
- 0xCC,
- 0xAC,
- 0xBE,
- 0xEF,
- 0xFE,
- 0x91
- };
- // this is from DHCP
- const char* ip_to_str(const uint8_t*);
- // REPLACE WITH DNS CALL INSTEAD
- String remoteServer = "redqueen.brokensidewalkfarm.com";
- byte remoteServerIp[] = { // RED QUEEN's IP ADDRESS
- 192,
- 168,
- 1,
- 21
- };
- // Initialize the Ethernet client library
- // with the IP address and port of the server
- // that you want to connect to (port 80 is default for HTTP):
- Client client(remoteServerIp, 80);
- // *************************************************************************************************************************** VARIABLE DECLARATION FOR STATUS AND VALUES OF PINS ON BOARD
- int state_ana[6]; // values of analog pins: 0-1023
- volatile int state_dig[14]; // values of digital pins: LOW or HIGH
- int state_pwm[6]; // values of PWM pins: 0-255
- int pins_ana[] = { // array to hold analog pin numbers (0-1023)
- 0,
- 1,
- 2,
- 3,
- 4,
- 5,
- };
- volatile int pins_dig[] = { // array to hold digital pin numbers
- 0,
- 1,
- 2,
- 4,
- 7,
- 8,
- 12,
- 13,
- // PWM PINS GO BELOW, AND THIS IS SET IN CASE THEY'RE SET AS THAT.
- 3,
- 5,
- 6,
- 9,
- 10,
- 11
- };
- int pins_pwm[] = { // array to hold PWM pin numbers
- 3,
- 5,
- 6,
- 9,
- 10,
- 11
- };
- // analaog => integer 0-1023
- // digital => high or low | input or output
- // pwm => high or low, or 0-255
- // assign pin names for analog pins, digital pins, pwm pins
- String pinNames[3][14];
- int pinVals[3][14];
- // initialize pinNames and pinVals
- /**
- 0 = Analog
- 1 = Digital
- 2 = PWM (Digital)
- */
- // / ************************************************************************************************************************* VARIABLE DECLARATION FOR STATUS AND VALUES OF PINS ON BOARD
- /*
- PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
- A4 = SDA
- A5 = SCL
- */
- // MAKE AN ARRAY OF NUMBERS THAT ARE ANALOG OR DIGITAL
- // ASSIGN NAMES TO THOSE CHANNELS
- // USE FUNCTION WITH NAME AND VALUE TO ACCESS
- // INITIALIZE CONSTANTS
- const int delaySerialRead = 0; // measure in seconds
- // Define the function pointer type.
- // The void pointer can be casted to any type of value.
- typedef void(*fctPtr)(void *);
- // Declare the functions.
- void fA(void *arg);
- void fB(void *arg);
- void fC(void *arg);
- // Declare an array of functions pointers.
- fctPtr f[3] = {fA,fB,fC}; // Array of function name (the pointer to that name).
- // The functions.
- void fA(void *arg) {
- // Cast as a pointer to an int and assign it's value to r.
- int r = *(int*)arg;
- Serial.print("fA:");
- Serial.println(r);
- }
- void fB(void *arg) {
- // Cast as a pointer to a string of chars.
- char * str = (char*)arg;
- Serial.print("fB:");
- Serial.println(str);
- }
- void fC(void *arg) {
- Serial.println("fC:Called with no parameter.");
- }
- /*
- int a = 123;
- (*f[0])(&a); // Call the first function of the array of functions.
- char b[] = "This is a test!";
- (*f[1])(&b);
- // Calling a function with no parameter.
- (*f[2])(NULL);
- // This call also work : (*f[2])(0).
- */
- void debug(char msg[100], char nl='n') {
- Serial.println(msg);
- } // END FUNCTION: DEBUG()
- void pinAssignA(String name, int pin=-1) {
- } // END FUNCTION
- void pinAssignD(String name, int dir, int pin=-1) {
- } // END FUNCTION
- void pinBlockA(int block[]) {
- } // END FUNCTION
- void pinBlockD(int block[]) {
- } // END FUNCTION
- void pinInit(String &names, String &vals) {
- int count = 0;
- int count2 = 0;
- while (count < 3) {
- count2 = 0;
- while (count2 < 14) {
- // names[count][count2] = "";
- // vals[count][count2] = 0;
- count2++;
- } // END WHILE LOOP
- count++;
- } // END WHILE LOOP
- } // END FUNCTION
- // DNS TO IP FUNCTION GOES HERE TO RETURN IP IN 4 CHUNK ARRAY
- /**
- Responsible for updating the lights to their present value
- */
- void lightAngel() {
- // check registry, use updatestatus, checkstatus
- // if network category, then examine what's there and not. internet conneciton is preferred. losing connection from prior state of success is critical - though also kick into alternative operations mode
- /*
- LED STATUS - GREEN
- LED STATUS - RED
- LED STATUS - SERVER
- LED STATUS - TRANSMIT
- LED STATUS - RECEIVE
- */
- } // END FUNCTION
- /**
- */
- void updateStatus() {
- // category
- // input device
- // priority - number that's used to gauge itself against other competitors across categories and itself
- // alert level - info, server, led, ---- RELATED TO PRIORITY?
- // status - okay, unknown, warning, critical
- // system severity: optional, preferred, critical
- } // END FUNCTION
- void checkStatus() {
- // if category great, show all. otherwise specific device of specific category
- } // END FUNCTION
- // set device light
- // set light patterns based on status for categories, as well as individual levels
- // set light patterns based on activities
- // keep priorities from superseding each other by somehow binding actions to lights? Case in point below:
- /*
- network lights will be triggered by default status of unknown. when they receive something, then they switch into warning mode, and then switch back to okay
- */
- /*
- also need to set current value of whatever light based on light functions. use true/high/255/etc for network TX/RX led indicators
- */
- /*
- USE CRON FOR ENTRIES TO CHECK STATUS ON, AND RESPOND TO.
- */
- // timing methods - http://tronixstuff.wordpress.com/2011/06/22/tutorial-arduino-timing-methods-with-millis/
- // initial setup for the board
- void setup() {
- // initialize pinNames & pinValues
- pinInit(&pinNames, &pinVals);
- // open up the Ethernet port
- // Ethernet.begin(mac, ip);
- // W5100.setRetransmissionTime(0x07D0);
- // W5100.setRetransmissionCount(3);
- // open up serial port rate
- Serial.begin(9600);
- int blockAeth[]={};
- int blockDeth[]={10,11,12,13};
- // Reserve Digital Pins for Ethernet Shield
- pinBlockD(blockDeth);
- pinAssignA("MoistSens1"); // can set pin
- pinAssignA("MoistSens2"); // can set pin
- pinAssignA("MoistSens3"); // can set pin
- pinAssignA("MoistSens4"); // can set pin
- pinAssignA("MoistSens5"); // can set pin
- pinAssignA("MoistSens6"); // can set pin
- pinAssignD("LedStatusG", OUTPUT, 3); // pwm
- pinAssignD("LedStatusR", OUTPUT, 5); // pwm
- pinAssignD("LedStatusServ", OUTPUT, 6); // pwm - leaves 9pin as only pwm left due to ethernet sucking down 2
- pinAssignD("LedStatusTx", OUTPUT, 2); // dig - shared
- pinAssignD("LedStatusRx", OUTPUT, 2); // dig - shared
- // unassigned pwm is 3 - the assignment program will try and save this off until the very last
- pinAssignD("RelayPump1", OUTPUT); //12
- pinAssignD("RelayPump2", OUTPUT); //8
- pinAssignD("RelayPump3", OUTPUT); //7
- pinAssignD("RelayPump4", OUTPUT); //4
- pinAssignD("RelayPump5", OUTPUT); //2
- pinAssignD("RelayPump6", OUTPUT); //3
- //////////
- //pinMode(12, OUTPUT);
- //pinMode(13, OUTPUT);
- //digitalWrite(12,HIGH);
- //digitalWrite(13,LOW);
- // give us time to catch a breather
- delay(1000);
- } // END FUNCTION: SETUP()
- // SETUP VARIABLES FOR OUR LOOP
- unsigned long currentMillis = 0; // know our current time updated by millis() (redundant, yes, I know... Call me superstitiuos or superstupid, your pick.)
- int currentCounts[] = {0,0,0}; // dimensions of time based off of dimensions of 10
- int priorSecs = 0; // prior second, used to detect when a second has elapsed --- TRASH THIS?
- int currentSecs = 0; // current second --- TRASH THIS?
- bool triggerServerCheckIn = true; // boolean trigger that alternates for checking into the server
- bool initLoop = true;
- void loop () {
- /*
- static DhcpState prevState = DhcpStateNone;
- static unsigned long prevTime = 0;
- DhcpState state = EthernetDHCP.poll();
- if (prevState != state) {
- Serial.println();
- switch (state) {
- case DhcpStateDiscovering:
- Serial.print("Discovering servers.");
- break;
- case DhcpStateRequesting:
- Serial.print("Requesting lease.");
- break;
- case DhcpStateRenewing:
- Serial.print("Renewing lease.");
- break;
- case DhcpStateLeased: {
- Serial.println("Obtained lease!");
- const byte* ipAddr = EthernetDHCP.ipAddress();
- const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
- const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
- Serial.print("My IP address is ");
- Serial.println(ip_to_str(ipAddr));
- Serial.print("Gateway IP address is ");
- Serial.println(ip_to_str(gatewayAddr));
- Serial.print("DNS IP address is ");
- Serial.println(ip_to_str(dnsAddr));
- Serial.println('\n');
- Serial.println("DNS server set via DHCP. Send a host name via serial to resolve it.\n");
- EthernetDNS.setDNSServer(dnsAddr);
- EthernetBonjour.begin(bonjour_hostname);
- Serial.print("Also, the board is resolvable via Bonjour/ZeroConf as ");
- Serial.print(bonjour_hostname);
- Serial.println(".local\n");
- break;
- }
- }
- } else if (state != DhcpStateLeased && millis() - prevTime > 300) {
- prevTime = millis();
- Serial.print('!');
- } else if (state == DhcpStateLeased) {
- char hostName[512];
- int length = 0;
- EthernetBonjour.run();
- while (Serial.available()) {
- hostName[length] = Serial.read();
- length = (length+1) % 512;
- delay(50);
- }
- hostName[length] = '\0';
- if (length > 0) {
- byte ipAddr[4];
- Serial.print("Resolving ");
- Serial.print(hostName);
- Serial.print("...");
- DNSError err = EthernetDNS.sendDNSQuery(hostName);
- if (DNSSuccess == err) {
- do {
- err = EthernetDNS.pollDNSReply(ipAddr);
- if (DNSTryLater == err) {
- delay(20);
- Serial.print("*");
- }
- } while (DNSTryLater == err);
- }
- Serial.println();
- if (DNSSuccess == err) {
- Serial.print("The IP address is ");
- Serial.print(ip_to_str(ipAddr));
- Serial.println(".");
- } else if (DNSTimedOut == err) {
- Serial.println("Timed out.");
- } else if (DNSNotFound == err) {
- Serial.println("Does not exist.");
- } else {
- Serial.print("Failed with error code ");
- Serial.print((int)err, DEC);
- Serial.println(".");
- }
- }
- }
- prevState = state;
- */
- } // END FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment