brokensidewalkfarm

2011-11-24: Draft Wicking Bed & subsequent library

Nov 24th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.41 KB | None | 0 0
  1. /**
  2. BrokenSidewalkFarm.com  -   Wicking Bed Garden Controller
  3. Licensed under Creative Commons NonCommercial Share-Alike with Attribution (BY-NC-SA)
  4. Louis (Thad) T. Getterman IV, Broken Sidewalk Farm - http://BrokenSidewalkFarm.com/
  5.  
  6. @TODO
  7. * Add Caching to SD Card and then dumps from there
  8. * Add Remote Firmware Upgrades?! HTF DO YOU DO THAT?
  9. */
  10.  
  11. #if defined(ARDUINO) && ARDUINO > 18
  12. #include <SPI.h>
  13. #endif
  14.  
  15. #include <Ethernet.h>
  16. #include <EthernetDHCP.h>
  17. #include <EthernetDNS.h>
  18. #include <EthernetBonjour.h>
  19. #include <Utility.h> // foreach on array (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245256501)
  20.  
  21. const byte NUMBER_OF_PINS = 3;
  22. byte ledPin[NUMBER_OF_PINS] = {10,11,12};
  23.  
  24. #include <string.h>
  25. #undef int() // needed by arduino 0011 to allow use of stdio
  26. #include <stdio.h> // for function sprintf
  27.  
  28. #include <utility/w5100.h> // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287775341
  29.  
  30. #define SHARE_FEED_ID              1872     // the Pachube feed ID that shares your sensor data
  31. #define CONTROL_FEED_ID            1260     // feed ID of the Pachube controller feed
  32. #define UPDATE_INTERVAL            10000    // if the connection is good wait 10 seconds before updating again - should not be less than 5
  33. #define RESET_INTERVAL             10000    // if connection fails/resets wait 10 seconds before trying again - should not be less than 5
  34. #define PACHUBE_API_KEY            "ENTER_YOUR_API_KEY_HERE"    // fill in your API key
  35.  
  36. const char* bonjour_hostname    =   "WB1";
  37.  
  38. // I2C BUS ADDRESS WITH 3 PINS - SEE BACKPLANE ARDUINO EXPERIMENT!
  39.  
  40. String dnaMfgr  =   "Broken Sidewalk Farm";
  41. String dnaModel =   "MS2WB";
  42. int dnaVer      =   "201111210140"; // Time Issued
  43. String dnaRmrks =   "Initial Prototype";
  44.  
  45. byte mac[]  =           {   // MAC ADDRESS - make sure this is unique on your network
  46.                             0xCC,
  47.                             0xAC,
  48.                             0xBE,
  49.                             0xEF,
  50.                             0xFE,
  51.                             0x91
  52.                         };
  53.  
  54. // this is from DHCP
  55. const char* ip_to_str(const uint8_t*);
  56.  
  57. // REPLACE WITH DNS CALL INSTEAD
  58. String remoteServer     =   "redqueen.brokensidewalkfarm.com";
  59. byte remoteServerIp[]   =   {   // RED QUEEN's IP ADDRESS
  60.                                 192,
  61.                                 168,
  62.                                 1,
  63.                                 21
  64.                             };
  65.  
  66. // Initialize the Ethernet client library
  67. // with the IP address and port of the server
  68. // that you want to connect to (port 80 is default for HTTP):
  69. Client client(remoteServerIp, 80);
  70.  
  71. // *************************************************************************************************************************** VARIABLE DECLARATION FOR STATUS AND VALUES OF PINS ON BOARD
  72.  
  73. int state_ana[6];           //  values of analog pins: 0-1023
  74. volatile int state_dig[14]; //  values of digital pins: LOW or HIGH
  75. int state_pwm[6];           //  values of PWM pins: 0-255
  76.  
  77. int pins_ana[]              =   {   // array to hold analog pin numbers (0-1023)
  78.                                     0,
  79.                                     1,
  80.                                     2,
  81.                                     3,
  82.                                     4,
  83.                                     5,
  84.                                 };
  85. volatile int pins_dig[]             =   {   // array to hold digital pin numbers
  86.                                     0,
  87.                                     1,
  88.                                     2,
  89.                                     4,
  90.                                     7,
  91.                                     8,
  92.                                     12,
  93.                                     13,
  94.                                     // PWM PINS GO BELOW, AND THIS IS SET IN CASE THEY'RE SET AS THAT.
  95.                                     3,
  96.                                     5,
  97.                                     6,
  98.                                     9,
  99.                                     10,
  100.                                     11
  101.                                 };
  102. int pins_pwm[]              =   {   // array to hold PWM pin numbers
  103.                                     3,
  104.                                     5,
  105.                                     6,
  106.                                     9,
  107.                                     10,
  108.                                     11
  109.                                 };
  110.  
  111. // analaog => integer 0-1023
  112. // digital => high or low | input or output
  113. // pwm => high or low, or 0-255
  114.  
  115. // assign pin names for analog pins, digital pins, pwm pins
  116. String pinNames[3][14];
  117. int pinVals[3][14];
  118. // initialize pinNames and pinVals
  119.  
  120. /**
  121. 0 = Analog
  122. 1 = Digital
  123. 2 = PWM (Digital)
  124. */
  125.  
  126. // / ************************************************************************************************************************* VARIABLE DECLARATION FOR STATUS AND VALUES OF PINS ON BOARD
  127.  
  128. /*
  129. PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
  130. A4 = SDA
  131. A5 = SCL
  132. */
  133.  
  134. // MAKE AN ARRAY OF NUMBERS THAT ARE ANALOG OR DIGITAL
  135. // ASSIGN NAMES TO THOSE CHANNELS
  136. // USE FUNCTION WITH NAME AND VALUE TO ACCESS
  137.  
  138. // INITIALIZE CONSTANTS
  139. const int delaySerialRead       =   0;  // measure in seconds
  140.  
  141. // Define the function pointer type.
  142. // The void pointer can be casted to any type of value.
  143. typedef void(*fctPtr)(void *);
  144.  
  145. // Declare the functions.
  146. void fA(void *arg);
  147. void fB(void *arg);
  148. void fC(void *arg);
  149.  
  150. // Declare an array of functions pointers.
  151. fctPtr f[3] = {fA,fB,fC}; // Array of function name (the pointer to that name).
  152.  
  153. // The functions.
  154. void fA(void *arg) {
  155.  
  156.     // Cast as a pointer to an int and assign it's value to r.
  157.     int r = *(int*)arg;
  158.  
  159.     Serial.print("fA:");
  160.     Serial.println(r);
  161. }
  162. void fB(void *arg) {
  163.  
  164.     // Cast as a pointer to a string of chars.
  165.     char * str = (char*)arg;
  166.  
  167.     Serial.print("fB:");
  168.     Serial.println(str);
  169. }
  170. void fC(void *arg) {
  171.     Serial.println("fC:Called with no parameter.");
  172. }
  173.  
  174. /*
  175.     int a = 123;
  176.     (*f[0])(&a); // Call the first function of the array of functions.
  177.  
  178.     char b[] = "This is a test!";
  179.     (*f[1])(&b);
  180.  
  181.     // Calling a function with no parameter.
  182.     (*f[2])(NULL);
  183.     // This call also work : (*f[2])(0).
  184. */
  185.  
  186. void debug(char msg[100], char nl='n') {
  187.     Serial.println(msg);
  188. } // END FUNCTION: DEBUG()
  189.  
  190. void pinAssignA(String name, int pin=-1) {
  191.    
  192. } // END FUNCTION
  193.  
  194. void pinAssignD(String name, int dir, int pin=-1) {
  195.  
  196. } // END FUNCTION
  197.  
  198. void pinBlockA(int block[]) {
  199. } // END FUNCTION
  200.  
  201. void pinBlockD(int block[]) {
  202. } // END FUNCTION
  203.  
  204. void pinInit(String &names, String &vals) {
  205.     int count   =   0;
  206.     int count2  =   0;
  207.  
  208.     while (count < 3) {
  209.         count2  =   0;
  210.         while (count2 < 14) {
  211. //          names[count][count2]    =   "";
  212. //          vals[count][count2]     =   0;
  213.             count2++;
  214.         } // END WHILE LOOP
  215.         count++;
  216.     } // END WHILE LOOP
  217.  
  218. } // END FUNCTION
  219.  
  220. // DNS TO IP FUNCTION GOES HERE TO RETURN IP IN 4 CHUNK ARRAY
  221.  
  222. /**
  223. Responsible for updating the lights to their present value
  224. */
  225. void lightAngel() {
  226. // check registry, use updatestatus, checkstatus
  227. // 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
  228. /*
  229. LED STATUS - GREEN
  230. LED STATUS - RED
  231. LED STATUS - SERVER
  232. LED STATUS - TRANSMIT
  233. LED STATUS - RECEIVE
  234. */
  235. } // END FUNCTION
  236.  
  237. /**
  238. */
  239. void updateStatus() {
  240. // category
  241. // input device
  242. // priority - number that's used to gauge itself against other competitors across categories and itself
  243. // alert level - info, server, led, ---- RELATED TO PRIORITY?
  244. // status - okay, unknown, warning, critical
  245. // system severity: optional, preferred, critical
  246. } // END FUNCTION
  247.  
  248. void checkStatus() {
  249. // if category great, show all. otherwise specific device of specific category
  250. } // END FUNCTION
  251.  
  252. // set device light
  253. // set light patterns based on status for categories, as well as individual levels
  254. // set light patterns based on activities
  255. // keep priorities from superseding each other by somehow binding actions to lights? Case in point below:
  256.  
  257. /*
  258. 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
  259. */
  260.  
  261. /*
  262. also need to set current value of whatever light based on light functions. use true/high/255/etc for network TX/RX led indicators
  263. */
  264.  
  265. /*
  266. USE CRON FOR ENTRIES TO CHECK STATUS ON, AND RESPOND TO.
  267. */
  268.  
  269. // timing methods - http://tronixstuff.wordpress.com/2011/06/22/tutorial-arduino-timing-methods-with-millis/
  270.  
  271. // initial setup for the board
  272. void setup() {
  273.     // initialize pinNames & pinValues
  274.     pinInit(&pinNames, &pinVals);
  275.  
  276.     // open up the Ethernet port
  277. //  Ethernet.begin(mac, ip);
  278. //  W5100.setRetransmissionTime(0x07D0);
  279. //  W5100.setRetransmissionCount(3);
  280.  
  281.     // open up serial port rate
  282.     Serial.begin(9600);
  283.  
  284.     int blockAeth[]={};
  285.     int blockDeth[]={10,11,12,13};
  286.  
  287.     // Reserve Digital Pins for Ethernet Shield
  288.     pinBlockD(blockDeth);
  289.  
  290.     pinAssignA("MoistSens1"); // can set pin
  291.     pinAssignA("MoistSens2"); // can set pin
  292.     pinAssignA("MoistSens3"); // can set pin
  293.     pinAssignA("MoistSens4"); // can set pin
  294.     pinAssignA("MoistSens5"); // can set pin
  295.     pinAssignA("MoistSens6"); // can set pin
  296.  
  297.     pinAssignD("LedStatusG",    OUTPUT, 3); // pwm
  298.     pinAssignD("LedStatusR",    OUTPUT, 5); // pwm
  299.     pinAssignD("LedStatusServ", OUTPUT, 6); // pwm - leaves 9pin as only pwm left due to ethernet sucking down 2
  300.     pinAssignD("LedStatusTx",   OUTPUT, 2); // dig - shared
  301.     pinAssignD("LedStatusRx",   OUTPUT, 2); // dig - shared
  302.     // unassigned pwm is 3 - the assignment program will try and save this off until the very last
  303.  
  304.     pinAssignD("RelayPump1",    OUTPUT); //12
  305.     pinAssignD("RelayPump2",    OUTPUT); //8
  306.     pinAssignD("RelayPump3",    OUTPUT); //7
  307.     pinAssignD("RelayPump4",    OUTPUT); //4
  308.     pinAssignD("RelayPump5",    OUTPUT); //2
  309.     pinAssignD("RelayPump6",    OUTPUT); //3
  310. //////////
  311.  
  312. //pinMode(12, OUTPUT);
  313. //pinMode(13, OUTPUT);
  314. //digitalWrite(12,HIGH);
  315. //digitalWrite(13,LOW);
  316.  
  317. // give us time to catch a breather
  318.     delay(1000);
  319. } // END FUNCTION: SETUP()
  320.  
  321. // SETUP VARIABLES FOR OUR LOOP
  322. unsigned long currentMillis = 0;    // know our current time updated by millis() (redundant, yes, I know... Call me superstitiuos or superstupid, your pick.)
  323. int currentCounts[] = {0,0,0};      // dimensions of time based off of dimensions of 10
  324. int priorSecs    =  0;          // prior second, used to detect when a second has elapsed --- TRASH THIS?
  325. int currentSecs  =  0;          // current second --- TRASH THIS?
  326. bool triggerServerCheckIn  =  true; // boolean trigger that alternates for checking into the server
  327. bool initLoop = true;
  328.  
  329. void loop () {
  330. /*
  331.   static DhcpState prevState = DhcpStateNone;
  332.   static unsigned long prevTime = 0;
  333.  
  334.   DhcpState state = EthernetDHCP.poll();
  335.  
  336.   if (prevState != state) {
  337.     Serial.println();
  338.  
  339.     switch (state) {
  340.       case DhcpStateDiscovering:
  341.         Serial.print("Discovering servers.");
  342.         break;
  343.       case DhcpStateRequesting:
  344.         Serial.print("Requesting lease.");
  345.         break;
  346.       case DhcpStateRenewing:
  347.         Serial.print("Renewing lease.");
  348.         break;
  349.       case DhcpStateLeased: {
  350.         Serial.println("Obtained lease!");
  351.  
  352.         const byte* ipAddr = EthernetDHCP.ipAddress();
  353.         const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  354.         const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
  355.  
  356.         Serial.print("My IP address is ");
  357.         Serial.println(ip_to_str(ipAddr));
  358.  
  359.         Serial.print("Gateway IP address is ");
  360.         Serial.println(ip_to_str(gatewayAddr));
  361.  
  362.         Serial.print("DNS IP address is ");
  363.         Serial.println(ip_to_str(dnsAddr));
  364.  
  365.         Serial.println('\n');
  366.        
  367.         Serial.println("DNS server set via DHCP. Send a host name via serial to resolve it.\n");
  368.         EthernetDNS.setDNSServer(dnsAddr);
  369.        
  370.         EthernetBonjour.begin(bonjour_hostname);
  371.        
  372.         Serial.print("Also, the board is resolvable via Bonjour/ZeroConf as ");
  373.         Serial.print(bonjour_hostname);
  374.         Serial.println(".local\n");
  375.        
  376.         break;
  377.       }
  378.     }
  379.   } else if (state != DhcpStateLeased && millis() - prevTime > 300) {
  380.      prevTime = millis();
  381.      Serial.print('!');
  382.   } else if (state == DhcpStateLeased) {
  383.     char hostName[512];
  384.     int length = 0;
  385.    
  386.     EthernetBonjour.run();
  387.    
  388.     while (Serial.available()) {
  389.       hostName[length] = Serial.read();
  390.       length = (length+1) % 512;
  391.       delay(50);
  392.     }
  393.    
  394.     hostName[length] = '\0';
  395.    
  396.     if (length > 0) {
  397.      
  398.       byte ipAddr[4];
  399.      
  400.       Serial.print("Resolving ");
  401.       Serial.print(hostName);
  402.       Serial.print("...");
  403.  
  404.       DNSError err = EthernetDNS.sendDNSQuery(hostName);
  405.  
  406.       if (DNSSuccess == err) {
  407.         do {
  408.           err = EthernetDNS.pollDNSReply(ipAddr);
  409.            
  410.           if (DNSTryLater == err) {
  411.             delay(20);
  412.             Serial.print("*");
  413.           }
  414.         } while (DNSTryLater == err);
  415.       }
  416.  
  417.       Serial.println();
  418.  
  419.       if (DNSSuccess == err) {
  420.         Serial.print("The IP address is ");
  421.         Serial.print(ip_to_str(ipAddr));
  422.         Serial.println(".");
  423.       } else if (DNSTimedOut == err) {
  424.         Serial.println("Timed out.");
  425.       } else if (DNSNotFound == err) {
  426.         Serial.println("Does not exist.");
  427.       } else {
  428.         Serial.print("Failed with error code ");
  429.         Serial.print((int)err, DEC);
  430.         Serial.println(".");
  431.       }
  432.     }  
  433.   }
  434.  
  435.   prevState = state;
  436. */
  437. } // END FUNCTION
  438.  
  439.  
  440.  
Advertisement
Add Comment
Please, Sign In to add comment