The Adafruit Ethernet FeatherWing is the perfect option for permanent installations using a reliable wired internet connections. What we like most about the Ethernet FeatherWing is that it works with the entire Feather board series and can be simply managed to run a number of different applications with high data rates using an ethernet connection. Click to learn more about the Ethernet FeatherWing from Adafruit's official page.
In the following tutorial, Ubidots will demonstrate how to setup and program the Adafruit Ethernet FeatherWing using the Arduino IDE to visualize and interpret data using Ubidots IoT Application Platfrom.
Requirements
Step-By-Step
Setup the Arduino IDE with you device
Hardware Setup
Programming
Summary
1. Setup the Arduino IDE with your device
The Adafruit Ethernet FeatherWing can be used with any Feather Board, and the sample code provided below was designed openly to fit the series, so choose your Feather hardware freely.
To learn how to setup the Feather boards reference to the Adafruit site
Looking for the Feather HUZZAH ESP8266 with the Arduino IDE setup? Reference to this guide to get you to the right place.
a) Library installations :
1. Go to the Adafruit library repository to download the Ethernet2 library. To download the library, click the green button called "Clone or download" and select "Download ZIP".
2. Now back in the Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library and select the .ZIP file of Ethernet2-master and then “Accept” or “Choose”. Once the library is successfully you will receive the message "Library added to your libraries".
Step 2. Hardware setup
1. Plug in the Ethernet FeatherWing into the Feather board.
2. Connect one end of the Ethernet cable to the Ethernet FeatherWing and the other to your hub, router, or switch.
Step 3. Sending (POST) Data to Ubidots
With the following sample code, we will POST ANALOG readings taken from the A0 pin of the Feather board to Ubidots for data enablement.
1. To post your first value to Ubidots, open the Arduino IDE and paste the sample code below. Once you've pasted the code, you will need to assign your individual, unique Ubidots TOKEN for authentication.
/********************************
* Libraries included
*******************************/
#include <SPI.h>
#include <Ethernet2.h>
/********************************
* Constants and objects
*******************************/
/* Enter a MAC address for your controller below.
Newer Ethernet shields have a MAC address printed on a sticker on the shield */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
/* Set the static IP address to use if the DHCP fails to assign */
IPAddress ip(192, 168, 0, 177);
/* Initialize the Ethernet client library
with the IP address and port of the server
that you want to connect to (port 80 is default for HTTP) */
EthernetClient client;
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
//#define WIZ_RESET 9
#if defined(ESP8266)
// default for ESPressif
#define WIZ_CS 15
#elif defined(ESP32)
#define WIZ_CS 33
#elif defined(ARDUINO_STM32_FEATHER)
// default for WICED
#define WIZ_CS PB4
#elif defined(TEENSYDUINO)
#define WIZ_CS 10
#elif defined(ARDUINO_FEATHER52)
#define WIZ_CS 11
#else // default for 328p, 32u4 and m0
#define WIZ_CS 10
#endif
const char* TOKEN = "...."; // Put here your TOKEN
const char* DEVICE_LABEL = "feather-ethernet"; // Your device label
const char* VARIABLE_LABEL = "temperature"; // Your variable label
const char* HTTPSERVER = "industrial.api.ubidots.com";
const char* USER_AGENT = "ESP8266";
const char* VERSION = "1.0";
/********************************
* Auxiliar Functions
*******************************/
/**
* Gets the length of the body
* @arg variable the body of type char
* @return dataLen the length of the variable
*/
int dataLen(char* variable) {
uint8_t dataLen = 0;
for (int i = 0; i <= 250; i++) {
if (variable[i] != '\0') {
dataLen++;
} else {
break;
}
}
return dataLen;
}
/********************************
* Main Functions
*******************************/
void setup() {
#if defined(WIZ_RESET)
pinMode(WIZ_RESET, OUTPUT);
digitalWrite(WIZ_RESET, HIGH);
delay(100);
digitalWrite(WIZ_RESET, LOW);
delay(100);
digitalWrite(WIZ_RESET, HIGH);
#endif
#if !defined(ESP8266)
/* wait for serial port to connect */
while (!Serial);
#endif
/* Open serial communications and wait for port to open */
Serial.begin(115200);
delay(1000);
Serial.println("\nSetting up...");
Ethernet.init(WIZ_CS);
/* give the ethernet module time to boot up */
delay(1000);
/* start the Ethernet connection */
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
/* no point in carrying on, so do nothing forevermore:
try to congifure using IP address instead of DHCP */
Ethernet.begin(mac, ip);
}
/* print the Ethernet board/shield's IP address */
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
/* if there's incoming data from the net connection.
send it out the serial port. This is for debugging
purposes only */
if (client.available()) {
char c = client.read();
Serial.write(c);
}
/* if ten seconds have passed since your last connection,
then connect again and send data */
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
/* this method makes a HTTP connection to the server */
void httpRequest() {
char* body = (char *) malloc(sizeof(char) * 150);
char* data = (char *) malloc(sizeof(char) * 300);
/* Space to store values to send */
char str_val[10];
/* close any connection before send a new request.
This will free the socket on the WiFi shield */
client.stop();
/* Read the sensor from the analog input*/
float sensor_value = analogRead(A0);
/*---- Transforms the values of the sensors to char type -----*/
/* 4 is mininum width, 2 is precision; float value is copied onto str_val*/
dtostrf(sensor_value, 4, 2, str_val);
/* Builds the body to be send into the request*/
sprintf(body, "{\"%s\":%s}", VARIABLE_LABEL, str_val);
/* Builds the HTTP request to be POST */
sprintf(data, "POST /api/v1.6/devices/%s", DEVICE_LABEL);
sprintf(data, "%s HTTP/1.1\r\n", data);
sprintf(data, "%sHost: industrial.api.ubidots.com\r\n", data);
sprintf(data, "%sUser-Agent: %s/%s\r\n", data, USER_AGENT, VERSION);
sprintf(data, "%sX-Auth-Token: %s\r\n", data, TOKEN);
sprintf(data, "%sConnection: close\r\n", data);
sprintf(data, "%sContent-Type: application/json\r\n", data);
sprintf(data, "%sContent-Length: %d\r\n\r\n", data, dataLen(body));
sprintf(data, "%s%s\r\n\r\n", data, body);
/* if there's a successful connection */
if (client.connect(HTTPSERVER, 80)) {
Serial.println("connecting...");
/* send the HTTP POST request */
client.print(data);
/* note the time that the connection was made */
lastConnectionTime = millis();
}
else {
/* if you couldn't make a connection */
Serial.println("connection failed");
}
/* Free memory */
free(data);
free(body);
}
2. Next, Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon, press it to verify your code.
3. Then, Upload the code into your Feather board. To do this, choose the "right-arrow"icon beside the check mark icon.
4. To verify the connectivity of the hardware with the internet, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of our Arduino IDE to see the connectivity logs.
5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, locate the device called "feather-ethernet" and visualize your data.
4. Summary
With this simple tutorial we are able to POST and publish data to Ubidots with the ease of the Arduino IDE and the Adafruit FeatherWing. If your desire send more than one variable to Ubidots, reference Ubidots REST API to learn how to build the request properly. :)
Now its time to create Ubidots Dashboards to visualize your data and deploy your internet connected monitoring solution!
Other readers have also found useful...