Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DigitalIO.h>
- #include <DigitalPin.h>
- #include <I2cConstants.h>
- #include <PinIO.h>
- #include <SoftI2cMaster.h>
- #include <SoftSPI.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <nRF24L01.h>
- #include <RF24.h>
- RF24 radio(6, 7);
- const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
- typedef enum { role_ping_out = 1, role_pong_back } role_e;
- const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back"};
- //role_e role = role_pong_back;
- role_e role = role_ping_out; //sets to perm transmit
- boolean reading = 0;
- String myStr;
- int redVal, greenVal, blueVal;
- byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
- IPAddress ip(192,168,30,80);
- EthernetServer server(80);
- void setup() {
- radio.begin();
- radio.setRetries(15, 15);
- radio.setDataRate(RF24_250KBPS);
- radio.setChannel(0x4d);
- //roll is always ping out. This is the base station
- radio.openWritingPipe(pipes[0]);
- radio.openReadingPipe(1,pipes[1]);
- radio.printDetails();
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.begin(9600);
- }
- //=============================================================================================
- void loop() {
- checkForClient();
- }
- //=============================================================================================
- void checkForClient() {
- EthernetClient client = server.available();
- if (client) {
- Serial.println("Got to the client");
- //http request ends with a blank line
- boolean currentLineIsBlank = true;
- boolean sentHeader = false;
- myStr = "";
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- if(reading && c == ' ') reading = false;
- if(c == '?'); reading = true; //found the ?, begin reading the info
- if(reading){
- Serial.print(c);
- if (c!='?') { //if c is not teh ? the keep reading
- myStr += c; //and add this character to the string
- }
- }
- if (c == '\n' && currentLineIsBlank) break;
- if (c == '\n') {
- currentLineIsBlank = true;
- } else if (c != '\r') {
- currentLineIsBlank = false;
- }
- }
- }
- parseHttp (myStr);
- //Serial.print("Value red is: ");
- //Serial.println(redVal);
- //Serial.print("Value green is: ");
- //Serial.println(greenVal);
- //Serial.print("Value blue is: ");
- //Serial.println(blueVal);
- delay(100); //give browser time
- client.stop(); //close the connection
- Serial.println("Sending Data Now:");
- broadcastData(redVal);
- }
- delay(2000);
- }
- //======================================================================================================
- void parseHttp (String str) {
- //do the redVal
- int startIndex = str.indexOf("r");
- int endIndex = str.indexOf("g");
- String redStr = str.substring(startIndex + 2, endIndex - 1);
- char tempRed[4];
- redStr.toCharArray(tempRed, sizeof(tempRed));
- redVal = atoi(tempRed);
- //do the greenVal
- startIndex = str.indexOf("g");
- endIndex = str.indexOf("b");
- String greenStr = str.substring(startIndex + 2, endIndex - 1);
- char tempGreen[4];
- greenStr.toCharArray(tempGreen, sizeof(tempGreen));
- greenVal = atoi(tempGreen);
- //do the blueVal
- startIndex = str.indexOf("b");
- endIndex = str.indexOf("e");
- String blueStr = str.substring(startIndex + 2, endIndex - 1);
- char tempBlue[4];
- blueStr.toCharArray(tempBlue, sizeof(tempBlue));
- blueVal = atoi(tempBlue);
- }
- void broadcastData (int tempVal) {
- //Serial.print("Sending tempVal: ");
- //Serial.println(tempVal);
- unsigned long time = millis();
- bool ok = radio.write( &time, sizeof(unsigned long) );
- if (ok) {
- Serial.println("ok...");
- } else {
- Serial.println("failed.\n\r");
- }
- // Now, continue listening
- radio.startListening();
- // Wait here until we get a response, or timeout (250ms)
- unsigned long started_waiting_at = millis();
- bool timeout = false;
- while ( ! radio.available() && ! timeout )
- if (millis() - started_waiting_at > 200 )
- timeout = true;
- // Describe the results
- if ( timeout )
- {
- Serial.println("Failed, response timed out.\n\r");
- }
- else
- {
- // Grab the response, compare, and send to debugging spew
- unsigned long got_time;
- radio.read( &got_time, sizeof(unsigned long) );
- Serial.println("Got response!");
- }
- // Try again 1s later
- radio.stopListening();
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement