【问题标题】:Connection problem of ESP8266 ESP-01 arduino uno to cayenneESP8266 ESP-01 arduino uno到cayenne的连接问题
【发布时间】:2020-04-19 10:04:18
【问题描述】:

我正在使用 ESP8266 Wi-Fi shield 将 Arduino 连接到 cayenne 服务器。但是,一旦将芯片连接到接入点,它就会变热,有时会滞后。之后,它显示了一些消息,但 Cayenne 服务器没有连接到 Arduino。每个小部件都无法操作(单击按钮时无响应,温度值不变等)。任何人都可以解决这个问题吗?这对我的课程作业很重要。我正在使用 Arduino Uno 和 ESP8266 ESP-01 芯片。我们尝试了不同样式的代码将数据上传到 cayenne 服务器,但它们不起作用(以下包括 3 个版本)

Finale 1 Version(Cayenne out) :

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <virtuabotixRTC.h>

//Real+Virtual
#define LED_PIN 5
#define temp_sensor 4
#define EspSerial Serial

//Virtual
#define Power_sensor 9
#define DayPower 10
#define MonthPower 11
#define LED_PWM 12

//SSID

char ssid[] = "TP-LINK_MWNg";
char password[] = "mwngpass";

char username[] = "743674368676328742somenumbers";
char mqtt_password[] = "743674368676328742somenumbers";
char client_id[] = "743674368676328742somenumbers";

ESP8266 wifi(&EspSerial);

//Temperature setup
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

//Power sensor
const int analogInPin = A0;
int sensorValue = 0;        // value read from the pot
float MAXValue = 0;        // value output to the PWM (analog out)
float Power = 0;
int PWM = 0;

//Time
int curSec = 0;
int curMin = 0;
int curHour = 0;
int curDay = 0;
virtuabotixRTC myRTC(6, 7, 8);
unsigned PowerInDay = 0;
unsigned PowerInMonth = 0;

void setup() {
  // put your setup code here, to run once:
  sensors.begin();
  EspSerial.begin(115200);
  delay(10);
  Cayenne.begin(username, mqtt_password, client_id, wifi, ssid, password);

  pinMode(LED_PIN, OUTPUT);
  analogWrite(LED_PIN, PWM);

  curSec = myRTC.seconds;
  curMin = myRTC.minutes;
  curHour = myRTC.hours;
  curDay = myRTC.dayofmonth;

  //test

}

void loop() {
  Cayenne.loop();
  myRTC.updateTime();
  if ( curSec != myRTC.seconds ) {
    PowerInDay += Power;
    curSec = myRTC.seconds;
  }
  if (curDay != myRTC.dayofmonth) {
    PowerInMonth += PowerInDay;
    PowerInDay = 0;
    curDay = myRTC.dayofmonth;
  }
  if (curDay > myRTC.dayofmonth) {
    PowerInMonth = 0;
  }
  Serial.print(myRTC.dayofmonth);
  Serial.print('/');
  Serial.print(myRTC.month);
  Serial.print('/');
  Serial.print(myRTC.year);
  Serial.print('/');
  Serial.print(' ');
  Serial.print(myRTC.hours);
  Serial.print(':');
  Serial.print(myRTC.minutes);
  Serial.print(':');
  Serial.print(myRTC.seconds);
  Serial.print('\n');
  Serial.print(PowerInDay);
  Serial.print(' ');
  Serial.print(PowerInMonth);
  Serial.print('\n');

}

CAYENNE_OUT(temp_sensor)
{
  sensors.requestTemperatures();
  Cayenne.celsiusWrite(temp_sensor, sensors.getTempCByIndex(0));
}

CAYENNE_OUT(Power_sensor)
{
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  if (sensorValue > 0)
    MAXValue = sensorValue * 0.00007307;

  Power = MAXValue * ((float)PWM / (float)255) * 3.3 * ((float)PWM / (float)255);
  // change the analog out value:/

  // print the results to the serial monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue); //RAW value from analog read :)
  Serial.print("\t output = ");
  Serial.println(MAXValue * ((float)PWM / (float)255), 3); //Output the current
  Serial.print("Power =");
  Serial.println(Power, 3);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(50);
  Cayenne.virtualWrite(Power_sensor, Power, "pow", "w");
}

CAYENNE_OUT(DayPower) {
  Cayenne.virtualWrite(DayPower, PowerInDay, "pow", "w");
}

CAYENNE_OUT(MonthPower) {
  Cayenne.virtualWrite(MonthPower, PowerInMonth, "pow", "w");
}


