Advertisement
Guest User

Untitled

a guest
Nov 11th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <EEPROM.h>
  4. #include <WiFiClient.h>
  5.  
  6. #define UART_BAUD 9600
  7. #define packTimeout 500 // ms (if nothing more on UART, then send packet)
  8. #define bufferSize 8192
  9.  
  10. #define MODE_STA // ESP connects to WiFi router
  11.  
  12. #define PROTOCOL_TCP
  13.  
  14. #ifdef MODE_STA
  15. const char *ssid = "XXXXXX";  // Your ROUTER SSID
  16. const char *pw = "XXXXXX"; // and WiFi PASSWORD
  17. IPAddress ip(XXX, XXX, XXX, XXX); // From RoboRemo app, connect to this IP
  18. IPAddress netmask(255, 255, 255, 0);
  19. IPAddress gateway(XXX,XXX,XXX,XXX);
  20. const int port = 9876;
  21. #endif
  22.  
  23. const char* mqtt_server = "XXX.XXX.XXX.XXX"; // Имя сервера MQTT
  24. const int mqtt_port = 1883; // Порт для подключения к серверу MQTT
  25. const char* mqtt_user = "XXX"; // Логи к серверу MQTT
  26. const char* mqtt_pass = "XXX"; // Пароль к серверу MQTT
  27. const char* topic_name = "XXXXXX"; // Название mqtt топика для передачи цвета
  28. const char* topic_s = "XXXXXX"; // Название mqtt топика для передачи цвета
  29.  
  30. const int r = 15; // GIPO управления каналом цвета R
  31. const int g = 12; // GIPO управления каналом цвета R
  32. const int b = 13; // GIPO управления каналом цвета B
  33.  
  34. const int eprom_r = 0; // Область в памяти для R
  35. const int eprom_g = 1; // Область в памяти для G
  36. const int eprom_b = 2; // Область в памяти для B
  37.  
  38. int tm = 300;
  39. float temp = 0;
  40. int c_r = 0; // Текущий цвет R
  41. int c_g = 0; // Текущий цвет G
  42. int c_b = 0; // Текущий цвет B
  43.  
  44.  
  45. WiFiClient client;
  46. PubSubClient mqclient(client);
  47. WiFiServer server(port);
  48.  
  49.  
  50. uint8_t buf1[bufferSize];
  51. uint8_t i1=0;
  52.  
  53. uint8_t buf2[bufferSize];
  54. uint8_t i2=0;
  55.  
  56. void setup() {
  57.  
  58.     delay(500);
  59.  
  60.   Serial.begin(UART_BAUD);
  61.  
  62.     // Конфигурируем GPIO при старте
  63.   pinMode(r, OUTPUT);
  64.   pinMode(g, OUTPUT);
  65.   pinMode(b, OUTPUT);
  66.  
  67.   EEPROM.begin(4);
  68.   c_r = EEPROM.read(eprom_r);
  69.   c_g = EEPROM.read(eprom_g);
  70.   c_b = EEPROM.read(eprom_b);
  71.   EEPROM.commit();
  72.  
  73.   setColor();
  74.  
  75.   #ifdef MODE_STA
  76.   WiFi.mode(WIFI_STA);
  77.   WiFi.config(ip, gateway, netmask);
  78.   WiFi.begin(ssid, pw);
  79.   mqclient.setServer(mqtt_server, mqtt_port);
  80.   mqclient.setCallback(callback);
  81.   while (WiFi.status() != WL_CONNECTED) {
  82.     delay(2000);
  83.   //  Serial.println("wifion");
  84.   //  Serial.println(WiFi.localIP());
  85.   }
  86.   #endif
  87.    
  88.  
  89.   Serial.println("Starting TCP Server");
  90.   server.begin(); // start TCP server
  91.  
  92. }
  93.  
  94.  
  95. void callback(char* topic, byte* payload, unsigned int length) {
  96.   String topicStr = topic;
  97.   Serial.println("Callback update.");
  98.  // Serial.print("Topic: ");
  99.  // Serial.println(topic);
  100.  
  101.   payload[length] = '\0';
  102.  // Serial.println((char*)payload);
  103.  
  104.   int number = (int) strtol((char*)payload, NULL, 16);
  105.  
  106.   // Split them up into r, g, b values
  107.   c_r = (number >> 16)*4;
  108.   c_g = (number >> 8 & 0xFF)*4;
  109.   c_b = (number & 0xFF)*4;
  110.  // c_r = c_r*4;
  111.  
  112.   //Serial.println(c_r);
  113.  // Serial.println(c_g);
  114.  // Serial.println(c_b);
  115.  
  116.    if (topicStr == topic_name)
  117.   {
  118.    // Serial.print("YaYks! ");
  119.        setColor();
  120.  
  121.       // Запишем новые цвета в память, что бы при следующем включении цвет был такой же
  122.       EEPROM.begin(4);
  123.       EEPROM.write(eprom_r, c_r);
  124.       EEPROM.write(eprom_g, c_g);
  125.       EEPROM.write(eprom_b, c_b);
  126.       EEPROM.commit();
  127.     } else {
  128.      // Serial.println("Fuck");
  129.     }
  130. }
  131.  
  132.  
  133. void reconnect() {
  134.   // Повторять пока нет MQTT соединения
  135.   while (!mqclient.connected()) {
  136.   //  Serial.println("connecting ...");
  137.     // Попытка подключения к MQTT
  138.     if (mqclient.connect("kitchenesp", mqtt_user, mqtt_pass)) {
  139.        // Подписываемся по топики (которые мы будем слушать от брокера)
  140.       mqclient.subscribe(topic_s);
  141.    //   Serial.println("subscribed");
  142.     //  client.publish("esp/kitchen", "Hello from rgb led");
  143.     } else {
  144.     //  Serial.println("not_subscribed");
  145.       delay(50);
  146.     }
  147.   }
  148. }
  149.  
  150. void setColor()
  151. {
  152.   // Передадим значения на GPIO
  153.   analogWrite(r, c_r);
  154.   analogWrite(g, c_g);
  155.   analogWrite(b, c_b);
  156. }
  157.  
  158. void loop() {
  159.  
  160.   if (!mqclient.connected()) {
  161.   reconnect();
  162.   }
  163.  
  164.   if(!client.connected()) { // if client not connected
  165.     client = server.available(); // wait for it to connect
  166.      return;
  167.   }
  168.  
  169.  
  170. mqclient.loop();
  171.    
  172.   // here we have a connected client
  173.  
  174.   if(client.available()) {
  175.     while(client.available()) {
  176.       Serial.println("read");
  177.       buf1[i1] = (uint8_t)client.read(); // read char from client (RoboRemo app)
  178.       if(i1<bufferSize-1) i1++;
  179.     }
  180.     // now send to UART:
  181.     Serial.write(buf1, i1);
  182.     i1 = 0;
  183.   }
  184.  
  185.   if(Serial.available()) {
  186.  
  187.     // read the data until pause:
  188.     while(1) {
  189.       if(Serial.available()) {
  190.         buf2[i2] = (char)Serial.read(); // read char from UART
  191.         if(i2<bufferSize-1) i2++;
  192.       } else {
  193.         //delayMicroseconds(packTimeoutMicros);
  194.         delay(packTimeout);
  195.         if(!Serial.available()) {
  196.           break;
  197.         }
  198.       }
  199.     }
  200.    
  201.     // now send to WiFi:
  202.     client.write((char*)buf2, i2);
  203.     i2 = 0;
  204.   }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement