Advertisement
Guest User

Base Station

a guest
Jun 10th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. #include <DigitalIO.h>
  2. #include <DigitalPin.h>
  3. #include <I2cConstants.h>
  4. #include <PinIO.h>
  5. #include <SoftI2cMaster.h>
  6. #include <SoftSPI.h>
  7.  
  8. #include <SPI.h>
  9. #include <Ethernet.h>
  10. #include <nRF24L01.h>
  11. #include <RF24.h>
  12. #include <printf.h>
  13. #include <RF24Network.h>
  14.  
  15. RF24 radio(6, 7);
  16.  
  17. boolean reading = 0;
  18. String myStr, dataString, red, green, blue;
  19. int nodeOne, nodeTwo, nodeThree, nodeFour, power, rgbColor, fRed, fGreen, fBlue, dataInt;
  20.  
  21. // Network uses that radio
  22. RF24Network network(radio);
  23.  
  24. // Address of our node
  25. const uint16_t this_node = 1;
  26.  
  27. // Address of the other node
  28. const uint16_t other_node = 0;
  29.  
  30. // How often to send 'hello world to the other unit
  31. const unsigned long interval = 2000; //ms
  32.  
  33. // When did we last send?
  34. unsigned long last_sent;
  35.  
  36. byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
  37. IPAddress ip(192,168,30,80);
  38. EthernetServer server(80);
  39.  
  40. void setup(void) {
  41.  
  42.   Serial.begin(9600);
  43.   printf_begin();
  44.   radio.begin();
  45.   SPI.begin();
  46.   network.begin(/*channel*/ 90, /*node address*/ this_node);
  47.  
  48.   Ethernet.begin(mac, ip);
  49.   server.begin();
  50.  
  51.   //radio.printDetails();
  52.  
  53. }
  54.  
  55. //=============================================================================================
  56.  
  57. void loop() {
  58.  
  59.  checkForClient();
  60.  
  61. }
  62.  
  63. //=============================================================================================
  64.  
  65. void checkForClient() {
  66.  
  67.  EthernetClient client = server.available();
  68.  
  69.  if (client) {
  70.  
  71.    //http request  ends  with a blank line
  72.    boolean currentLineIsBlank = true;
  73.    boolean sentHeader = false;
  74.    myStr = "";
  75.    while (client.connected()) {
  76.      if (client.available()) {
  77.        
  78.        char c = client.read();
  79.        
  80.        if(reading && c == ' ') reading = false;
  81.        if(c == '?'); reading = true; //found the ?, begin reading the info
  82.        
  83.        if(reading){
  84.          Serial.print(c);
  85.          if (c!='?') {   //if c is not teh ? the keep reading
  86.            myStr += c;   //and add this character to the string
  87.          }
  88.          
  89.        }
  90.        
  91.        if (c == '\n' && currentLineIsBlank) break;
  92.        
  93.        if (c == '\n')  {
  94.          currentLineIsBlank = true;
  95.        } else if (c != '\r') {
  96.          currentLineIsBlank = false;
  97.        }
  98.      }
  99.    }
  100.  
  101.    parseHttp (myStr);
  102.    
  103.    delay(100); //give browser time
  104.  
  105.    client.stop(); //close the connection
  106.    
  107.    Serial.println("Sending Data Now:");
  108.    broadcastData(dataString);
  109.  }
  110.  
  111.  delay(2000);
  112. }
  113.  
  114. //======================================================================================================
  115.  
  116. void parseHttp (String str) {
  117.  
  118.   //pull out node1
  119.  
  120.   String strNodeOne = str.substring(10,11);
  121.   Serial.print("STRNodeOne:  ");
  122.   Serial.println(strNodeOne);
  123.  
  124.     //pull out node2
  125.   String strNodeTwo = str.substring(17,18);
  126.   Serial.print("STRNodeTwo:  ");
  127.   Serial.println(strNodeTwo);
  128.  
  129.   //pull out node3
  130.   String strNodeThree = str.substring(26,27);
  131.   Serial.print("STRNodeThree:  ");
  132.   Serial.println(strNodeThree);
  133.  
  134.     //pull out node4
  135.   String strNodeFour = str.substring(34,35);
  136.   Serial.print("STRNodeFour:  ");
  137.   Serial.println(strNodeFour);
  138.  
  139.   //pull out power
  140.  
  141.   String strPower = str.substring(42,43);
  142.   Serial.print("STRPower: ");
  143.   Serial.println(strPower);
  144.  
  145.   //finally pull out color.  it will always be xxxxxxxxx rgb
  146.   String strColor = str.substring(50, 59);
  147.   Serial.print("STRcolor:");
  148.   Serial.println(strColor);
  149.  
  150.   red = strColor.substring(0,3);
  151.   blue = strColor.substring(3, 6);
  152.   green = strColor.substring(6,9);
  153.  
  154.  
  155.   dataString =  strNodeOne + strNodeTwo + strNodeThree + strNodeFour + strPower + strColor;
  156.  
  157.  
  158. }
  159. void broadcastData(String sendString){
  160.  
  161.     Serial.print("Here is sendString:  ");
  162.     Serial.println(sendString);
  163.  
  164.    // Pump the network regularly
  165.   network.update();
  166.  
  167.   unsigned long now = millis();
  168.   if ( now - last_sent > interval  )
  169.   {
  170.     last_sent = now;
  171.  
  172.     printf("Sending...\r\n");
  173.     size_t len = sendString.length();
  174.     char buf[len+1]; // +1 for the trailing zero terminator
  175.     sendString.toCharArray(buf, len);
  176.     RF24NetworkHeader header(other_node);
  177.     bool ok = network.write(header,buf,strlen(buf));
  178.     if (ok)
  179.       printf("\tok.\r\n");
  180.     else
  181.     {
  182.       printf("\tfailed.\r\n");
  183.       delay(250); // extra delay on fail to keep light on longer
  184.     }
  185.   }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement