Follow Us
Facebook Twitter Google Plus Linkedin Instagram YouTube

Hello world of IoT

Almost all tech enthusiasts have performed “Hello World” practicals when learning any new programming language, which displays “Hello World” statement on output console.

People from embedded system background have performed “LED Blinking” as a Hello World while learning any new embedded platform. In similar manner, an IoT engineer performs a Hello World practical while learning IoT, it is known as sending data over internet using HTTP protocol.

There are plenty of easy to use platforms available to send and observe data using HTTP protocol like ThingsSpeak, Freeboard, Node-Red etc. But out of these all, most easy platform is Dweet.io

 

It is fast, free and ridiculously simple— it’s like Twitter for social machines.

We will be using NodeMCU hardware platform to send data collected on analog pin A0 to cloud. If you are new to NodeMCU then first check out Getting Started with NodeMCU(ESP8266) tutorial.

 

Interfacing:

You could interface any analog sensor of your choice to pin A0 or you could connect a jumper wire which could be switched between 3v3 pin/ GND pin to have variation at the input of A0 pin.

 

Programming:

Now it’s time for programming. Copy and paste the following code in Arduino IDE. Change “ssid” and “password” with your respective WiFi ssid and password. So that NodeMCU could be connected to internet.


#include <ESP8266WiFi.h>
const int AnalogIn = A0;
const char* ssid = "Your_ssid";
const char* password = "Your_Password";
const char* host = "www.dweet.io";
const char* thing = "cirkitect";   //replace it with your thing name
const char* thing_content = "A0";


void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
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());
}


int value = 0;


void loop() {
delay(1000);
value = analogRead(AnalogIn);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/dweet/for/";
url += thing;
url += "?";
url += thing_content;
url += "=";
url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
int timeout = millis() + 5000;
while (client.available() == 0) {
if (timeout - millis() < 0) { Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}

After successfully uploading code on NodeMCU, open the serial monitor and see weather your NodeMCU have started sending data.

Now to observe this data on Dweet.io platform go to this URL : http://dweet.io/follow/cirkitect

You should relace “cirkitect” with the thing name used in program. Try making changes in sensor output and observe the respective changes on webpage in the form of graph.

Leave a Reply

Your email address will not be published.Required fields are marked *

Bitnami