Connect ServoMotor to Internet using MicroPython

Keywords: MQTT, IoT, Motor Control, PWM control, GPIO, ESP8266-ThingSpeak, Connecting ESP8266 to WiFi, ESP8266, MicroPython, uPyCraft, Connecting ‘Thing – ESP8266’ to ‘Internet’, ESP8266-IoT

 

We hear many times the word IoT, after all what does this word mean – Internet of Things, that seems to be the full form but what does Internet of Things mean???????

Yes you guessed it right, connecting Things to Internet. Things can be sensors, machines, cars, etc.

I have ‘Things’ with me like motor and LED, now I have to send the motor average voltage value to server. For that I need a cloud space to Visualize live data and this platform would be ‘ThingSpeak‘. Now what is this ‘ThingSpeak‘ – ThingSpeak is an IoT analytics platform service that allows us to visualize live data streams in the cloud. We can send data to ThingSpeak from our devices, create instant visualization of live data, and send alerts. It is free of cost but with some limitations.

Now lets learn about how to connect Things – Servo Motor to Internet

Prerequisites:

  • ThingSpeak Account – You can follow the below steps to create and account in ThingSpeak.
    • Step 1 – Sign Up

    • Step 2 – Click on Create Account and enter valid email address.

    • Step3 – Click on continue.

    • Step4 – Click on continue.

    • Step 5 – Click on continue, you would receive an email from Mathworks which is an activation link for ThingSpeak.

    • Step 6 – Once clicked on the emailed link, it states that the profile is verified.

    • Step 7 – Once email is verified crate a password for this account.

    • Step 8 – After login the page would be like this

    • Step 9 – To create a new channel follow this link
    • Step 10 – This is the channel which I have created.

 

Now that we are done with setting ThingSpeak server account and creating channels and fields, we have achieved the Internet part. Here comes the great challenge, Thing is ready and Internet is ready but connection is still pending. How do we connect this, we need a medium which is capable of connecting thing to internet which I choose to be ESP8266 WiFi board here.

There are many IoT protocols but MQTT is most commonly used.It basically works on Publish and Subscribe model.

Here are few terminologies used in MQTT

Client

The Client is some thing which gets connected to server. For example I am using my laptop/mobile/desktop to connect to google website, so my laptop/mobile/desktop is a client and google is a server.

Broker

The MQTT broker (or server) is the central server to which all MQTT clients connect. The MQTT broker manages message topics. When a client updates a topic, the broker sends the new messages to all of the clients who subscribed the same topic.

Topic

MQTT Messages are arranged in a hierarchy of topics that are addressed as UTF-8 strings. Levels in the topic hierarchy are separated by forward slash characters (‘/’) similar to how files are arranged on your local disk. Topic strings (paths) are used to access MQTT messages in the same way file paths are used to access files.

For example: “myChannel/field1/voltage” and “myChannel/field2/state”.

Publish

If a client updates the data associated with topic in broker, it is called Publishing. If any client subscribes for that same topic the same updated data is reflected and broker publishes new messages for the topic’s subscribers . There is no limit to the number of clients that can publish to a topic.  Clients can only publish to one topic at a time.

Subscribe

Once a client subscribes to a topic on the MQTT broker, it will receive all of the subsequent messages that have been published to the topic. There is no limit to the number of clients that can subscribe to a topic.  Clients can subscribe to either a specific topic such as “myChannel/field1/voltage” or to a range of topics which would be discussed later.

MQTT Payloads

MQTT payloads are the body of each MQTT message – the application data carried by the MQTT packet.  The payload is data format agnostic and can support anything including text, images, binary numbers, etc.

Prerequisites continued:

  • Software
    • uPyCraftIDE
    • esp8266 latest bin file
    • Python
  • Hardware
      • ESP8266 WiFi board
    • WiFi broadband/hotspot/LAN
    • Servo Motor

Environment SetUp

Please go through this link just for reference.

IoT – MQTT publish

  • Here is the output of this code.

  • Now we know how to publish to a server using MQTT protocol. KUDOS!

NOTE : All the code written in posts are all well tried and tested personally.

THANK YOU and many more programs coming your way!

Please put your queries in comment section, I will get back to you with answers ASAP.
Please like and share Techawarey. Find and Like Techawarey on Facebook.😄

Related posts

One Thought to “Connect ServoMotor to Internet using MicroPython”

  1. […] Connecting ESP8266 to a server Thingspeak/AWS for IoT  […]

Leave a comment....