CAYENNE_IN(LED_PIN)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1) {
    PWM = 255;
    digitalWrite(LED_PIN, HIGH);
  } else {
    PWM = 0;
    digitalWrite(LED_PIN, LOW);
  }
  //digitalWrite(Relay, HIGH);
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

CAYENNE_IN(LED_PWM)
{
  int value = getValue.asInt(); // 0 to 255
  PWM = value;
  analogWrite(LED_PIN, value);
}

Finale 2 版本(Cayenne out_Default):

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <virtuabotixRTC.h>

//Real+Virtual
#define LED_PIN 5
#define temp_sensor 4
#define EspSerial Serial

//Virtual
#define Power_sensor 9
#define DayPower 10
#define MonthPower 11
#define LED_PWM 12

//SSID

char ssid[] = "TP-LINK_MWNg";
char password[] = "mwngpass";

char username[] = "743674368676328742somenumbers";
char mqtt_password[] = "743674368676328742somenumbers";
char client_id[] = "743674368676328742somenumbers";

ESP8266 wifi(&EspSerial);

//Temperature setup
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

//Power sensor
const int analogInPin = A0;
int sensorValue = 0;        // value read from the pot
float MAXValue = 0;        // value output to the PWM (analog out)
float Power = 0;
int PWM = 0;

//Time
int curSec = 0;
int curMin = 0;
int curHour = 0;
int curDay = 0;
virtuabotixRTC myRTC(6, 7, 8);
unsigned PowerInDay = 0;
unsigned PowerInMonth = 0;

void setup() {
  // put your setup code here, to run once:
  sensors.begin();
  EspSerial.begin(115200);
  delay(10);
  Cayenne.begin(username, mqtt_password, client_id, wifi, ssid, password);

  pinMode(LED_PIN, OUTPUT);
  analogWrite(LED_PIN, PWM);

  curSec = myRTC.seconds;
  curMin = myRTC.minutes;
  curHour = myRTC.hours;
  curDay = myRTC.dayofmonth;

  //test

}

void loop() {
  Cayenne.loop();
  myRTC.updateTime();
  if ( curSec != myRTC.seconds ) {
    PowerInDay += Power;
    curSec = myRTC.seconds;
  }
  if (curDay != myRTC.dayofmonth) {
    PowerInMonth += PowerInDay;
    PowerInDay = 0;
    curDay = myRTC.dayofmonth;
  }
  if (curDay > myRTC.dayofmonth) {
    PowerInMonth = 0;
  }
  Serial.print(myRTC.dayofmonth);
  Serial.print('/');
  Serial.print(myRTC.month);
  Serial.print('/');
  Serial.print(myRTC.year);
  Serial.print('/');
  Serial.print(' ');
  Serial.print(myRTC.hours);
  Serial.print(':');
  Serial.print(myRTC.minutes);
  Serial.print(':');
  Serial.print(myRTC.seconds);
  Serial.print('\n');
  Serial.print(PowerInDay);
  Serial.print(' ');
  Serial.print(PowerInMonth);
  Serial.print('\n');

}

  CAYENNE_OUT_DEFAULT()
  {
  //Temp sensor-----------------------------------------
  sensors.requestTemperatures();
  Cayenne.celsiusWrite(temp_sensor, sensors.getTempCByIndex(0));
  //Temp sensor-----------------------------------------

  //Power sensor-------------------------------------------------
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  if (sensorValue > 0)
    MAXValue = sensorValue * 0.00007307;

  Power = MAXValue * ((float)PWM / (float)255) * 3.3 * ((float)PWM / (float)255);
  // change the analog out value:/

  // print the results to the serial monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue); //RAW value from analog read :)
  Serial.print("\t output = ");
  Serial.println(MAXValue * ((float)PWM / (float)255), 3); //Output the current
  Serial.print("Power =");
  Serial.println(Power, 3);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(50);
  Cayenne.virtualWrite(Power_sensor, Power, "pow", "w");
  //Power sensor-------------------------------------------------

  //Power Day-------------------------------------------------
  Cayenne.virtualWrite(DayPower, PowerInDay, "pow", "w");

  //Power Month-------------------------------------------------
  Cayenne.virtualWrite(MonthPower, PowerInMonth, "pow", "w");

  }

CAYENNE_IN(LED_PIN)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1) {
    PWM = 255;
    digitalWrite(LED_PIN, HIGH);
  } else {
    PWM = 0;
    digitalWrite(LED_PIN, LOW);
  }
  //digitalWrite(Relay, HIGH);
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

CAYENNE_IN(LED_PWM)
{
  int value = getValue.asInt(); // 0 to 255
  PWM = value;
  analogWrite(LED_PIN, value);
}

Finale 3 版本(发送时差数据):

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266Shield.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <virtuabotixRTC.h>

//Real+Virtual
#define LED_PIN 5
#define temp_sensor 4
#define EspSerial Serial

//Virtual
#define Power_sensor 9
#define DayPower 10
#define MonthPower 11
#define LED_PWM 12

//SSID

char ssid[] = "TP-LINK_MWNg";
char password[] = "mwngpass";

char username[] = "743674368676328742somenumbers";
char mqtt_password[] = "743674368676328742somenumbers";
char client_id[] = "743674368676328742somenumbers";

ESP8266 wifi(&EspSerial);

//Temperature setup
OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

//Power sensor
const int analogInPin = A0;
int sensorValue = 0;        // value read from the pot
float MAXValue = 0;        // value output to the PWM (analog out)
float Power = 0;
int PWM = 0;

//Time
int curSec = 0;
int curMin = 0;
int curHour = 0;
int curDay = 0;
virtuabotixRTC myRTC(6, 7, 8);
unsigned PowerInDay = 0;
unsigned PowerInMonth = 0;
int lastMillis = 0;
void setup() {
  // put your setup code here, to run once:
  sensors.begin();
  EspSerial.begin(115200);
  delay(10);
  Cayenne.begin(username, mqtt_password, client_id, wifi, ssid, password);

  pinMode(LED_PIN, OUTPUT);
  analogWrite(LED_PIN, PWM);

  curSec = myRTC.seconds;
  curMin = myRTC.minutes;
  curHour = myRTC.hours;
  curDay = myRTC.dayofmonth;

  //test

}

void loop() {
  Cayenne.loop();
  myRTC.updateTime();
  if ( curSec != myRTC.seconds ) {
    PowerInDay += Power;
    curSec = myRTC.seconds;
  }
  if (curDay != myRTC.dayofmonth) {
    PowerInMonth += PowerInDay;
    PowerInDay = 0;
    curDay = myRTC.dayofmonth;
  }
  if (curDay > myRTC.dayofmonth) {
    PowerInMonth = 0;
  }
//  Serial.print(myRTC.dayofmonth);
//  Serial.print('/');
//  Serial.print(myRTC.month);
//  Serial.print('/');
//  Serial.print(myRTC.year);
//  Serial.print('/');
//  Serial.print(' ');
//  Serial.print(myRTC.hours);
//  Serial.print(':');
//  Serial.print(myRTC.minutes);
//  Serial.print(':');
//  Serial.print(myRTC.seconds);
//  Serial.print('\n');
//  Serial.print(PowerInDay);
//  Serial.print(' ');
//  Serial.print(PowerInMonth);
//  Serial.print('\n');

 if(millis() - lastMillis > 10000 && millis() - lastMillis < 20000 ) {//Send data between 10 - 20 seconds
   //Temp sensor-----------------------------------------
  sensors.requestTemperatures();
  Cayenne.celsiusWrite(temp_sensor, sensors.getTempCByIndex(0));
  //Temp sensor-----------------------------------------
   }
 if(millis() - lastMillis > 20000 && millis() - lastMillis < 30000 ) {//Send data between 20 - 30 seconds
      //Power sensor-------------------------------------------------
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  if (sensorValue > 0)
    MAXValue = sensorValue * 0.00007307;

  Power = MAXValue * ((float)PWM / (float)255) * 3.3 * ((float)PWM / (float)255);
  // change the analog out value:/

  // print the results to the serial monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue); //RAW value from analog read :)
  Serial.print("\t output = ");
  Serial.println(MAXValue * ((float)PWM / (float)255), 3); //Output the current
  Serial.print("Power =");
  Serial.println(Power, 3);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(50);
  Cayenne.virtualWrite(Power_sensor, Power, "pow", "w");
  //Power sensor-------------------------------------------------

   }
 if(millis() - lastMillis > 30000 && millis() - lastMillis < 40000 ) {//Send data between 30 - 40 seconds

      //Power Day-------------------------------------------------
  Cayenne.virtualWrite(DayPower, PowerInDay, "pow", "w");
   }
 if(millis() - lastMillis > 40000 && millis() - lastMillis < 50000 ) {//Send data between 40 - 50 seconds

       //Power Month-------------------------------------------------
  Cayenne.virtualWrite(MonthPower, PowerInMonth, "pow", "w");
  lastMillis = millis();
   }

}

