Thursday, May 16, 2024

Caravan/Motor-home leveler part 4 - Run on your PC

For an index to all my stories click this text

As a reaction to my previous stories on the caravan/mobile-home leveler I received mail from a few readers that wanted to know if they could test the caravan/mobile-home leveler on a PC.


The previous stories on this project showed how to install the software on your mobile phone. Installing it on your PC for testing at home is actually easy to do.

The MicroPython program for the ESP8266

First thing to do is to adapt the program for the ESP8266.
In the project the program creates an access point. For using it at home you need to start the program as a wifi device.

To adapt the program, connect the ESP8266 to your computer with an USB cable and start Thonny to Edit the program.

MAKE SURE TO REMOVE THE BATTERIES BEFORE CONNECTING THE ESP8266 TO YOUR COMPUTER. NEVER USE BATTERIES AND THE USB CONNECTION AT THE SAME TIME AS THAT MIGHT DAMAGE YOUR ESP8266 OR EVEN WORSE: DAMAGE YOUR COMPUTER BEYOND REPAIR.

I will give here the part of the original program we need to change. It looks like this

'''
Caravan and Mobile Home leveling aid
http://lucstechblog.blogspot.com/

Version with http server
The x and y values are rounded
ESP is configured as Access Point
'''

from machine import Pin, I2C
import utime
import math
from lucsmpulib import init_mpu, get_data

import socket
import network

ssid = 'Caravan-Leveler'
password = '123456789'

ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid=ssid, password=password)

while ap.active() == False:
  pass

print('Connection successful')
print(ap.ifconfig())

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # esp8266
init_mpu(i2c)

 

This is the part where the libraries are imported and the Accesspoint is activated. Replace this part with the following code:

'''
Version with http server
version where the x and y values are rounded
ESP is station
'''

from machine import Pin, I2C
import utime
import math
from lucsmpulib import init_mpu, get_data

import socket
import network

ssid = "YOUR-ROUTERS-NAME"
pw = "PASSWORD"

print("Connecting to wifi...")

# wifi connection
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)
print('Waiting for connection.',end="")
while wifi.isconnected() == False:
    utime.sleep(1)
    print('', end='.')
print("")

ip = wifi.ifconfig()[0]

print("Connected with IP adress : "+ip)

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) # esp8266
init_mpu6050(i2c)


And this is what you should replace it with. The program now acts as a standard wifi device (station), makes contact with your browser, and gets an IP address. The reswt of the program stays the same.

Make sure that you replace YOUR-ROUTERS-NAME and PASSWORD with your routers credentials.



You need to run the adapted program with a USB connection to your computer as we need to get the IP address the ESP8266 has gotten from your router. Your's will of course be different.

And now we have the IP address.

The Javascript program.

The Javascript program is more than 300 lines long.
Use a text editor like notepad to load the program. An HTML file is just a plain ASCII text.
Use the search function in the editor to find this line:

fetch(`http://192.168.4.1`).then(function(response) {

Replace the IP address 192.168.4.1 with the address Thonny showed. In my example it should look like this:

fetch(`http://192.168.178.231`).then(function(response) {

Then save the file with exact the same name in a directory on your PC.
Open that directory and click on the HTML file.



And here is the caravan/mobile-home leveler running in my browsers screen on my PC.

Clicking anywhere in the screen with your mouse will open the program full-screen. Clicking the red exit button in the top-right corner makes the screen smaller again. You can resize it to any size you like and the program will adjust automatically.

Several parts of the program were taken from my book on Javascript: Javascript tips. This book does not teach how to program in Javascript but gives more than 500 tips that can make programming in Javascript a lot easier.


Click this line (or the book) to find the book on Amazon.

That's it for now
Have fun

Luc Volders