Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Created By Matthew aka Elitism 2015
- //Arduino Mega2560 + esp8266 wifi module (connect to router and accept LAN connection String commands)
- #include "ESP8266.h"
- #define SSID "raptor" //Your Router WiFi SSID
- #define PASSWORD "poodsdfdf@@WF@WF@WF<<" //Your Router WiFi Password
- #define PORT 8080 //The Port you want the TCP Server to run on
- ESP8266 wifi(Serial1);
- int r = 255;
- int g = 255;
- int b = 255;
- void setup(void)
- {
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- pinMode(10, OUTPUT);
- analogWrite(8, r);
- analogWrite(9, g);
- analogWrite(10, b);
- Serial.begin(115200);
- Serial.println("Wifi Connecting: " + (String)SSID);
- wifi.setOprToStationSoftAP(); //Set Mode - Connect to Access Point
- if (wifi.joinAP(SSID, PASSWORD)) {
- wifi.enableMUX();
- wifi.startTCPServer(PORT);
- wifi.setTCPServerTimeout(10000);
- Serial.print((String)SSID + " TCP Server Port: " + String(PORT) + "\n");
- Serial.println(wifi.getLocalIP().c_str());
- }
- else {
- Serial.println("Connection Failed");
- }
- }
- void loop(void)
- {
- uint8_t buffer[128] = {
- 0 };
- uint8_t mux_id;
- uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
- String Message;
- if (len > 0) {
- wifi.send(mux_id, buffer, len);//Return message received
- //Get the message out of the buffer
- for(uint32_t i = 0; i < len; i++) {
- Message += (String)(char)buffer[i];
- }
- //Print the message
- Serial.println(Message);
- //Accept commands to change rgb led ;)
- if(Message.indexOf("rgb") >=0)
- {
- //R & B are switched (Using Common Anode RGB LED)
- int r = map(split(Message, ',', 3).toInt(), 0, 255, 255, 0); //Split & Map Third Value
- int g = map(split(Message, ',', 2).toInt(), 0, 255, 255, 0); //Split & Map Second Value
- int b = map(split(Message, ',', 1).toInt(), 0, 255, 255, 0); //Split & Map First Value
- analogWrite(8, r);
- analogWrite(9, g);
- analogWrite(10, b);
- Serial.println("DATA: " + (String)r + "," + (String)g + "," + (String)b + "\r\n");
- }
- //Test & Respond
- if(Message.indexOf("test") >=0)
- {
- uint8_t dcrespond[] = "Received";
- wifi.send(mux_id, dcrespond, sizeof(dcrespond) - 1);//Return message received
- }
- //Disconnect from the TCP Connection
- if(Message.indexOf("disconnect") >=0)
- {
- wifi.releaseTCP(mux_id);
- Serial.println("(Client " + (String)mux_id + ") Disconnected");
- }
- if(Message.indexOf("devs") >=0)
- {
- Serial.println(wifi.getJoinedDeviceIP());
- }
- }
- }
- //Split an string into an array for direct get_val[index]
- String split(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {
- 0, -1 };
- int maxIndex = data.length()-1;
- for(int i=0; i<=maxIndex && found<=index; i++){
- if(data.charAt(i)==separator || i==maxIndex){
- found++;
- strIndex[0] = strIndex[1]+1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement