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>
- #include <printf.h>
- #include <RF24Network.h>
- RF24 radio(6, 7);
- boolean reading = 0;
- String myStr, dataString, red, green, blue;
- int nodeOne, nodeTwo, nodeThree, nodeFour, power, rgbColor, fRed, fGreen, fBlue, dataInt;
- // Network uses that radio
- RF24Network network(radio);
- // Address of our node
- const uint16_t this_node = 1;
- // Address of the other node
- const uint16_t other_node = 0;
- // How often to send 'hello world to the other unit
- const unsigned long interval = 2000; //ms
- // When did we last send?
- unsigned long last_sent;
- byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
- IPAddress ip(192,168,30,80);
- EthernetServer server(80);
- void setup(void) {
- Serial.begin(9600);
- printf_begin();
- radio.begin();
- SPI.begin();
- network.begin(/*channel*/ 90, /*node address*/ this_node);
- Ethernet.begin(mac, ip);
- server.begin();
- //radio.printDetails();
- }
- //=============================================================================================
- void loop() {
- checkForClient();
- }
- //=============================================================================================
- void checkForClient() {
- EthernetClient client = server.available();
- if (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);
- delay(100); //give browser time
- client.stop(); //close the connection
- Serial.println("Sending Data Now:");
- broadcastData(dataString);
- }
- delay(2000);
- }
- //======================================================================================================
- void parseHttp (String str) {
- //pull out node1
- String strNodeOne = str.substring(10,11);
- Serial.print("STRNodeOne: ");
- Serial.println(strNodeOne);
- //pull out node2
- String strNodeTwo = str.substring(17,18);
- Serial.print("STRNodeTwo: ");
- Serial.println(strNodeTwo);
- //pull out node3
- String strNodeThree = str.substring(26,27);
- Serial.print("STRNodeThree: ");
- Serial.println(strNodeThree);
- //pull out node4
- String strNodeFour = str.substring(34,35);
- Serial.print("STRNodeFour: ");
- Serial.println(strNodeFour);
- //pull out power
- String strPower = str.substring(42,43);
- Serial.print("STRPower: ");
- Serial.println(strPower);
- //finally pull out color. it will always be xxxxxxxxx rgb
- String strColor = str.substring(50, 59);
- Serial.print("STRcolor:");
- Serial.println(strColor);
- red = strColor.substring(0,3);
- blue = strColor.substring(3, 6);
- green = strColor.substring(6,9);
- dataString = strNodeOne + strNodeTwo + strNodeThree + strNodeFour + strPower + strColor;
- }
- void broadcastData(String sendString){
- Serial.print("Here is sendString: ");
- Serial.println(sendString);
- // Pump the network regularly
- network.update();
- unsigned long now = millis();
- if ( now - last_sent > interval )
- {
- last_sent = now;
- printf("Sending...\r\n");
- size_t len = sendString.length();
- char buf[len+1]; // +1 for the trailing zero terminator
- sendString.toCharArray(buf, len);
- RF24NetworkHeader header(other_node);
- bool ok = network.write(header,buf,strlen(buf));
- if (ok)
- printf("\tok.\r\n");
- else
- {
- printf("\tfailed.\r\n");
- delay(250); // extra delay on fail to keep light on longer
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement