Advertisement
mudem

Untitled

Feb 9th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.82 KB | None | 0 0
  1. /*
  2.  
  3.  Open Kontrol Gateway (OKG) Example  
  4.  Receive data from an emonTx via RFM12B wireless then post online to emoncms using Wiznet 5200 Ethernet Module
  5.  
  6.  Part of the openenergymonitor.org project
  7.  Licence: GNU GPL V3
  8.  Authors: Trystan Lea and Glyn Hudson
  9.  Created: 13/08/12
  10.  
  11.  Use the following libraries:  
  12.  https://github.com/openenergymonitor/Ethernet
  13.  https://github.com/openenergymonitor/jeelib
  14.  
  15.  For full step-by-step instructions See: http://wiki.openenergymonitor.org/index.php?title=Open_Kontrol_Gateway
  16.  
  17.  Credits:
  18.  Based on Arduino DNS and DHCP-based Web client by David A. Mellis
  19.  modified 12 April 2011by Tom Igoe, based on work by Adrian McEwen
  20.  Uses Jeelabs.org JeeLib library for RFM12B
  21.  
  22.  Example will also work with Arduino Ethernet, Arduino + newer Ethernet shields with addition of RFM12B. Bug in older Ethernet shields stops RFM12B and Wiznet being using together
  23.  
  24.  */
  25.  
  26. #include <SPI.h>
  27. #include <Ethernet.h>
  28. #include <JeeLib.h>
  29. #include <avr/wdt.h>
  30.  
  31. //------------------------------------------------------------------------------------------------------
  32. // RFM12B Wireless Config
  33. //------------------------------------------------------------------------------------------------------
  34. #define MYNODE 30            // node ID of OKG
  35. #define freq RF12_433MHZ     // frequency - must match RFM12B module and emonTx
  36. #define group 210            // network group - must match emonTx
  37. //------------------------------------------------------------------------------------------------------
  38.  
  39. //------------------------------------------------------------------------------------------------------
  40. // Ethernet Config
  41. //------------------------------------------------------------------------------------------------------
  42. byte mac[] = { 0x00, 0xAB, 0xBB, 0xCC, 0xDE, 0x02 };  // OKG MAC - experiment with different MAC addresses if you have trouble connecting
  43. byte ip[] = { 192, 168, 1, 99 };                       // OKG static IP - only used if DHCP failes
  44.  
  45. // Enter your apiurl here including apikey:
  46. char apiurl[] = "http://emoncms.org/api/post.json?apikey=08c1dc2cab544b037bdac6afb7dccd1f";
  47. char timeurl[] = "http://emoncms.org/time/local.json?apikey=08c1dc2cab544b037bdac6afb7dccd1f";   //usually same API key twice
  48. // For posting to emoncms server with host name, (DNS lookup) comment out if using static IP address below
  49. // emoncms.org is the public emoncms server. Emoncms can also be downloaded and run on any server.
  50. char server[] = "emoncms.org";    
  51.  
  52. //IPAddress server(xxx,xxx,xxx,xxx);                  // emoncms server IP for posting to server without a host name, can be used for posting to local emoncms server
  53.  
  54. EthernetClient client;
  55.  
  56. const int WizResetPin = 7;                             // wired to the Wiznet reset line
  57. //------------------------------------------------------------------------------------------------------
  58.  
  59. //------------------------------------------------------------------------------------------------------
  60. // Open Kontrol Gateway Config
  61. //------------------------------------------------------------------------------------------------------
  62. const int LEDpin=12;         //front status LED on OKG
  63. //------------------------------------------------------------------------------------------------------
  64. const int SD_DISABLE_PIN=4; // Set high to disable
  65. const int ETHER_DISABLE_PIN=10; // Set high to disable
  66. const int RF12_DISABLE_PIN=11; // Set high to disable
  67.  
  68. //------------------------------------------------------------------------------------------------------
  69. // The PacketBuffer class is used to generate the json string that is send via ethernet - JeeLabs
  70. //------------------------------------------------------------------------------------------------------
  71. class PacketBuffer :
  72. public Print {
  73. public:
  74.   PacketBuffer () :
  75.   fill (0) {
  76.   }
  77.   const char* buffer() {
  78.     return buf;
  79.   }
  80.   byte length() {
  81.     return fill;
  82.   }
  83.   void reset()
  84.   {
  85.     memset(buf,NULL,sizeof(buf));
  86.     fill = 0;
  87.   }
  88.   virtual size_t write (uint8_t ch)
  89.   {
  90.     if (fill < sizeof buf) buf[fill++] = ch;
  91.   }
  92.   byte fill;
  93.   char buf[150];
  94. private:
  95. };
  96. PacketBuffer str;
  97. //--------------------------------------------------------------------------------------------------------
  98.  
  99. int data_ready, rf_error;
  100. unsigned long last_rf, time60s;
  101. boolean lastConnected = false;
  102.  
  103. char line_buf[50];                        // Used to store line of http reply header
  104.  
  105. //------------------------------------------------------------------------------------------------------
  106. // SETUP
  107. //------------------------------------------------------------------------------------------------------
  108. void setup() {
  109.  
  110.   Serial.begin(9600);
  111.   Serial.println("openenergymonitor.org RFM12B > OKG > Wiznet, > emoncms MULTI-NODE");
  112.  
  113.   pinMode(LEDpin, OUTPUT);
  114.  
  115.   pinMode(SD_DISABLE_PIN, OUTPUT);
  116.   digitalWrite(SD_DISABLE_PIN,HIGH); //Disable SD
  117.  
  118.   digitalWrite(LEDpin,HIGH);
  119.  
  120.   rf12_set_cs(11);
  121.   rf12_initialize(MYNODE, freq, group);
  122.   last_rf = millis()-40000;                                       // setting lastRF back 40s is useful as it forces the ethernet code to run straight away
  123.  
  124.   if (Ethernet.begin(mac) == 0) {
  125.     Serial.println("Failed to configure Ethernet using DHCP");
  126.     Ethernet.begin(mac, ip);                                      //configure manually
  127.   }
  128.  
  129.   // print your local IP address:
  130.   Serial.print("Local IP address: ");
  131.   for (byte thisByte = 0; thisByte < 4; thisByte++) {
  132.     // print the value of each byte of the IP address:
  133.     Serial.print(Ethernet.localIP()[thisByte], DEC);
  134.     Serial.print(".");
  135.   }
  136.   Serial.println();
  137.  
  138.   // print RFM12B settings
  139.   Serial.print("Node: ");
  140.   Serial.print(MYNODE);
  141.   Serial.print(" Freq: ");
  142.   if (freq == RF12_433MHZ) Serial.print("433Mhz");
  143.   if (freq == RF12_868MHZ) Serial.print("868Mhz");
  144.   if (freq == RF12_915MHZ) Serial.print("915Mhz");
  145.   Serial.print(" Network: ");
  146.   Serial.println(group);
  147.  
  148.   delay(200);
  149.   digitalWrite(LEDpin,LOW); //turn of OKG status LED to indicate setup success
  150.  
  151.   wdt_enable(WDTO_8S);                //enable an 8's reset watchdog
  152. }
  153. //------------------------------------------------------------------------------------------------------
  154.  
  155.  
  156. //------------------------------------------------------------------------------------------------------
  157. // LOOP
  158. //------------------------------------------------------------------------------------------------------
  159. void loop()
  160. {
  161.  
  162.   //-----------------------------------------------------------------------------------------------------------------
  163.   // 1) Receive date from emonTx via RFM12B
  164.   //-----------------------------------------------------------------------------------------------------------------
  165.  
  166.   digitalWrite(LEDpin,HIGH);
  167.   // turn off the W5100 chip!      
  168.  
  169. //  digitalWrite(ETHER_DISABLE_PIN, HIGH);  
  170. //  digitalWrite(RF12_DISABLE_PIN, LOW);  
  171.  
  172.   if (rf12_recvDone()){    
  173.           Serial.println("Receive done");
  174.         digitalWrite(LEDpin,LOW);
  175.         delay(100);
  176.         digitalWrite(LEDpin,HIGH);
  177.         delay(100);
  178.         digitalWrite(LEDpin,LOW);
  179.        
  180.     if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
  181.     {
  182.  
  183.       int node_id = (rf12_hdr & 0x1F);
  184.       byte n = rf12_len;
  185.  
  186.       str.reset();
  187.       str.print("&node=");  
  188.       str.print(node_id);
  189.       str.print("&csv=");
  190.       for (byte i=0; i<n; i+=2)
  191.       {
  192.         int num = ((unsigned char)rf12_data[i+1] << 8 | (unsigned char)rf12_data[i]);
  193.         if (i) str.print(",");
  194.         str.print(num);
  195.       }
  196.  
  197.       str.print("\0");  //  End of json string
  198.  
  199.       data_ready = 1;
  200.       last_rf = millis();
  201.       rf_error=0;
  202.      
  203.  
  204.       for (int i = 0; i<3; i++)
  205.       {
  206.         digitalWrite(LEDpin,HIGH);
  207.         delay(100);
  208.         digitalWrite(LEDpin,LOW);
  209.         delay(100);
  210.       }
  211.     }
  212.        Serial.println("Receive complete");
  213.   }
  214.  
  215.   digitalWrite(LEDpin,LOW);
  216.  
  217.   //-----------------------------------------------------------------------------------------------------------------
  218.   // 2) If no data is recieved from rf12 emoncms is updated every 30s with RFfail = 1 indicator for debugging
  219.   //-----------------------------------------------------------------------------------------------------------------
  220.   if ((millis()-last_rf)>90000)
  221.   {
  222.     last_rf = millis();                                                 // reset lastRF timer
  223.     str.reset();                                                        // reset json string
  224.     str.print("&json={rf_fail:1}\0");                                   // No RF received in 30 seconds so send failure
  225.     data_ready = 1;                                                     // Ok, data is ready
  226.     rf_error=1;
  227.   }
  228.  
  229.   //-----------------------------------------------------------------------------------------------------------------
  230.   // 3) Post Data
  231.   //-----------------------------------------------------------------------------------------------------------------
  232.  
  233.   // turn on the W5100 chip!        
  234. //  digitalWrite(RF12_DISABLE_PIN, HIGH);    
  235. //  digitalWrite(ETHER_DISABLE_PIN, LOW);  
  236.  
  237.  
  238.   if (client.available())
  239.   {
  240.     Serial.println("client.available()");
  241.     memset(line_buf,NULL,sizeof(line_buf));
  242.     int pos = 0;
  243.  
  244.     while (client.available()) {
  245.       char c = client.read();
  246.       line_buf[pos] = c;
  247.       pos++;
  248.     }  
  249.  
  250.     if (strcmp(line_buf,"ok")==0)
  251.     {
  252.       Serial.println("OK recieved");
  253.     }
  254.     Serial.println(line_buf);
  255.   }
  256.  
  257.   if (!client.connected() && lastConnected) {
  258.     Serial.println();
  259.     Serial.println("disconnecting.");
  260.     client.stop();
  261.   }
  262.  
  263.   if (!client.connected() && data_ready) {
  264.     Serial.println("Connecting");
  265.     if (client.connect(server, 80)) {
  266.      
  267.       Serial.println();
  268.       Serial.print("Sending: ");
  269.       Serial.println(str.buf);
  270.       client.print("GET ");
  271.       client.print(apiurl);
  272.       client.print(str.buf);
  273.       client.println();
  274.  
  275.       delay(300);
  276.  
  277.       data_ready=0;
  278.     }
  279.  
  280.     else {
  281.       Serial.println("Cant connect to send data");
  282.       delay(500);
  283.       client.stop();
  284.     }
  285.   }
  286.  
  287.   if (!client.connected() && ((millis()-time60s)>600000))
  288.   {
  289.     time60s = millis();                                                 // reset lastRF timer
  290.  
  291.     if (client.connect(server, 80)) {
  292.       Serial.println();
  293.       client.print("GET ");
  294.       client.print(timeurl);
  295.       client.println();
  296.       Serial.println("Sent time request");
  297.     }
  298.     else {
  299.       Serial.println("Cant connect to req time");
  300.       delay(500);
  301.       client.stop();
  302.     }
  303.  
  304.   }
  305.  
  306.  
  307.   lastConnected = client.connected();
  308.  
  309.   wdt_reset();                                                   // Reset the watchdog
  310.  
  311. }//end loop
  312.  
  313. //------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement