Body Scanner Using HC-SR04 and ESP8266
Show the insides of a body scan based on distance measurement.
Devices & Components
1
Ultrasonic Sensor - HC-SR04 (Generic)
1
NodeMCU ESP8266 Breakout Board
Project description
Code
Display images with distance data sent via MQTT
python
Display images with distance data sent via MQTT
1###demo code provided by Steve Cope at www.steves-internet-guide.com 2##email steve@steves-internet-guide.com 3###Free to use for any purpose 4""" 5Passwords demo code 6""" 7 8import paho.mqtt.client as mqtt #import the client1 9#import Pygame and system modules 10import pygame, sys 11#important all modules 12from pygame.locals import * 13from os import listdir 14from os.path import isfile, join 15import glob 16import time 17import re 18# initialize this Pygame. 19pygame.init() 20#img2 = pygame.image.load("images/out2.jpg") 21 22#Create and set The display area size 23screen = pygame.display.set_mode((640, 480)) 24 25#create a variable with the colour black. 26width = 260 27BLACK = (0, 0, 0) 28time.sleep(1) #allow sonar to get going 29print ("Start") 30 31#set Pygames background colour to black 32screen.fill(BLACK) 33 34#filenames = [img for img in glob.glob("images/*.jpg")] #Load the filenames into a list use for .jpg 35filenames = [img for img in glob.glob("images/*.jpg")] #Load the filenames into a list use for .png 36filenames.sort(key=lambda f: int(''.join(filter(str.isdigit, f)))) #Sort the file list numerically 37print (len(filenames)) 38#while True: 39QOS1=1 40QOS2=1 41CLEAN_SESSION=True 42broker="192.168.1.12" 43 44def on_disconnect(client, userdata, flags, rc=0): 45 m="DisConnected flags"+"result code "+str(rc) 46 print(m) 47 48def on_connect(client, userdata, flags, rc): 49 print("distance ",str(flags),"result code ",str(rc)) 50 51def on_message(client, userdata, message): 52 print("Distance is =",str(message.payload.decode("utf-8"))) 53 54 print (str(message.payload.decode("utf-8"))) 55 if int(str(message.payload.decode("utf-8"))) <= 147: #Only interested in 144 images 56 required_image = int(str(message.payload.decode("utf-8"))) 57 image_wanted = required_image 58 print ("Image number is :", image_wanted) 59 img2= pygame.image.load(filenames[image_wanted]) 60 screen.blit(img2,(130,60)) 61 pygame.display.flip() 62 #update the display 63 pygame.display.update() 64 time.sleep(0.01) 65 66 else: 67 print ("Flipping heck") 68 blank= "1.jpg" 69 screen.blit(blank,(90,60)) 70 pygame.display.flip() 71 #update the display 72 pygame.display.update() 73 client.disconnect 74 75print("creating client 1 with clean session set to",CLEAN_SESSION) 76client1 = mqtt.Client("Python1",clean_session=CLEAN_SESSION) #create new instance 77## edit code for passwords 78print("setting password") 79client1.username_pw_set(username="Your_MQTT_username",password="Your_MQTT_password") 80## 81client1.on_message=on_message #attach function to callback 82client1.on_connect=on_connect 83print("connecting to ",broker) 84client1.connect(broker) 85client1.subscribe("distance")#subscribe 86client1.loop_forever() 87time.sleep(1) 88 89client1.disconnect() 90 91client1.loop_stop() 92 93 94
Sonar sensor and MQTT
arduino
Reads Ultrasonic sensor HC-SR04 and publishes with MQTT
1#include <ESP8266WiFi.h> 2#include <Ultrasonic.h> 3#include <PubSubClient.h> 4 5 6// Update these with values suitable for your network. 7const char* ssid = "Your SSID"; //Replace with your SSID 8const char* password = "Your WIFI password"; //Replace with your WIFI password 9const char* mqtt_server = "The IP of your computer"; //Replace with the I.P of the computer 10const char* mqtt_username = "MQTT username"; //Replace with the username you used in the setup of MQTT on your computer 11const char* mqtt_password = "MQTT password";//Replace with the MQTT password you used when you set up MQTT on your computer 12const char* clientID = "espClient"; 13WiFiClient wifiClient; 14PubSubClient client(mqtt_server, 1883, wifiClient); 15long lastMsg = 100; 16// defines variables 17long duration; 18int distance; 19// Defines pins numbers for Sonar HC-SR04) 20Ultrasonic ultrasonic(16, 5); // (Trig, Echoe) 21 22void setup_wifi() { 23 delay(10); 24 // We start by connecting to a WiFi network 25 Serial.println(); 26 Serial.print("Connecting to "); 27 Serial.println(ssid); 28 WiFi.begin(ssid, password); 29 30 while (WiFi.status() != WL_CONNECTED) 31 { 32 delay(500); 33 Serial.print("."); 34 } 35 Serial.println(""); 36 Serial.println("WiFi connected"); 37 Serial.println("IP address: "); 38 Serial.println(WiFi.localIP()); 39 40 // Connect to MQTT Broker 41 // client.connect returns a boolean value to let us know if the connection was successful. 42 // If the connection is failing, make sure you are using the correct MQTT Username and Password (Setup Earlier in the Instructable) 43 if (client.connect(clientID, mqtt_username, mqtt_password)) { 44 Serial.println("Connected to MQTT Broker!"); 45 } 46 else { 47 Serial.println("Connection to MQTT Broker failed..."); 48 } 49} 50 51void reconnect() { 52 // Loop until we're reconnected 53 while (!client.connected()) { 54 Serial.print("Attempting MQTT connection..."); 55 // Attempt to connect 56 if (client.connect("arduinoClient_distance_sensor")) { 57 Serial.println("connected"); 58 } else { 59 Serial.print("failed, rc="); 60 Serial.print(client.state()); 61 Serial.println(" try again in 5 seconds"); 62 // Wait 5 seconds before retrying 63 delay(5000); 64 } 65 } 66} 67 68void setup() 69{ 70 Serial.begin(115200); 71 setup_wifi(); 72 client.setServer(mqtt_server, 1883); 73} 74 75void loop() 76{ 77 if (!client.connected()) { 78 reconnect(); 79 } 80 client.loop(); 81 //Read the distane and publish it to the computer 82 long now = millis(); 83 if (now - lastMsg > 100) { 84 lastMsg = now; 85 distance = ultrasonic.read(CM); 86 client.publish("distance", String(distance).c_str()); 87 Serial.print("Distance: "); 88 Serial.println(distance); 89 90 } 91}
Display images with distance data sent via MQTT
python
Display images with distance data sent via MQTT
1###demo code provided by Steve Cope at www.steves-internet-guide.com 2##email 3 steve@steves-internet-guide.com 4###Free to use for any purpose 5""" 6Passwords 7 demo code 8""" 9 10import paho.mqtt.client as mqtt #import the client1 11#import 12 Pygame and system modules 13import pygame, sys 14#important all modules 15from 16 pygame.locals import * 17from os import listdir 18from os.path import isfile, 19 join 20import glob 21import time 22import re 23# initialize this Pygame. 24pygame.init() 25#img2 26 = pygame.image.load("images/out2.jpg") 27 28#Create and set The display area 29 size 30screen = pygame.display.set_mode((640, 480)) 31 32#create a variable with 33 the colour black. 34width = 260 35BLACK = (0, 0, 0) 36time.sleep(1) #allow sonar 37 to get going 38print ("Start") 39 40#set Pygames background colour to black 41screen.fill(BLACK) 42 43#filenames 44 = [img for img in glob.glob("images/*.jpg")] #Load the filenames into a list use 45 for .jpg 46filenames = [img for img in glob.glob("images/*.jpg")] #Load the filenames 47 into a list use for .png 48filenames.sort(key=lambda f: int(''.join(filter(str.isdigit, 49 f)))) #Sort the file list numerically 50print (len(filenames)) 51#while True: 52QOS1=1 53QOS2=1 54CLEAN_SESSION=True 55broker="192.168.1.12" 56 57def 58 on_disconnect(client, userdata, flags, rc=0): 59 m="DisConnected flags"+"result 60 code "+str(rc) 61 print(m) 62 63def on_connect(client, userdata, flags, rc): 64 65 print("distance ",str(flags),"result code ",str(rc)) 66 67def on_message(client, 68 userdata, message): 69 print("Distance is =",str(message.payload.decode("utf-8"))) 70 71 print 72 (str(message.payload.decode("utf-8"))) 73 if int(str(message.payload.decode("utf-8"))) 74 <= 147: #Only interested in 144 images 75 required_image = int(str(message.payload.decode("utf-8"))) 76 image_wanted 77 = required_image 78 print ("Image number is :", image_wanted) 79 img2= 80 pygame.image.load(filenames[image_wanted]) 81 screen.blit(img2,(130,60)) 82 pygame.display.flip() 83 #update 84 the display 85 pygame.display.update() 86 time.sleep(0.01) 87 88 else: 89 print 90 ("Flipping heck") 91 blank= "1.jpg" 92 screen.blit(blank,(90,60)) 93 pygame.display.flip() 94 #update 95 the display 96 pygame.display.update() 97 client.disconnect 98 99print("creating 100 client 1 with clean session set to",CLEAN_SESSION) 101client1 = mqtt.Client("Python1",clean_session=CLEAN_SESSION) 102 #create new instance 103## edit code for passwords 104print("setting password") 105client1.username_pw_set(username="Your_MQTT_username",password="Your_MQTT_password") 106## 107client1.on_message=on_message 108 #attach function to callback 109client1.on_connect=on_connect 110print("connecting 111 to ",broker) 112client1.connect(broker) 113client1.subscribe("distance")#subscribe 114client1.loop_forever() 115time.sleep(1) 116 117client1.disconnect() 118 119client1.loop_stop() 120 121 122
Downloadable files
Ultrasonic wiring
https://github.com/ErickSimoes/Ultrasonic/raw/master/extras/HC-SR04-with-Arduino.jpg?raw=true
Ultrasonic wiring
https://github.com/ErickSimoes/Ultrasonic/raw/master/extras/HC-SR04-with-Arduino.jpg?raw=true
Comments
Only logged in users can leave comments