A
A
Actor2015-11-19 10:39:52
Arduino
Actor, 2015-11-19 10:39:52

Communication protocol with Arduino?

Good afternoon!
I am making my own server to control arduna through a com port.
At the design stage, a question arose about the protocol for communicating with the adruina. The server should receive and send commands to the arduino, but how?
In a simple implementation, I would do something like this:
"MODE:SET; P_N:1; P_IN:0; P_LOW: 0." - sending a command to the device to set pin 1 to the IN, LOW position.
"MODE:GET; P_N:1." - sending a command to receive data or the state of pin 1 to the device.
All this is quite simple when we only deal directly with the pin, but what if DHT11 hangs on pin 1 and you need to get the temperature and humidity?
"MODE:GET; P_N:1, 1 "? - get data from pin 1, conditionally the first data (temperature).
And if a shift register hangs on the leg?
"MODE:SET; P_N:1, 0,1,0,0,0,1,1,1 ; P_IN:0; P_LOW: 0." - pin 1 to the IN, LOW position and turn on the corresponding pins on the shift register.
And if there are sensors on the 1Wire leg??

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Alexander Gusev, 2015-11-19
@Sanchogus

The whole protocol is in an exchange of words, in fact.
Those. for a certain set of input words, you can make functions that will work and send a string in response - the result.
You can do something like this (don't kick too much for the shitty code, this is an example!):
send a string. Arudina reads it, if it finds a substring, then it performs a certain action, sometimes it writes some words to the terminal that the action has been completed.

if (strstr(term_in, "SWPOWER"   ) != 0) code = 1;/*strstr - найти подстроку в строке, пришедшей с ПК (алгоритм чтения слова опустим)*/
if (strstr(term_in, "CALL_1"    ) != 0) code = 2;
if (strstr(term_in, "SMS_SEND"  ) != 0) code = 3;
if (strstr(term_in, "CTRL+Z"    ) != 0) code = 4;
//в зависимости от найденной подстроки был получен один из кодов и по нему работает case
      switch (code)
      {
        case 1:
          Serial.println("@power on/off signal");
          digitalWrite(9, HIGH);
          delay(1000);
          digitalWrite(9, LOW);
          break;
        case 2:
          Serial.println("[email protected]: call number +790679__823");//отправляем сообщение, что начата попытка дозвона
          gprsSerial.println("ATD+790679__823;");//в шилд
          break;
        case 3:
          function1();
   default:
          /*Код, который выполнится, если ниодно из константых значению не соответствует значение в переменной variable*/
          Serial.print("text ");
          break;

A
Alexander Volkov, 2015-11-20
@a_volkov1987

First, determine what equipment will be connected to the arduino and in what form it will receive and give data to communicate with the server.
Then decide how exactly the communication with the server will take place. Maybe you need data from the equipment only "on request" or the arduina should give the data itself at a certain interval.
Well and proceeding from it develop the protocol.
Define the packet header for transmitting and receiving, if you have to receive and transmit packets of different lengths, select a section in the header to describe the length of the packet. And do not transfer data in stock form, if you can do without it. That is, the section of the package that describes the recipient of the data should not be with string data. Use code. 01 - modem, A2 - temperature sensor .... F0 - humidity sensor, etc.
In principle, a packet from the server can look like this:
AA 00 - (2 bytes) packet start indicator
XX XX - (2 bytes) packet length in bytes (without header)
XX - (1 byte) data recipient
XX...XX (n bytes) argument to pass to the recipient
Optionally, at the end of the packet, you can pass a checksum calculated as a function of the packet header and body.
What and how is passed in the argument is up to you. For a DHT sensor, you may not specify anything at all, and for a modem, the first byte of the packet may contain an AT command in the form of a conditional code, 10 bytes after it - the subscriber's number without 8 and (possibly) another n bytes with text if this is an SMS transmission.
To control any indication, you can send 1 byte, in which the type of indication will be encrypted (00 - indication off, 01 - constant indication, 02 - blinking indication)
You can receive data from arduino in the same package.

M
Max, 2015-11-19
@MaxDukov

make separate commands for working directly with pins, separate commands for working with sensors, separate commands for registers. Otherwise, it turns out that ALL logic is implemented on the computer, the arduina only implements the transmission / reception of data.

A
AntHTML, 2015-11-19
@anthtml

UART for a microcontroller (arduino) is just an interface for exchanging text or bit messages, it has nothing to do with pins and other devices, but has an output only to the software part of the MK. So how you implement its work in the firmware (sketch), so it will work. Take the commands coming from serial, decrypt them and give tasks to the functions of working with pins or devices. Collect data from them, form text responses from the data and send to serial. There are practically no standards - everything here is as the soul of the developer / customer wishes.

V
Vitaly Pukhov, 2016-03-21
@Neuroware

It seems to me that you are trying to write another Firmata , look towards this library, maybe everything you need is already there

M
Melz, 2015-11-19
@melz

Well, usually for this they take RS-232 and the company through MAX232 or its alternative.
https://www.arduino.cc/en/Tutorial/ArduinoSoftwareRS232
Well, how does Arduino determine what is hanging on its leg? She does not have the gift of prediction, if you just hang a 1Wire sensor on your leg, nothing will happen.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question