CAYENNE_IN(LED_PIN)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1) {
    PWM = 255;
    digitalWrite(LED_PIN, HIGH);
  } else {
    PWM = 0;
    digitalWrite(LED_PIN, LOW);
  }
  //digitalWrite(Relay, HIGH);
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}
CAYENNE_IN(LED_PWM)
{
  int value = getValue.asInt(); // 0 to 255
  PWM = value;
  analogWrite(LED_PIN, value);
}

串行监视器消息:

AT
ATE0
AT+CIPMUX=1
AT+CWMODE?
AT+CWJAP="TP-LINK_MWNg","mwngpass"
[5618] Connected to WiFi
[5619] Connecting to mqtt.mydevices.com:1883
AT+CIPSTART=1,"TCP","mqtt.mydevices.com",1883
[15636] Network connect failed
AT+CIPSTART=1,"TCP","mqtt.mydevices.com",1883
AT+CIPSEND=1,40
MQIsdp
9c6b2/things/743674368676328742somenumbersAT+CIPSEND=1,16
e7f5ba423/cmd/+
AT+CIPSEND=1,40
AT+CIPSEND=1,40
AT+CIPSEND=1,40
AT+CIPSEND=1,40
1i Zv1/743674368676328742somenumbersAT+CIPSEND=1,40
6b2/things/743674368676328742somenumbersAT+CIPSEND=1,27
f5ba423/data/4temp,c=28.125sensor = 0     output = 0.000
Power =0.000
AT+CIPSEND=1,40
1g

【问题讨论】:

  • 我从问题中删除了您的密码。是 ESP8266 防护罩(如果是,请添加产品链接)还是普通的 esp8266 模块
  • 非常感谢。我没有注意到隐私问题。我使用连接到 Arduino UNO 的 ESP8266 ESP-01 模块。这是一个来自淘宝捆绑包的模块。如果你还不清楚我可以给你链接
  • 是的,请提供链接或发布您的组合的照片谢谢
  • 欢迎来到 Stack Overflow!您似乎发布了敏感/私人信息。请重置您的密码和/或撤销 API 密钥和令牌,因为它们在互联网上发布时被视为已泄露。

标签: arduino wifi mqtt iot arduino-uno


【解决方案1】:

如果您的模块变热,则可能是硬件问题。我猜您为 esp 模块使用了 5V,这很糟糕,因为它只需要 3.3V。它可能会在短时间内(片刻)在 5V 电压下存活,但不要延长你的运气。所以你必须转换 5V 到 3.3V 电平转换器。您不能使用 Arduino 3.3V 输出引脚,因为它无法提供 ESP 所需的功率(最高 250mA),而 Arduino (UNO) 3.3V 引脚上的最大 50mA。

原理图简要说明:

  • ESP 的 VCC 引脚由稳压器的 3.3V 输出引脚供电(图中的 AMS1117)。
  • 10uF 电容连接到输出引脚以稳定稳压器。
  • CH_PD 引脚也必须连接到 3.3V。
  • GND 引脚明显接地。
  • ESP 的 TXD 引脚可以直接连接到 Arduino 的 RX 引脚(在引脚 6 上模拟)。
  • ESP 的 RXD 引脚通过电平转换器连接到 Arduino 的 TX 引脚(在引脚 7 上模拟)。

最后两个可以更改,具体取决于您使用的是硬件还是软件串行
编辑
当您使用 rtc 时,更新每个循环的时间是没有意义的,这会使您的连接崩溃。要么每隔 x 小时执行一次,并使用 if/else 确保 nochyenne loop() 正在运行。每隔 x millis() 同步一次时间是没有意义的

【讨论】:

  • 其实我只是用Arduino中的3.3V接口。当我只编写 2 个组件的代码来与 Cayenne 服务器通信时,它工作正常(功率传感器和接收 Cayenne 中的滑块数据)。但是,当我添加更多组件时它开始升温
  • 添加更多组件 软件还是硬件?如果它的硬件监视电流消耗,如果它是软的,那么你的 ESP 可能有缺陷 - 它使用大约 80mA 并且在 AP 模式下变得非常温暖(但在规格范围内)。所以你再次按照我的图形连接,如果没有,你将继续错开在黑暗中
  • 我会尝试购买这些组件。非常感谢您的详细解决方案。
  • 另外我想问一下是否可以使用其他3.3V稳压器但不能使用AMS1117?因为我在当地商店找不到它。
  • 是的,稳压器的类型可以是任何类似的类型,您也可以为此使用电平转换器模块
猜你喜欢
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2019-08-20
  • 2023-03-24
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多