agentsimon
Published © LGPL

Body Scanner Using HC-SR04 and ESP8266

Show the insides of a body scan based on distance measurement.

IntermediateFull instructions provided2,789
Body Scanner Using HC-SR04 and ESP8266

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1

Story

Read more

Schematics

Ultrasonic wiring

Code

Sonar sensor and MQTT

Arduino
Reads Ultrasonic sensor HC-SR04 and publishes with MQTT
#include <ESP8266WiFi.h>
#include <Ultrasonic.h>
#include <PubSubClient.h>


// Update these with values suitable for your network.
const char* ssid = "Your SSID"; //Replace with your SSID
const char* password = "Your WIFI password"; //Replace with your WIFI password
const char* mqtt_server = "The IP of your computer"; //Replace with the I.P of the computer
const char* mqtt_username = "MQTT username"; //Replace with the username you used in the setup of MQTT on your computer
const char* mqtt_password = "MQTT password";//Replace with the MQTT password you used when you set up MQTT on your computer
const char* clientID = "espClient";
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient);
long lastMsg = 100;
// defines variables
long duration;
int distance;
// Defines pins numbers  for Sonar HC-SR04)
Ultrasonic ultrasonic(16, 5); // (Trig, Echoe)

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Connect to MQTT Broker
  // client.connect returns a boolean value to let us know if the connection was successful.
  // If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable)
  if (client.connect(clientID, mqtt_username, mqtt_password)) {
    Serial.println("Connected to MQTT Broker!");
  }
  else {
    Serial.println("Connection to MQTT Broker failed...");
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient_distance_sensor")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
 
void setup()
{
  Serial.begin(115200);
  setup_wifi(); 
  client.setServer(mqtt_server, 1883);  
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
  //Read the distane and publish it to the computer
  long now = millis();
  if (now - lastMsg > 100) {
    lastMsg = now;
    distance = ultrasonic.read(CM);
    client.publish("distance", String(distance).c_str());
    Serial.print("Distance: ");
    Serial.println(distance);
    
  }
}

Display images with distance data sent via MQTT

Python
Display images with distance data sent via MQTT
###demo code provided by Steve Cope at www.steves-internet-guide.com
##email steve@steves-internet-guide.com
###Free to use for any purpose
"""
Passwords demo code
"""

import paho.mqtt.client as mqtt  #import the client1
#import Pygame and system modules
import pygame, sys
#important all  modules
from pygame.locals import *
from os import listdir
from os.path import isfile, join
import glob
import time
import re
# initialize this Pygame.
pygame.init()
#img2 = pygame.image.load("images/out2.jpg")

#Create and set The display area size
screen = pygame.display.set_mode((640, 480))

#create a variable with the colour black.
width = 260
BLACK = (0, 0, 0)
time.sleep(1) #allow sonar to get going
print ("Start")

#set Pygames background colour to black
screen.fill(BLACK)

#filenames = [img for img in glob.glob("images/*.jpg")] #Load the filenames into a list use for .jpg
filenames = [img for img in glob.glob("images/*.jpg")] #Load the filenames into a list use for .png
filenames.sort(key=lambda f: int(''.join(filter(str.isdigit, f)))) #Sort the file list numerically
print (len(filenames))
#while True:
QOS1=1
QOS2=1
CLEAN_SESSION=True
broker="192.168.1.12"

def on_disconnect(client, userdata, flags, rc=0):
    m="DisConnected flags"+"result code "+str(rc)
    print(m)

def on_connect(client, userdata, flags, rc):
    print("distance ",str(flags),"result code ",str(rc))

def on_message(client, userdata, message):
	print("Distance is =",str(message.payload.decode("utf-8")))

	print (str(message.payload.decode("utf-8")))
	if int(str(message.payload.decode("utf-8"))) <= 147: #Only interested in 144 images
		required_image = int(str(message.payload.decode("utf-8")))
		image_wanted = required_image 
		print ("Image number is :", image_wanted)
		img2= pygame.image.load(filenames[image_wanted])
		screen.blit(img2,(130,60))
		pygame.display.flip()
		#update the display
		pygame.display.update()
		time.sleep(0.01)
		
	else:
		print ("Flipping heck")
		blank= "1.jpg"
		screen.blit(blank,(90,60))
		pygame.display.flip()
		#update the display
		pygame.display.update()
	client.disconnect

print("creating client 1 with clean session set to",CLEAN_SESSION)
client1 = mqtt.Client("Python1",clean_session=CLEAN_SESSION)    #create new instance
## edit code for passwords
print("setting  password")
client1.username_pw_set(username="Your_MQTT_username",password="Your_MQTT_password")
##
client1.on_message=on_message        #attach function to callback
client1.on_connect=on_connect
print("connecting to ",broker)
client1.connect(broker)
client1.subscribe("distance")#subscribe
client1.loop_forever()
time.sleep(1)

client1.disconnect()

client1.loop_stop()

Credits

agentsimon

agentsimon

3 projects • 0 followers

Comments