你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

将Adafruit Ethernet FeatherWing连接到Ubidots

[复制链接]
点点&木木 发布时间:2019-3-11 17:56
本帖最后由 点点&木木 于 2019-4-13 09:42 编辑

介绍
该Adafruit的以太网FeatherWing是与有线和可靠的网络连接永久安装的完美选择。我们最喜欢的以太网FeatherWing是它可以与整个Feather板系列配合使用,并且可以简单地管理以高数据速率运行多种不同的应用程序。

在下面的教程中,Ubidots将演示如何使用ArduinoIDE设置和编程Adafruit以太网FeatherWing,以使用Ubidots可视化和解释数据。

1.jpg




要求  
AdafruitEthernet FeatherWing
任何AdafruitFeatherWing
Arduino IDE1.8.2或更高
Ubidots帐户 - 教育许可证- 营业执照


步骤1.使用您的设备设置ArduinoIDE
Adafruit以太网FeatherWing可以与任何Feather board一起使用,下面提供的示例代码是公开设计的,以适应该系列,因此请自由选择您的Feather硬件。


a)图书馆安装:

转到Adafruit库存储库以下载Ethernet2库。要下载库,请单击名为“ 克隆或下载” 的绿色按钮,然后选择“ 下载ZIP ”。
现在回到Arduino IDE,单击Sketch -> Include Library - > Add .ZIP Library 并选择Ethernet2-master 的.ZIP文件,然后选择“ Accept ”或“ Choose ”。一旦库成功,您将收到消息“ 库已添加到您的库 ”。  

步骤2.硬件设置
将以太网Feather插入Feather board。
将以太网电缆的一端连接到以太网FeatherWing,另一端连接到集线器,路由器或交换机。

第3步。编程
使用以下示例代码,我们将从Feather板的A0引脚发布的ANALOG读数发送到Ubidots:
要将您的第一个值发布到Ubidots,请打开Arduino IDE并粘贴下面的代码。粘贴代码后,您需要分配个人独特的Ubidots TOKEN。

/********************************
* 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 = "things.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 != '\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: things.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);
}


验证Arduino IDE中的代码。为此,在我们的ArduinoIDE的左上角,您将看到“ Check Mark ”图标,按下它以验证您的代码。
将代码上传到您的羽毛板。为此,请选中复选标记图标旁边的“ 右箭头” 图标。


要验证设备的连接性,请通过选择ArduinoIDE右上角的“放大镜”图标打开串行监视器。
在Ubidots中确认您的数据。现在,您应该在Ubidots帐户中看到已发布的数据,找到名为“ feather-ethernet ”的设备并可视化您的数据。



2.jpg


结果
通过这个简单的教程,我们可以轻松地将数据发布到Ubidots并使用Arduino IDE和Adafruit FeatherWing。如果您希望向Ubidots发送多个变量,请参考Ubidots REST API以了解如何正确构建请求。
现在是时候创建UbidotsDashboards来可视化您的数据并部署您的互联网连接监控解决方案!  

代码
/********************************
* Libraries included
*******************************/
#include <SPI.h>
#include <Ethernet2.h>

/********************************
* Constants and objects
*******************************/

/* Enter a MAC address for your controllerbelow.
   Newer Ethernet shields have aMAC address printed on a sticker on the shield */
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF,0xFE, 0xED };

/* Set the static IP address to use if theDHCP fails to assign */
IPAddress ip(192, 168, 0, 177);

/* Initialize the Ethernet client library
   with the IP address and portof 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 theserver, in milliseconds
const unsigned long postingInterval = 10L *1000L; // delay between updates, in milliseconds
// the "L" is needed to use longtype 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 andm0
  #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 ="things.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 thevariable
*/
int dataLen(char* variable) {
  uint8_t dataLen = 0;
  for (int i = 0; i <= 250; i++) {
    if (variable != '\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 andwait for port to open */
  Serial.begin(115200);
  delay(1000);
  Serial.println("\nSettingup...");

  Ethernet.init(WIZ_CS);

  /* give the ethernet module time toboot up */
  delay(1000);

  /* start the Ethernet connection */
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failedto configure Ethernet using DHCP");
    /* no point in carrying on,so do nothing forevermore:
       try to congifureusing IP address instead of DHCP */
    Ethernet.begin(mac, ip);
  }

  /* print the Ethernet board/shield'sIP address */
  Serial.print("My IP address:");
  Serial.println(Ethernet.localIP());
}

void loop() {
  /* if there's incoming data from thenet connection.
     send it out the serialport.  This is for debugging
     purposes only */
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  /* if ten seconds have passed sinceyour last connection,
     then connect again andsend data */
  if (millis() - lastConnectionTime> postingInterval) {
    httpRequest();
  }

}

/* this method makes a HTTP connection tothe 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 senda new request.
     This will free thesocket on the WiFi shield */
  client.stop();

  /* Read the sensor from the analoginput*/
  float sensor_value = analogRead(A0);

  /*---- Transforms the values of thesensors to char type -----*/
  /* 4 is mininum width, 2 isprecision; float value is copied onto str_val*/
  dtostrf(sensor_value, 4, 2,str_val);

  /* Builds the body to be send intothe request*/
  sprintf(body,"{\"%s\":%s}", VARIABLE_LABEL, str_val);

  /* Builds the HTTP request to bePOST */
  sprintf(data, "POST/api/v1.6/devices/%s", DEVICE_LABEL);
  sprintf(data, "%sHTTP/1.1\r\n", data);
  sprintf(data, "%sHost:things.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 successfulconnection */
  if (client.connect(HTTPSERVER, 80)){
   Serial.println("connecting...");
    /* send the HTTP POST request*/
    client.print(data);
    /* note the time that theconnection was made */
    lastConnectionTime =millis();
  }
  else {
    /* if you couldn't make aconnection */
   Serial.println("connection failed");
  }

  /* Free memory */
  free(data);
  free(body);
}

相关代码
adafruit /Ethernet2--- Ethernet2-master.zip (65.1 KB, 下载次数: 0)
收藏 评论0 发布时间:2019-3-11 17:56

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版