정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

Thinkspeak과 아두이노 esp8266사용할 때 초음파센서값이 0이 나오는 이유가 궁금합니다.
yoon423 | 2019-03-29

보드는 esp8266사용하고있고, 와이파이까지 연결은 다 되서 thinkspeak에 그래프는 성공적으로 그려졌는데

오렌지보드에서는 잘 되던 초음파센서가 자꾸 값이 0으로만 나옵니다.

코딩은 아래와 같이 작성했고, 5v어뎁터도 꽂았고, 빵판과 보드연결은 vcc-5v, gnd-gnd, trig-d5, echo-d4에 연결했습니다. 또 아두이노에서는 보드를 "Adafruit Feather HUZZAH ESP8266", upload Speed = 115200으로 설정했는데 센서값이 자꾸 0으로 나오는 이유는 뭘까요..?

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <PubSubClient.h>
 
const char *ssid =  "*****";        //Your Access Point or Personal Hotspot, cannot be longer than 32 characters!
const char *pass =  "*************";    //Your Access Point or Personal Hotspot password
const char* serverTS = "api.thingspeak.com";
String apiKey = "B*************N";          //Insert your Channel API Key here
//const int pingPin = 2;          //Ultrasonic connected to GPIO0
int TRIGGER = 5; //Pin D1 = TRIGGER
int ECHO = 4; //Pin D2 = ECHO
void setup()
{
  pinMode(0,OUTPUT);            //LED connected to GPIO2
  Serial.begin(115200);         //Recommended speed is 115200
  pinMode(TRIGGER,OUTPUT);
  pinMode(ECHO,INPUT);
  connectWifi();
  }
 
void loop()
{
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
 
  digitalWrite(TRIGGER, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
 
  duration = pulseIn(ECHO, HIGH);
 
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
 
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
 
  sendHeight(cm);
}
void connectWifi()
{
  Serial.print("Connecting to "+*ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
  Serial.println("");
  Serial.println("Connected");
  Serial.println("");  
}
 
long microsecondsToInches(long microseconds)
{ // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
 
void sendHeight(float cm)
{  
  WiFiClient tclient;//not to be confused with "client" in PubSub{}, and wclient for mqtt
   if (tclient.connect(serverTS, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
   //Serial.println("WiFi Client connected ");
  
   String postStr = apiKey;
   postStr += "&field1=";
   postStr += String(cm);
   postStr += "\r\n\r\n";
  
   tclient.print("POST /update HTTP/1.1\n");
   tclient.print("Host: api.thingspeak.com\n");
   tclient.print("Connection: close\n");
   tclient.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
   tclient.print("Content-Type: application/x-www-form-urlencoded\n");
   tclient.print("Content-Length: ");
   tclient.print(postStr.length());
   tclient.print("\n\n");
   tclient.print(postStr);
   delay(1000);
  
   }
 
tclient.stop();
}
이전글   |    Sharp / GP2Y1010AU0F 미세먼지 센서 data 값이 어떤 부분이랑 코드적으로... 2019-03-29
다음글   |    컴파일은 되는데 소스가 충돌이 난것 같습니다...... 2019-03-30