Advertisement
AsmodHacker

Arduino Mega2560, ESP WiFi Module Command Device

Mar 11th, 2015
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. //Created By Matthew aka Elitism 2015
  2. //Arduino Mega2560 + esp8266 wifi module (connect to router and accept LAN connection String commands)
  3.  
  4. #include "ESP8266.h"
  5. #define SSID "raptor" //Your Router WiFi SSID
  6. #define PASSWORD "poodsdfdf@@WF@WF@WF<<" //Your Router WiFi Password
  7. #define PORT 8080 //The Port you want the TCP Server to run on
  8.  
  9. ESP8266 wifi(Serial1);
  10.  
  11. int r = 255;
  12. int g = 255;
  13. int b = 255;
  14.  
  15. void setup(void)
  16. {
  17. pinMode(8, OUTPUT);
  18. pinMode(9, OUTPUT);
  19. pinMode(10, OUTPUT);
  20. analogWrite(8, r);
  21. analogWrite(9, g);
  22. analogWrite(10, b);
  23.  
  24. Serial.begin(115200);
  25. Serial.println("Wifi Connecting: " + (String)SSID);
  26. wifi.setOprToStationSoftAP(); //Set Mode - Connect to Access Point
  27. if (wifi.joinAP(SSID, PASSWORD)) {
  28. wifi.enableMUX();
  29. wifi.startTCPServer(PORT);
  30. wifi.setTCPServerTimeout(10000);
  31. Serial.print((String)SSID + " TCP Server Port: " + String(PORT) + "\n");
  32. Serial.println(wifi.getLocalIP().c_str());
  33. }
  34. else {
  35. Serial.println("Connection Failed");
  36. }
  37. }
  38.  
  39. void loop(void)
  40. {
  41. uint8_t buffer[128] = {
  42. 0 };
  43. uint8_t mux_id;
  44. uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
  45. String Message;
  46. if (len > 0) {
  47. wifi.send(mux_id, buffer, len);//Return message received
  48.  
  49. //Get the message out of the buffer
  50. for(uint32_t i = 0; i < len; i++) {
  51. Message += (String)(char)buffer[i];
  52. }
  53.  
  54. //Print the message
  55. Serial.println(Message);
  56.  
  57. //Accept commands to change rgb led ;)
  58. if(Message.indexOf("rgb") >=0)
  59. {
  60. //R & B are switched (Using Common Anode RGB LED)
  61. int r = map(split(Message, ',', 3).toInt(), 0, 255, 255, 0); //Split & Map Third Value
  62. int g = map(split(Message, ',', 2).toInt(), 0, 255, 255, 0); //Split & Map Second Value
  63. int b = map(split(Message, ',', 1).toInt(), 0, 255, 255, 0); //Split & Map First Value
  64. analogWrite(8, r);
  65. analogWrite(9, g);
  66. analogWrite(10, b);
  67. Serial.println("DATA: " + (String)r + "," + (String)g + "," + (String)b + "\r\n");
  68. }
  69.  
  70. //Test & Respond
  71. if(Message.indexOf("test") >=0)
  72. {
  73. uint8_t dcrespond[] = "Received";
  74. wifi.send(mux_id, dcrespond, sizeof(dcrespond) - 1);//Return message received
  75. }
  76.  
  77. //Disconnect from the TCP Connection
  78. if(Message.indexOf("disconnect") >=0)
  79. {
  80. wifi.releaseTCP(mux_id);
  81. Serial.println("(Client " + (String)mux_id + ") Disconnected");
  82. }
  83.  
  84. if(Message.indexOf("devs") >=0)
  85. {
  86. Serial.println(wifi.getJoinedDeviceIP());
  87. }
  88. }
  89. }
  90.  
  91. //Split an string into an array for direct get_val[index]
  92. String split(String data, char separator, int index)
  93. {
  94. int found = 0;
  95. int strIndex[] = {
  96. 0, -1 };
  97. int maxIndex = data.length()-1;
  98. for(int i=0; i<=maxIndex && found<=index; i++){
  99. if(data.charAt(i)==separator || i==maxIndex){
  100. found++;
  101. strIndex[0] = strIndex[1]+1;
  102. strIndex[1] = (i == maxIndex) ? i+1 : i;
  103. }
  104. }
  105. return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement