Tuesday, 17 September 2019
ESP32-FTP
This summary is not available. Please
click here to view the post.
ESP32-JSON
ESP32-JSON
Aim:
To
interface Wi-Fi module with ESP32 to send data to server using RDL ESP32
Development board.
Description:
Interfacing
wi-fi module to send data to the server using ESP32microcontroller.
Hardware required:
ESP32-Microcontroller
development board.
Procedure:
1.
Connect the USB cable to the ESP32 development board.
2.
Open Arduino IDE .Select DOIT ESP32 DEVKIT V1in
boards and select COM port.
3.
Verify the program and upload it.
4.
Now you can see wifi connected along with the IP
address on the serial monitor.
5. To check the inserted values of gas and temperature use the server address and check it on
the browser.
Program:
/*
* This
sketch sends data via HTTP POST requests to data.sparkfun.com service.
*
* You
need to get streamId and privateKey at data.sparkfun.com and paste them
* below.
Or just customize this script to talk to other HTTP servers.
*
*/
#include <WiFi.h>
char ssid[] = "your
ssid"; char password[] = "your password";
const char* host = "varmatrix.com";
const char* streamId = "....................................................... ";
const char* privateKey = ".......................................................................... ";
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);
/*
Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to
act as both a client and an access-point and could cause network-issues with
your other WiFi-devices on your WiFi-network. */
//WiFi.mode(WIFI_STA);
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() {
String url="/surendrardl/nitk/getone/insert.php?";
String jason_string="";
char cmd=0;
while(cmd!=13)
{
if (Serial.available()) { cmd=Serial.read();
// Serial.write(cmd); if(cmd!=13)
jason_string+=cmd;
}
}
while(Serial.available()>0) {Serial.read();}
Serial.print("connecting to ");
Serial.println(host);
//
Use WiFiClient class to create TCP connections const int httpPort = 80;
if (!esp32client.connect(host, httpPort))
{ Serial.println("connection failed"); return;
}
// We now
create a URI for the request
//String url="/energyconsumption/insert.php?one=1&two=3&three=2";
Serial.print("Requesting URL:");
Serial.println(url);
//String
jason_string="{\"gas\":\"40\",\"temp\":\"70\"}";
// This will
send the request to the server esp32client.print(String("POST ") + url
+ "
HTTP/1.0\r\n" +
"Host:
" + host + "\r\n"
+ "Accept:
*" + "/"
+ "*\r\n"
+
"Content-Length:
" + jason_string.length() + "\r\n" + "Content-Type: application/json\r\n" +
"\r\n" + jason_string
+ "\r\n");
unsigned long timeout = millis(); while (esp32client.available() == 0) {
if (millis() - timeout
> 5000) { Serial.println(">>>
Client Timeout !"); esp32client.stop();
return;
}
}
// Read all
the lines of the reply from server and print them to Serial while(esp32client.available()){
String line = esp32client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
// Serial.println("closing connection");
}
Output:
1.
Open serial monitor you can see Wi-Fi connected
along with IP address
Insert { “gas”:”40” , “temperature”:”70” } in the
serial monitor and press enter.
Monday, 16 September 2019
ESP32-MQTT
ESP32-MQTT
Aim:
To
interface Wi-Fi module with ESP32 to receive data from cloud using RDL ESP32
Development board.
Description:
Interfacing wi-fi module with the cloud MQTT to receive and send
data using ESP32 microcontroller.
Hardware required:
ESP32-Microcontroller
development board.
Pin connection:
Procedure:
Uploading
and Receiving Data from Cloud using ESP32
2.
Create an instance which will be used in the program
to publish data to the cloud.
3. Select the Instance name (test_1) and use the
details of the same in the code. Include the
code.
5.
Enter the details given under server, port, user,
ssl port in the given program.
const char*
mqttServer =”m16.mqtt.com”; const int mqttPort = 18215;
const char*
mqttUser = “ofmydmjh”;;
const char*
mqttPassword = “c5goIEjTCGRn”;
6.
In order to send a message to the
CloudMQTT, include the line client.publish("topic_name", "Message");
7.
To send data from CloudMQTT ,include the line client.subscribe("Topic_name");\
5. 8. The following code prints the received data from the
cloud in the Serial Monitor void callback(char* topic, byte* payload, unsigned
int length)
{
uint8_t s;
Serial.print("Message
arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i =
0; i < length; i++)
{
s= payload[i];
Serial.write(s);
}
}
5.
The final Program:
#include <WiFi.h>
#include <PubSubClient.h> const char* ssid = "userid";
const char*
password = "*********";
/* Details from the instance created */
const char*
mqttServer = "m16.cloudmqtt.com"; const int mqttPort = 18215;
const char*
mqttUser = "ofmydmjh";
const char*
mqttPassword = "c5g0IEjTCGRn"; WiFiClient
espClient;
PubSubClient client(espClient); void setup(void)
{
Serial.begin(115200); WiFi.begin(ssid, password);
/* Connecting ESP8266 to WiFi */
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.write('.');
}
Serial.println("Connected to the WiFi
network"); client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
/* Connecting to CloudMqtt */ while (!client.connected())
{
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client",
mqttUser, mqttPassword ))
{
Serial.println("connected");
}
else
{
Serial.print("failed with state ");
Serial.print(client.state()); delay(2000);
}
}
/* Sending message to Topic "test1" */ client.publish("ABC", "Hello from RDL_IOT");
client.subscribe("test"); //Receives
message sent to the topic "test"
}
/* This function is used to print the incoming data sent
to the topic "test" */ void
callback(char* topic, byte* payload, unsigned int length)
{
uint8_t s;
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int
i = 0; i <
length; i++)
{
s= payload[i];
Serial.write(s);
}
}
void loop(void)
{
client.loop();
}
Output:
Subscribe to:
Posts (Atom)