The use of the ESP8266 in the world of IoT

User avatar
By HayleyJ7
#70730 Hello! I'm pretty new here and to the ESP8266. I'm trying to gather distance data from an ultrasonic distance sensor and upload and graph this data in real time to ThingSpeak. The output to the Serial Monitor when I compile and run the code is this:

Code: Select allWiFi connected
IP address:
10.0.1.24
connecting to api.thingspeak.com

distance (cm) =135.06500
Posting to Thingspeak
Done Posting to Thingspeak
distance (cm) =64.54900
Posting to Thingspeak
Done Posting to Thingspeak
distance (cm) =64.12400
Posting to Thingspeak.

...etc.

This looks good and well but when I go to my ThingSpeak channel, there is no data uploaded and the graph is blank. :( I'm not super familiar with ThingSpeak syntax on Arduino IDE, so I'm not sure if this could be a source of the issue.
My hardware setup is attached, and my Arduino IDE code is as follows:

Code: Select all#include <stdlib.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>  #include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <ThingSpeak.h>    //<<<<<I HAD FORGOTTON THIS>>>>>>

int Trig    = 14;
int Echo    = 12;

//Wifi Host
const char* ssid = "spoomouse_3T";
const char* password = "merci8cake";
WiFiClient client;

//Server Information
const char* host = "api.thingspeak.com";
const int postingInterval = 30 * 1000; // post data every 30 seconds to prevent missing points

// ThingSpeak Ultrasonic Channel
const int TempchannelID = 337019;
String apiIP = "184.106.153.149";
String apiKey = "VZ4EW5LYJNHJ011C";
// write API key for your ThingSpeak Channel

//SoftwareSerial softwareSerial(rxPin, txPin);

float soundVelocity = 0.034;

void setup()
{
  //HC-SR04 setup pins
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  Serial.begin(115200);
  delay(2000);
  bool status;

  // default settings
  Serial.println("-- Default Test --");
  delay(5000);
  Serial.println();

  // Now we connect to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("");
  Serial.print("WiFi connected");
  Serial.println();
  Serial.print("IP address: ");
  Serial.println();
  Serial.print(WiFi.localIP());
  Serial.println();

  int value = 0;
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
  Serial.println();
}

///////////MAIN LOOP//////////////
void loop() {
  float distance = getDistance();
  Serial.print("distance (cm) =");
  Serial.print(distance, 5);
  Serial.print("\n");
  delay(500);

  //Create String using value
  String body = "field1=";
  body += String(distance, 2);
  //Create String using value

  Serial.print("Posting to Thingspeak");
  Serial.println();
  //Post on the channel
  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  client.print("Content-Type: application/x-www-form-urlencoded\n");
  client.print("Content-Length: ");
  client.print(body.length());
  client.print("\n\n");
  client.print(body);
  client.print("\n\n");

  delay(postingInterval);
  Serial.println("Done Posting to Thingspeak");
  // wait and then post again
  client.stop();
}

/////////FUNCTION GETDISTANCE///////////
float getDistance ()
{ //collect values
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  long highleveltime = pulseIn(Echo, HIGH);
  // Serial.print(highleveltime);
  //Serial.print("\n");
  float distance =  (float (highleveltime) * soundVelocity) / 2.0;
  return distance;
}


If there's anything you guys can see that could cause this, or even any debugging tips, I'd be grateful for your advice. Trying to get this working by tomorrow :roll: Thanks!