Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <EEPROM.h>
- #include <WiFiClient.h>
- #define UART_BAUD 9600
- #define packTimeout 500 // ms (if nothing more on UART, then send packet)
- #define bufferSize 8192
- #define MODE_STA // ESP connects to WiFi router
- #define PROTOCOL_TCP
- #ifdef MODE_STA
- //const char* ssid = "Хервамтам";
- //const char* pw = "Хервамтам";
- const char *ssid = "Хервамтам"; // Your ROUTER SSID
- const char *pw = "Хервамтам"; // and WiFi PASSWORD
- IPAddress ip(10, 100, 100, 208); // From RoboRemo app, connect to this IP
- IPAddress netmask(255, 255, 255, 0);
- IPAddress gateway(10,100,100,103);
- const int port = 9876;
- #endif
- //const int mqtt_port = 1993;
- const char* mqtt_server = "1.1.1.2"; // Имя сервера MQTT
- const int mqtt_port = 1883; // Порт для подключения к серверу MQTT
- const char* mqtt_user = "Хервамтам"; // Логи к серверу MQTT
- const char* mqtt_pass = "Хервамтам"; // Пароль к серверу MQTT
- const char* topic_name = "esp/kitchen/rgb"; // Название mqtt топика для передачи цвета
- const char* topic_s = "esp/kitchen/#"; // Название mqtt топика для передачи цвета
- const int r = 15; // GIPO управления каналом цвета R
- const int g = 12; // GIPO управления каналом цвета R
- const int b = 13; // GIPO управления каналом цвета B
- const int eprom_r = 0; // Область в памяти для R
- const int eprom_g = 1; // Область в памяти для G
- const int eprom_b = 2; // Область в памяти для B
- int tm = 300;
- float temp = 0;
- int c_r = 0; // Текущий цвет R
- int c_g = 0; // Текущий цвет G
- int c_b = 0; // Текущий цвет B
- #ifdef PROTOCOL_TCP
- WiFiClient wclient;
- PubSubClient client(wclient);
- WiFiServer server(port);
- #endif
- uint8_t buf1[bufferSize];
- uint8_t i1=0;
- uint8_t buf2[bufferSize];
- uint8_t i2=0;
- void setup() {
- delay(500);
- Serial.begin(UART_BAUD);
- // Конфигурируем GPIO при старте
- pinMode(r, OUTPUT);
- pinMode(g, OUTPUT);
- pinMode(b, OUTPUT);
- EEPROM.begin(4);
- c_r = EEPROM.read(eprom_r);
- c_g = EEPROM.read(eprom_g);
- c_b = EEPROM.read(eprom_b);
- EEPROM.commit();
- setColor();
- #ifdef MODE_STA
- WiFi.mode(WIFI_STA);
- WiFi.config(ip, gateway, netmask);
- WiFi.begin(ssid, pw);
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
- while (WiFi.status() != WL_CONNECTED) {
- delay(2000);
- // Serial.println("wifion");
- // Serial.println(WiFi.localIP());
- }
- #endif
- #ifdef PROTOCOL_TCP
- //Serial.println("Starting TCP Server");
- server.begin(); // start TCP server
- #endif
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- String topicStr = topic;
- // Serial.println("Callback update.");
- // Serial.print("Topic: ");
- // Serial.println(topic);
- payload[length] = '\0';
- // Serial.println((char*)payload);
- int number = (int) strtol((char*)payload, NULL, 16);
- // Split them up into r, g, b values
- c_r = (number >> 16)*4;
- c_g = (number >> 8 & 0xFF)*4;
- c_b = (number & 0xFF)*4;
- // c_r = c_r*4;
- //Serial.println(c_r);
- // Serial.println(c_g);
- // Serial.println(c_b);
- if (topicStr == topic_name)
- {
- // Serial.print("YaYks! ");
- setColor();
- // Запишем новые цвета в память, что бы при следующем включении цвет был такой же
- EEPROM.begin(4);
- EEPROM.write(eprom_r, c_r);
- EEPROM.write(eprom_g, c_g);
- EEPROM.write(eprom_b, c_b);
- EEPROM.commit();
- } else {
- // Serial.println("Fuck");
- }
- }
- void reconnect() {
- // Повторять пока нет MQTT соединения
- while (!client.connected()) {
- // Serial.println("connecting ...");
- // Попытка подключения к MQTT
- if (client.connect("kitchenesp", mqtt_user, mqtt_pass)) {
- // Подписываемся по топики (которые мы будем слушать от брокера)
- client.subscribe(topic_s);
- // Serial.println("subscribed");
- // client.publish("esp/kitchen", "Hello from rgb led");
- } else {
- // Serial.println("not_subscribed");
- delay(50);
- }
- }
- }
- void setColor()
- {
- // Передадим значения на GPIO
- analogWrite(r, c_r);
- analogWrite(g, c_g);
- analogWrite(b, c_b);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- if(!wclient.connected()) { // if client not connected
- wclient = server.available(); // wait for it to connect
- return;
- }
- // here we have a connected client
- #ifdef PROTOCOL_TCP
- if(wclient.available()) {
- while(wclient.available()) {
- buf1[i1] = (uint8_t)wclient.read(); // read char from client (RoboRemo app)
- if(i1<bufferSize-1) i1++;
- }
- // now send to UART:
- Serial.write(buf1, i1);
- i1 = 0;
- }
- if(Serial.available()) {
- // read the data until pause:
- while(1) {
- if(Serial.available()) {
- buf2[i2] = (char)Serial.read(); // read char from UART
- if(i2<bufferSize-1) i2++;
- } else {
- //delayMicroseconds(packTimeoutMicros);
- delay(packTimeout);
- if(!Serial.available()) {
- break;
- }
- }
- }
- // now send to WiFi:
- wclient.write((char*)buf2, i2);
- i2 = 0;
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement