Skip to first unread message

Ade Tuck

unread,
Aug 23, 2016, 11:38:38 AM8/23/16
to mitappinv...@googlegroups.com
Hi,

I have a HP-SR04 distance sensor connected to an ArdaFruit Huzzah ESP8266 Wi-Fi board.
I have the WiFi board reading the distance sensor data ok and displaying it on the screen,
I would like to squirt those distance numbers into App Inventor
Here is my Arduino code, i am just a beginner so its the last line that (I think) that sends the data out.

Can someone create or explain what App inventor blocks i need to use to read this distance data on my phone please ?
Ignore the LED stuff, its just old code and i was worried i might break something if i delete it... NooB here :)

Thanks 
Adrian 

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>

const char* ssid = "***";
const char* password = "**"

ESP8266WebServer server(80);

const int led = 0;
const int led2 = 2;
const int trigPin = 12;
const int echoPin = 15 ;
long duration;
int distance;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/bold", "HELLO!");
  digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);

  server.on("/RON", [](){
   digitalWrite(led, 1);
   server.send(200, "text/plain", "Red LED On");    
  });

  server.on("/ROFF", [](){
   digitalWrite(led, 0);
   server.send(200, "text/plain", "Red LED Off");   
  });

  server.on("/BON", [](){
   digitalWrite(led2, 1);
   server.send(200, "text/plain", "Blue LED On");    
  });

  server.on("/BOFF", [](){
   digitalWrite(led2, 0);
   server.send(200, "text/plain", "Blue LED Off");   
  });
  
  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop(void){
  server.handleClient();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance); 
server.send(distance);
}  

Abraham Getzler

unread,
Aug 23, 2016, 3:22:50 PM8/23/16
to MIT App Inventor Forum
https://groups.google.com/forum/#!searchin/mitappinventortest/ESP8266

Try the second link, from Robert Holt, for short code.

ABG


Ade Tuck

unread,
Aug 24, 2016, 11:16:38 AM8/24/16
to MIT App Inventor Forum
Thanks for the link Abraham,
So is my above Arduino code ok ? 
Will it keep sending out distance data from the last line of code ?
Or do i need to put it in a loop ?  

I made a text box, in the App Inventor, and used the lower block of code from the link you sent me:
But when i try running the App inventor on my phone its not showing any distance data from the Huzzah board.

Any ideas ?
Don't forget I am super new at this so i might be missing some basic info.

Thanks 

Abraham Getzler

unread,
Aug 24, 2016, 1:15:22 PM8/24/16
to MIT App Inventor Forum
Easiest way to test a system is one part at a time.

Can you query the ESP device from a web browser?
Does the returned Web output look reasonable?

ABG
Reply all
Reply to author
Forward
0 new messages