ESP8266 ESP-01 arduino uno与cayenne的连接问题

问题描述 投票:-1回答:1

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

Finale 1版本(Cayenne退出:

#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
arduino wifi mqtt iot arduino-uno
1个回答
1
投票

如果您的模块变热,可能是硬件问题。我想您为esp模块使用5V,这很糟糕,因为它仅需要3.3V。它可能会在5V电压下短时间(一会儿)幸存下来,但不会延长运气。因此,您必须将5V或3.3V电平转换器转换。您无法使用Arduino 3.3V输出引脚,因为它无法提供ESP所需的功率(最高250mA),而不能提供Arduino(UNO)3.3V引脚上的最大50mA的电流。

enter image description here

示意图简要说明:

  • ESP的VCC引脚由稳压器的3.3V输出引脚供电(图中AMS1117)。
  • 10uF电容器连接到输出引脚,以稳定稳压器。
  • CH_PD引脚也必须连接到3.3V。
  • GND引脚显然已接地。
  • ESP的TXD引脚可以直接连接到Arduino的RX引脚(在引脚6上仿真。)>
  • ESP的RXD引脚通过电平转换器连接到Arduino的TX引脚(在引脚7上模拟)。
  • 最后两个可以更改,具体取决于您使用的是硬件还是软件序列EDIT

使用rtc时,没有必要更新每个循环的时间,这将使您的连接崩溃。每隔x个小时执行一次,并确保是否运行nochyenne loop()。在每种情况下,每x millis()同步时间都是没有意义的,对于您的情况而言,我每天要去一次]
© www.soinside.com 2019 - 2024. All rights reserved.