arduino nano与NodeMcu的接口(ESP8266)

时间:2019-03-08 06:04:43

标签: arduino nodemcu

当我将arduino nano与esp8266接口时,我的连接是: Arduino的TX连接到esp8266的RX Arduino的RX连接到esp8266的TX Arduino的GND连接到esp8266的GND 我正在使用2个传感器,它们都连接到arduino的模拟引脚(A0和A1),我希望从串行监控器收集的数据发送到Google表格和LCD,我的代码是:

#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <HTTPSRedirect.h>
//const int Pin=2;
const char* ssid = "Realme";
const char* password = "zinu1234";
const int dataPostDelay = 2000;  // 15 minutes = 15 * 60 * 1000
const char* host = "script.google.com";
const char* googleRedirHost = "script.googleusercontent.com";
const int httpsPort =  443;
HTTPSRedirect client(httpsPort);
String url ="/macros/s/AKfycbzbFJzmUWebhCW3X5mYLmu9z0M6FG2qAWvXu5b2BPaUfSPOYis/exec?";
const char* fingerprint = "F0 5C 74 77 3F 6B 25 D7 3B 66 4D 43 2F 7E BC 5B E9 28 86 AD";
SoftwareSerial ser(2,3);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
void setup() {
  lcd.begin(16,2);
  lcd.init();
  lcd.backlight();
  Serial.begin(115200);
  ser.begin(115200);
  Wire.begin(2); 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");

  bool flag = false;
  for (int i=0; i<5; i++){
    int retval = client.connect(host, httpsPort);
    if (retval == 1) {
       flag = true;
       break;
    }
    else
      Serial.println("Connection failed. Retrying...");
  }
  Serial.println("Connection Status: " + String(client.connected()));
  Serial.flush();
  if (!flag){
    Serial.print("Could not connect to server: ");
    Serial.println(host);
    Serial.println("Exiting...");
    Serial.flush();
    return;
  }
  if (client.verify(fingerprint, host)) {
    Serial.println("Certificate match.");
  } 
  else {
    Serial.println("Certificate mis-match");
  }
}
void postData(String tag, float data,float data1){
  if (!client.connected()){
    Serial.println("Connecting to client again..."); 
    client.connect(host, httpsPort);
  }
  String urlFinal = url + "tag=" + tag + "&moisture=" + String(data) + "&rain=" + String(data1);
  client.printRedir(urlFinal, host, googleRedirHost);

}
void loop() {
  float moisture_percentage;
   int sensorValue = analogRead(A0);
  moisture_percentage = (sensorValue/1023.00) * 100.00 ;
  Serial.print("Moisture Percentage = ");
  Serial.print(moisture_percentage);
  Serial.print("%\n\n");

  ser.print("Moisture Percentage = ");
  ser.print(moisture_percentage);
  ser.print("%\n\n");
  delay(500);

  int sensorReading = analogRead(A1);
   Serial.print("Rain=");
   Serial.println(sensorReading);
   ser.print("Rain=");
   ser.println(sensorReading);
   delay(1000);

   /*while (ser.available())
   {
    ser.read(moisture_percentage);
    ser.read(sensorReading);
   }*/

   postData("SensorValue",moisture_percentage,sensorReading);
   delay (dataPostDelay);
   lcd.setCursor(0,0);
  lcd.print("moisture=");
   lcd.print(moisture_percentage);
   lcd.setCursor(0,1);
   lcd.print("rain=");
   lcd.print(sensorReading);                                                                              

}

您能帮我在arduino IDE中选择端口和主板吗!!

0 个答案:

没有答案