Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #10 from ubidots/mariacarlina/fixing_memory_issues
Browse files Browse the repository at this point in the history
Mariacarlina/fixing memory issues
  • Loading branch information
woakas committed Feb 4, 2018
2 parents f30fcd1 + 50ccb30 commit f308784
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 149 deletions.
30 changes: 21 additions & 9 deletions README.md
Expand Up @@ -21,7 +21,7 @@ Before to start, you must choose which logger to use, in this case we decided us
* [UartSBee](http://wiki.seeed.cc/UartSBee_V4/) or any Uart to USB device.
* [Arduino IDE 1.6.0 or higher](https://www.arduino.cc/en/Main/Software)
* [Ubidots Arduino ESP8266 library](https://github.com/ubidots/ubidots-esp8266-serial/archive/master.zip)
* [Ubidots Account](https://ubidots.com/).
* [Ubidots Account](https://ubidots.com/).

## Command request

Expand All @@ -37,35 +37,47 @@ The first part of the structure of the Payload is:
Where:

* **USER_AGENT**: An identifier of your choice to help us to log your data for possible debugging in the future.
* **USER_AGENT**: An identifier of your choice to help us to log your data for possible debugging in the future.
* **VERSION**: It's a good practice to include a version in case you're wrapping these requests within a library.

**Here are some examples of a USER_AGENT/VERSION**: "CompanyIoT/1.0" or "Particle/1.3"

* **METHOD(POST/GET/LV)**: Of the following protocol methods, you must choose one. To send values to Ubidots use the method `POST`. To get value from Ubidots you have to use the methods `GET` or `LV`.
* **TOKEN**: Your Ubidots account TOKEN.
* **TOKEN**: Your Ubidots account [TOKEN](http://help.ubidots.com/user-guides/find-your-token-from-your-ubidots-account).
* **DEVICE_NAME**: The name of the device at Ubidots.
* **DEVICE_LABEL**: The unique label of your Device. [Check out our API docs](https://ubidots.com/docs/api/index.html#send-values) to learn how to send data to Ubidots.
* **VARIABLE_LABEL**: The unique label of your Variable. [Check out our API docs](https://ubidots.com/docs/api/index.html#send-values) to learn how to send data to Ubidots.
* **VARIABLE_ID**: This is the unique ID of your Device. [Check out our API docs](https://ubidots.com/docs/api/index.html#send-values) to learn how to send data to Ubidots

The library will verify the request to Ubidots´ server using `init#` and `#final` at the start and final of the payload/command. Reference: as you can see -> `init# command #final`
The library will verify the request to Ubidots server using `init#` and `#final` at the start and final of the payload/command. Reference: as you can see -> `init# command #final`

Also, we setup an `ONLINE?` command to let you know if the everything is OK with the connection.
Also, we setup an `ONLINE?` command to let you know if the everything is OK with the connection.

These **request examples** will help you understand how to build the command:

**WARNING**: Based on the **BUFFER SIZE** you will be able to send a command with a maximum length of **128 characters**. If your command exceed the maximum lenght, you will received an `error message`.


* Verify the connection (**ONLINE?**):

> init#ONLINE?#final
* Send values to Ubidots (**POST**):
> init#USER_AGENT/VERSION|POST|TOKEN|DEVICE_LABEL:DEVICE_NAME=>VARIABLE:VALUE|end#end
* Send values to Ubidots(**POST**) :

**POST**

> init#USER_AGENT/VERSION|POST|TOKEN|DEVICE_LABEL:DEVICE_NAME=>VARIABLE:VALUE|end#final
The command below will assign automatically the `ÙSER_AGENT` as **device name**:

> init#USER_AGENT/VERSION|POST|TOKEN|DEVICE_LABEL=>VARIABLE:VALUE|end#final
**Command request example**:

> init#UbidotsESP8266/2.0|POST|4fgSKmYujOJZ2x63s7376xbFtaSLvq0|telemetry:ESP8266=>temperature:91.00000|end#final
> init#UbidotsESP8266/2.0|POST|4fgSKmYujOJZ2x63s7376xbFtaSLvq0|telemetry=>temperature:91.00000|end#final
* Get value from Ubidots (**GET / LV**):

**GET**:
Expand All @@ -86,6 +98,6 @@ These **request examples** will help you understand how to build the command:
## Setup

1. Setup the **telemetry unit - ESP8266**. Reference to the [Documentation](https://github.com/ubidots/ubidots-esp8266-serial/tree/master/docs/TelemetryUnit_ESP8266.md) to find all the steps required.
1. Setup the **telemetry unit - ESP8266**. Reference to the [Documentation](https://github.com/ubidots/ubidots-esp8266-serial/tree/master/docs/TelemetryUnit_ESP8266.md) to find all the steps required.
2. Setup the **Logger - Arduino MEGA**. Reference to the [Documentation](https://github.com/ubidots/ubidots-esp8266-serial/tree/master/docs/Logger.md) to find all the steps required.
3. Once the you complete the steps above, go to your [Ubidots account](https://ubidots.com/) to visualize the data received.
3. Once the you complete the steps above, go to your [Ubidots account](https://ubidots.com/) to visualize the data received.
121 changes: 69 additions & 52 deletions docs/Logger.md
@@ -1,10 +1,10 @@
# Logger
# Logger

The logger will be in charge of taking sensor (pin) readings and send values to the **Telemetry Unit** via serial.
The logger will be in charge of taking sensor (pin) readings and send values to the **Telemetry Unit** via serial.

You can use any microcontroller as logger, but please note that if the logger (microcontroller) does not have more than one hardware serial ports you will not able to visualize the response, but it is also possible to send data without any issue.

We decided to used the **Arduino MEGA** as logger, because it has more than one hardware serial ports available.
We decided to used the **Arduino MEGA** as logger, because it has more than one hardware serial port available.

## Hardware setup - Wiring

Expand All @@ -15,29 +15,29 @@ Once the code is uploaded into the telemetry unit (**ESP8266**), you have to mak
Arduino MEGA | ESP8266
-------------|---------
RX1 - (19) | TX
TX1 - (18) | RX
TX1 - (18) | RX
3.3V | 3V3
3.3V | CH_PD
3.3V | CH_PD
GND | GND

* **Diagram Connections**:

![ESP8266 Logger](https://cdn2.hubspot.net/hubfs/329717/Documentation%20files/images/ArduinoMEGA_ESP8266_Logger.png)
![ESP8266 Logger](https://cdn2.hubspot.net/hubfs/329717/Documentation%20files/images/ArduinoMEGA_ESP8266_Logger.png)


## Firmware Setup - Arduino IDE
## Firmware Setup - Arduino IDE

1. Once you completed the step before go to the **Arduino IDE**.
2. Assign the board to compile, in this case **Arduino Mega**. Click on **Tools > Board: "Arduino/Genuino Mega or Mega 2560"**.
3. Select the Port assigned for the board. Click on **Tools > Port > Select the port assgined**.
4. Paste the code provided below with the modification required (reference to the next step).
5. Upload the code into the board. Then, verify it works through the **Serial Monitor**.
5. Upload the code into the board. Then, verify it works through the **Serial Monitor**.

## Send command via serial - Manual
## Send command via serial - Manual

The following code lets you send commands through the Serial Monitor. This will help you to verify if the logger and the Telemetry unit are working together.
The following code lets you send commands through the Serial Monitor. This will help you to verify if the logger and the Telemetry unit are working together.

**NOTE**: To know how to build the command required, reference library's **README**.
**NOTE**: To know how to build the command required, reference library's **README**.

Once the code is uploaded, open the **Serial Monitor** and send the command, then, when all is programmed correctly, you have to receive the **"OK"**response from the server. If you receive an **"ERROR"** response verify the structure of the command.

Expand All @@ -47,30 +47,30 @@ Once the code is uploaded, open the **Serial Monitor** and send the command, the
/****************************************
* Define Constants
****************************************/
char command[700]; // command
char command[128]; // command
char answer[100]; // answer from server

/****************************************
* Main Functions
****************************************/
****************************************/
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
}

void loop() {

int i = 0;
int j = 0;

if (Serial.available()){
while (Serial.available() > 0) {
command[i++] = (char)Serial.read();
}
/* Sends the command to the telemetry unit */
Serial1.print(command);
Serial1.print(command);
i = 0;
}
}

if (Serial1.available() > 0) {
/* Reading the telemetry unit */
Expand All @@ -84,28 +84,26 @@ void loop() {
}
```

## Send command via serial - Automatically
## Send command via serial - Automatically

Once you verify that everythings works as it should, you can use the following code to manage your sensors readings and send the values to Ubidots.

As you can see at the first part of the code you just have to assign your Ubidots TOKEN where is indicated. And also, please assign your device and variable parameters apporpriately if you adjusted any portion of code provided in this documentation.
As you can see at the first part of the code you just have to assign your Ubidots TOKEN where is indicated. And also, please assign your device and variable parameters apporpriately if you adjusted any portion of code provided in this documentation.

```c++
/****************************************
* Define Constants
****************************************/
namespace {
bool flow_control = true; // control the flow of the requests
const char * USER_AGENT = "UbidotsESP8266"; // Assgin the user agent
const char * VERSION = "1.0"; // Assign the version
const char * VERSION = "1.0"; // Assign the version
const char * METHOD = "POST"; // Set the method
const char * TOKEN = "Put_your_Ubidots_token_here"; // Assign your Ubidots TOKEN
const char * DEVICE_NAME = "telemetry"; // Assign the desire device name
const char * DEVICE_LABEL = "ESP8266"; // Assign the device label
const char * VARIABLE_LABEL_1 = "temperature"; // Assign the variable label
const char * VARIABLE_LABEL_2 = "humidity"; // Assign the variable label
const char * TOKEN = "........"; // Assign your Ubidots TOKEN
const char * DEVICE_LABEL = "ESP8266"; // Assign the device label
const char * VARIABLE_LABEL = "temp"; // Assign the variable label
}

char command[700]; // command
char telemetry_unit[100]; // response of the telemetry unit

/* Space to store values to send */
Expand All @@ -114,45 +112,64 @@ char str_sensor2[10];

/****************************************
* Main Functions
****************************************/
****************************************/
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
}

void loop() {
char* command = (char *) malloc(sizeof(char) * 128);
/* Wait for the server response to read the values and built the command */
/* While the flag is true it will take the sensors readings, build the command,
and post the command to Ubidots */
if (flow_control) {
/* Analog reading */
float sensor1 = analogRead(A0);
float sensor2 = analogRead(A1);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(sensor1, 4, 2, str_sensor1);
dtostrf(sensor2, 4, 2, str_sensor2);

/* Building the logger command */
sprintf(command, "init#");
sprintf(command, "%s%s/%s|%s|%s|", command, USER_AGENT, VERSION, METHOD, TOKEN);
sprintf(command, "%s%s=>", command, DEVICE_LABEL);
sprintf(command, "%s%s:%s", command, VARIABLE_LABEL, str_sensor1);
sprintf(command, "%s|end#final", command);

/* Prints the command sent */
//Serial.println(command);// uncomment this line to print the command

/* Analog reading */
float sensor1 = analogRead(A0);
float sensor2 = analogRead(A1);

/* 4 is mininum width, 2 is precision; float value is copied onto str_sensor*/
dtostrf(sensor1, 4, 2, str_sensor1);
dtostrf(sensor2, 4, 2, str_sensor2);

/* Building the logger command */
sprintf(command, "init#");
sprintf(command, "%s%s/%s|%s|%s|", command, USER_AGENT, VERSION, METHOD, TOKEN);
sprintf(command, "%s%s:%s=>", command, DEVICE_NAME, DEVICE_LABEL);
sprintf(command, "%s%s:%s", command, VARIABLE_LABEL_1, str_sensor1);
//sprintf(command, "%s,%s:%s", command, VARIABLE_LABEL_2, str_sensor2); // uncomment this line to send sensor 2 values
sprintf(command, "%s|end#final", command);

/* Prints the command sent */
//Serial.println(command);// uncomment this line to print the command

/* Sends the command to the telemetry unit */
Serial1.print(command);
/* Sends the command to the telemetry unit */
Serial1.print(command);
/* free memory*/
free(command);
/* Change the status of the flag to false. Once the data is sent, the status
of the flag will change to true again */
flow_control = false;
}

/* Reading the telemetry unit */
int i = 0;
while (Serial1.available() > 0) {
telemetry_unit[i++] = (char)Serial1.read();
telemetry_unit[i++] = (char)Serial1.read();
/* Change the status of the flag; allows the next command to be built */
flow_control = true;
}

if (flow_control) {
/* Print the server response -> OK */
Serial.write(telemetry_unit);
/* free memory */
memset(telemetry_unit, 0, i);
}
Serial.println(telemetry_unit);
i = 0;
delay(5000);

delay(1000);
}
```

If you desire sending more values, reference the **README** to learn how to build the request containing more than one variable. Then, you will have to modify the command (as seen above) `/* Building the logger command */` to include a data traffic rate that is greater than one variable.

**WARNING**: Based on the **BUFFER SIZE** you will be able to send a command with a maximum length of **128 characters**. If your command exceed the maximum lenght, you will received an `error message`.
18 changes: 9 additions & 9 deletions docs/TelemetryUnit_ESP8266.md
@@ -1,8 +1,8 @@
# Telemetry unit (ESP8266)

As we know **Telemetry** is an automated communications process by which measurements and other data is collected at remote or inaccessible points and transmitted to receiving equipment for monitoring.
As we know **Telemetry** is an automated communications process by which measurements and other data is collected at remote or inaccessible points and transmitted to receiving equipment for monitoring.

In this case, we are using the ESP8266 as a telemetry unit. We collect the data from the logger (Arduino MEGA) and the ESP8266 will be in charge of sending the collected data to Ubidots.
In this case, we are using the ESP8266 as a telemetry unit. We collect the data from the logger (Arduino MEGA) and the ESP8266 will be in charge of sending the collected data to Ubidots.

## Hardware setup - Wiring

Expand All @@ -12,7 +12,7 @@ In this case, we are using the ESP8266 as a telemetry unit. We collect the data

To program the **ESP8266** you have to use a programmer as **UartSBee**, also you can use the **Arduino MEGA** to program the ESP8266. To upload the code into the ESP8266 you have to follow the connections below.

* **ESP8266 - Arduino MEGA / ESP8266 - UartSBee connections**:
* **ESP8266 - Arduino MEGA / ESP8266 - UartSBee connections**:

ESP8266 | Arduino MEGA | UartSBee
--------|--------------|---------
Expand Down Expand Up @@ -59,8 +59,8 @@ Once you have pasted the code below into the **Arduino IDE**, you will have to a
/****************************************
* Define Constants
****************************************/
namespace {
const char * WIFISSID = "Put_your_WIFI_SSID_here"; // Assign your WiFi SSID
namespace {
const char * WIFISSID = "Put_your_WIFI_SSID_here"; // Assign your WiFi SSID
const char * PASSWORD = "Put_your_WIFI_password_here"; // Assign your WiFi password
const char * TOKEN = "Put_your_Ubidots_Token_here"; // Assign your Ubidots TOKEN
}
Expand All @@ -69,7 +69,7 @@ Ubidots client(TOKEN);

/****************************************
* Main Functions
****************************************/
****************************************/
void setup() {
Serial.begin(115200);
client.wifiConnection(WIFISSID, PASSWORD);
Expand All @@ -81,9 +81,9 @@ void loop() {
}
```
Once the code is uploaded into the ESP8266, reference to the [Documentation](https://github.com/ubidots/ubidots-esp8266-serial/tree/master/docs/TelemetryUnit_ESP8266.md) of **telemetry unit - ESP8266** to find all the next steps required.
Once the code is uploaded into the ESP8266, reference to the [Documentation](https://github.com/ubidots/ubidots-esp8266-serial/tree/master/docs/TelemetryUnit_ESP8266.md) of **telemetry unit - ESP8266** to find all the next steps required.
**NOTE**: The sampling frequency on the ESP8266 is of 1000 milliseconds
**NOTE**: The sampling frequency on the ESP8266 is of **1000 milliseconds**
## FAQs and Troubleshooting
Expand All @@ -93,4 +93,4 @@ One of the most common troubleshooting with the ESP8266 is this one:
If you got the issue above, please verify if the connections are in the right way, check if the Arduino **RST** is setted to **GND** , also verify if the board **Generic ESP8266 Module** is selected on the Arduino IDE.
**NOTE**: If you use the Arduino MEGA to program the ESP8266 don't forget establish the new wires connections to be able to communicate the devices through the hardware serial port.
**NOTE**: If you use the Arduino MEGA to program the ESP8266 don't forget establish the new wires connections to be able to communicate the devices through the hardware serial port.

0 comments on commit f308784

Please sign in to comment.