Code Monkey home page Code Monkey logo

stm32-to-internet's Introduction

STM to internet

Задание

Есть два устройства одно с кнопкой и потенциометром, второе со светодиодной лентой. Реализуйте общение двух устройств по сценарию:

  1. Нажатие кнопки -> включение/выключение света
  2. Регулировка потенциометра -> изменение яркости

ESP использовать только для связи. Управление датчиками и светодиодной лентой сделать с помощью STM

Для проверки связи STM32 и ESP

Питание ESP осуществить подключив Main Power с PINBOARD на Vin ESP.

На стороне ESP

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {     
    delay(50);
    Serial.println("Echo from ESP");
    while (Serial.available())
      Serial.write(Serial.read());  
    Serial.println();
  }
}

На стороне STM

#include "mbed.h"
#define MAXIMUM_BUFFER_SIZE 32
static BufferedSerial serial_port(PA_9, PA_10);
static BufferedSerial esp_serial(PA_2, PA_3);
int main(void)
{
  serial_port.set_baud(9600);
  serial_port.set_format(8, BufferedSerial::None, 1);
  esp_serial.set_baud(9600);
  esp_serial.set_format(8, BufferedSerial::None, 1);
  char buf[MAXIMUM_BUFFER_SIZE] = {0};
  while (1) {
    if (uint32_t num = serial_port.read(buf, sizeof(buf))) {
        esp_serial.write(buf, num);
    }
    ThisThread::sleep_for(100ms);
    if (uint32_t num = esp_serial.read(buf, sizeof(buf))) {
        serial_port.write(buf, num);
    }

  }
}

Конвертация Serial-MQTT

Дополните скетч ESP чтобы обрабатывать данные приходящие с STM и отправлять их по MQTT

void publish_when_available() {

  while(Serial.available() > 1) {
    String topic = "";
    char source = Serial.read();
    char val = Serial.read();
    if(source == 'b')
       topic = "Light/Switch";
    else if(source == 'p')
       topic = "Light/Brightness";
    if(topic != "")
       mqtt_cli.publish(topic, String((int) val));
  }

}
void callback(char *topic, byte *payload, unsigned int length) {
   
    char source = ' ';
    if(strcmp(topic, "Light/Switch") == 0)
       source = 'b';
    else if(strcmp(topic, "Light/Brightness") ==0 )
       source == 'p';
    int data = 0;
    int order = 1;
    for (int i = length -1 ; i >= 0; i--) {
      data += int((char) payload[i]) * order;
      order *= 10;
    }
    if(source != ' ' and data < 256 and data > 0) {
      Serial.write(source);
      Serial.write((char)data);
    }
}

stm32-to-internet's People

Contributors

xitowzys avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.