Advertisement
Guest User

Untitled

a guest
Jun 7th, 2015
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.61 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.  
  13. RF24 radio(6, 7);
  14. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  15.  
  16. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  17. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
  18. //role_e role = role_pong_back;
  19. role_e role = role_ping_out; //sets to perm transmit
  20.  
  21. boolean reading = 0;
  22. String myStr;
  23. int redVal, greenVal, blueVal;
  24.  
  25. byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
  26. IPAddress ip(192,168,30,80);
  27. EthernetServer server(80);
  28.  
  29. void setup() {
  30.  
  31.   radio.begin();
  32.   radio.setRetries(15, 15);
  33.   radio.setDataRate(RF24_250KBPS);
  34.   radio.setChannel(0x4d);
  35.  
  36.   //roll is always ping out. This is the base station
  37.   radio.openWritingPipe(pipes[0]);
  38.   radio.openReadingPipe(1,pipes[1]);
  39.  
  40.   radio.printDetails();
  41.  
  42.   Ethernet.begin(mac, ip);
  43.   server.begin();
  44.   Serial.begin(9600);
  45.  
  46. }
  47.  
  48. //=============================================================================================
  49.  
  50. void loop() {
  51.  
  52.  checkForClient();
  53.  
  54. }
  55.  
  56. //=============================================================================================
  57.  
  58. void checkForClient() {
  59.  
  60.  EthernetClient client = server.available();
  61.  
  62.  if (client) {
  63.   Serial.println("Got to the client");
  64.    //http request  ends  with a blank line
  65.    boolean currentLineIsBlank = true;
  66.    boolean sentHeader = false;
  67.    myStr = "";
  68.    while (client.connected()) {
  69.      if (client.available()) {
  70.        
  71.        char c = client.read();
  72.        
  73.        if(reading && c == ' ') reading = false;
  74.        if(c == '?'); reading = true; //found the ?, begin reading the info
  75.        
  76.        if(reading){
  77.          Serial.print(c);
  78.          if (c!='?') {   //if c is not teh ? the keep reading
  79.            myStr += c;   //and add this character to the string
  80.          }
  81.          
  82.        }
  83.        
  84.        if (c == '\n' && currentLineIsBlank) break;
  85.        
  86.        if (c == '\n')  {
  87.          currentLineIsBlank = true;
  88.        } else if (c != '\r') {
  89.          currentLineIsBlank = false;
  90.        }
  91.      }
  92.    }
  93.  
  94.    parseHttp (myStr);
  95.    
  96.    //Serial.print("Value red is:  ");
  97.    //Serial.println(redVal);
  98.    //Serial.print("Value green is:  ");
  99.    //Serial.println(greenVal);
  100.    //Serial.print("Value blue is:  ");
  101.    //Serial.println(blueVal);
  102.    delay(100); //give browser time
  103.    
  104.    client.stop(); //close the connection
  105.    
  106.    Serial.println("Sending Data Now:");
  107.    broadcastData(redVal);
  108.  }
  109.  
  110.  delay(2000);
  111. }
  112.  
  113. //======================================================================================================
  114.  
  115. void parseHttp (String str) {
  116.  
  117.   //do the redVal
  118.   int startIndex = str.indexOf("r");
  119.   int endIndex = str.indexOf("g");
  120.   String redStr = str.substring(startIndex + 2, endIndex - 1);
  121.   char tempRed[4];
  122.   redStr.toCharArray(tempRed, sizeof(tempRed));
  123.   redVal = atoi(tempRed);
  124.  
  125.   //do the greenVal
  126.   startIndex = str.indexOf("g");
  127.   endIndex = str.indexOf("b");
  128.   String greenStr = str.substring(startIndex  + 2, endIndex - 1);
  129.   char tempGreen[4];
  130.   greenStr.toCharArray(tempGreen, sizeof(tempGreen));
  131.   greenVal = atoi(tempGreen);
  132.  
  133.   //do the blueVal
  134.   startIndex = str.indexOf("b");
  135.   endIndex = str.indexOf("e");
  136.   String blueStr = str.substring(startIndex + 2, endIndex - 1);
  137.   char tempBlue[4];
  138.   blueStr.toCharArray(tempBlue, sizeof(tempBlue));
  139.   blueVal = atoi(tempBlue);
  140.  
  141. }
  142.  
  143. void broadcastData (int tempVal) {
  144.  
  145.   //Serial.print("Sending tempVal:  ");
  146.   //Serial.println(tempVal);
  147.  
  148.  
  149.  
  150.   unsigned long time = millis();
  151.   bool ok = radio.write( &time, sizeof(unsigned long) );
  152.    
  153.     if (ok) {
  154.       Serial.println("ok...");
  155.     } else {
  156.       Serial.println("failed.\n\r");
  157.     }
  158.    
  159.     // Now, continue listening
  160.     radio.startListening();
  161.  
  162.     // Wait here until we get a response, or timeout (250ms)
  163.     unsigned long started_waiting_at = millis();
  164.     bool timeout = false;
  165.     while ( ! radio.available() && ! timeout )
  166.       if (millis() - started_waiting_at > 200 )
  167.         timeout = true;
  168.  
  169.     // Describe the results
  170.     if ( timeout )
  171.     {
  172.       Serial.println("Failed, response timed out.\n\r");
  173.     }
  174.     else
  175.     {
  176.       // Grab the response, compare, and send to debugging spew
  177.       unsigned long got_time;
  178.       radio.read( &got_time, sizeof(unsigned long) );
  179.  
  180.       Serial.println("Got response!");
  181.     }
  182.  
  183.     // Try again 1s later
  184.     radio.stopListening();
  185.     delay(5000);
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement