0
\$\begingroup\$

I'm using a Microchip RN1810 wifi module to learn about how to send data (from a PIC24F32KA302) using ethernet /wifi to another device like my windows 10 PC. I'm reading the pressure, temperature and humidity from a BME280. I got that part working. Now I have to figure out the client/server thing and the actual sending of the data. I want to connect the RN1810 to my router. I realized that I can connect the RN1810 using UDP or TCP but that's just a connection, right?. Then, I need to send the actual data which seems to be up to me how/what to send? Is there a "standard" way of packaging/sending this kind of data that I'm not aware of?

BTW This is the first time I'm doing this wifi stuff so feel free to point out anything that seems wrong. I'm aware of the "not for new designs" status on the RN1810 though.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is really too broad, but you might look into any of JSON, protocol buffers, or binary encoding for the fields. And you could look at using MQTT or a REST call as the paradigm for network interaction. JSON over either might not be the most efficient, but it's human readable and human fakeable which often proves handy. Ideally in either case you'd then encapsulate in SSL, eg, MQTT-over-SSL or REST to an https:// endpoint. \$\endgroup\$ Jul 2, 2020 at 18:49
  • \$\begingroup\$ @ Chris Stratton: I think everything you said flew over my head...lol. But I'll research those topics to learn more. Thanks. \$\endgroup\$
    – Rodo
    Jul 2, 2020 at 20:17
  • \$\begingroup\$ Coming from an industrial background, I'd like to mention Modbus as a very simple and popular protocol for master-slave operation. Namely Modbus/TCP if the sensor box has an IP stack = your sensor unit would be a Modbus/TCP server (= modbus slave). Then again, I agree with Chris that for DIY home automation or the modern IoT world, MQTT might be a better choice. That's as long as interoperability with existing other people's software is a concern. \$\endgroup\$
    – frr
    Jul 2, 2020 at 20:56
  • \$\begingroup\$ And don't feel humiliated by us suggesting standard protocols. If your goal is to cut your teeth on a basic BSD Sockets project, go ahead, just invent your own protocol. TCP is just an opaque stream of bytes. You could send your data formatted as a sequence of closest-fit binary types, or JSON as others have suggested, or just formatted as plain text :-) The latter would allow you to connect by a Telnet client and get some human-readable output immediately. If you go for binary data, you need to invent some way of detecting/deciding frame start and end... etc. \$\endgroup\$
    – frr
    Jul 2, 2020 at 21:01

2 Answers 2

1
\$\begingroup\$

If you are the one writing the software on both ends, then it's fully up to you.

The simplest way is to store all the data you want to send into a struct, and then send the struct. Something like:

struct DataPacket
{
    uint16_t temperature;
    uint16_t humidity;
    uint16_t pressure;
} __attribute__((packed));

void send_current_data() {
    struct DataPacket packet;
    packet.temperature = get_temperature();
    packet.humidity = get_humidity();
    packet.pressure = get_pressure();
    send(udp_connection, &packet, sizeof packet);
}

What this actually does is it sends 6 bytes; first a temperature in uint16_t format, then a humidity in uint16_t format, then a pressure in uint16_t format. Your server receives the data and uses the same struct (or any other method) to unpack it. Pay attention to endianness - a uint16_t on the server might not be the same format as a uint16_t on the microcontroller, you might need to byte-swap it.

Of course if 16 bits are not enough you can change it to uint32_t, or any other type.

Also pay attention to __attribute__((packed)) which tells the compiler not to insert any padding between struct members. In this particular case it's not required because no padding is required in this structure. If your CPU does need padding, you'll have to add it yourself, to make sure it's consistent on the client and the server.

Padding is added to ensure that 16-bit variables are aligned at multiples of 16 bits, 32-bit variables are aligned at multiples of 32 bits, and so on. If all the variables are the same size then it's not needed.

\$\endgroup\$
0
\$\begingroup\$

I've never worked with the RN1810 directly, only similar modules. You'd have to change the firmware for the RN1810, I couldn't find the source code for it after 5mins of looking so I don't know if it's available (I'll bet that it is, and you might have to contact microchip directly to see if they have it). It may be that the RN1810 is only configurable via command.

There are several ways to do this:

Way 1) if you have the BME280 (a really cool part) hooked directly to the RN1810 (and you have the code for the RN1810) then you could insert your code for accessing the BM280 into the the RN1810, you could even make a webserver that prints the temp humidity and whatever else you wanted (if you can find the code and compile it).

Way 2) If you have the RN1810 hooked to the PIC24F via a uart , you can then send and receive commands over TCP. The pic24F would need to send the commands over the uart to set up the RN1810. You could practice this with a PC, and when it works use the PIC24F.

There is an example of how to do this on page 43 of the RN1810 document. Under SENDING DATA USING TCP - MODULE IS A TCP CLIENT

Section 2.4.8 describes how to send data from the RX 2.4.8 set comm match

\$\endgroup\$
6
  • \$\begingroup\$ I'm using the PIC24 (I2C) to read the BME280 and planning to use its UART to control the RN1810. Why do you say I have to upgrade the firmware in the RN1810? What's wrong with its firmware? \$\endgroup\$
    – Rodo
    Jul 2, 2020 at 20:14
  • \$\begingroup\$ On other modules, you can re-write the firmware, which means you can modify the firmware to do whatever you want, I couldn't find the source code though and don't know if it's avalable. \$\endgroup\$
    – Voltage Spike
    Jul 2, 2020 at 20:19
  • \$\begingroup\$ You don't have to upgrade the firmware, the commands below should work with connecting the RN1810 to the PIC24, read the whole post carefully \$\endgroup\$
    – Voltage Spike
    Jul 2, 2020 at 20:20
  • \$\begingroup\$ I'll be doing #2 this weekend. I have a ICL3232E connected to the RN1810 (no PIC24 for now) and a serial port in my PC. I have Command Reference pdf. After reading it I realized that I could do TCP or UDP. I also read TLS but that I don't know much about. Thanks. \$\endgroup\$
    – Rodo
    Jul 2, 2020 at 20:24
  • \$\begingroup\$ Instead of saying thanks could you upvote? \$\endgroup\$
    – Voltage Spike
    Jul 2, 2020 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.