Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
gopalreddylakki committed Oct 21, 2022
1 parent 38cbcb6 commit 18bb680
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
Binary file added b44m6eapplication.aia
Binary file not shown.
1 change: 1 addition & 0 deletions flows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":"4021245d1b6d7d40","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"f6a7ec8bfb5ef015","type":"ibmiot in","z":"4021245d1b6d7d40","authentication":"apiKey","apiKey":"5294e29e93ef0d30","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"b11m3edeviceid","applicationId":"","deviceType":"b11m3edevicetype","eventType":"+","commandType":"","format":"json","name":"IBM IoT","service":"registered","allDevices":"","allApplications":"","allDeviceTypes":"","allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":"","qos":0,"x":90,"y":80,"wires":[["0d9ec2a6c3a1254c","4bee47d588da7f95","1e0c2960f6208f51"]]},{"id":"0d9ec2a6c3a1254c","type":"debug","z":"4021245d1b6d7d40","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":80,"wires":[]},{"id":"ec9880a871673597","type":"ui_gauge","z":"4021245d1b6d7d40","name":"","group":"d99cefdf8f5b3159","order":0,"width":0,"height":0,"gtype":"gage","title":"Temperature","label":"C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":550,"y":140,"wires":[]},{"id":"4bee47d588da7f95","type":"function","z":"4021245d1b6d7d40","name":"temperature Node","func":"msg.payload=msg.payload.Temperature\nglobal.set('t',msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":140,"wires":[["ec9880a871673597"]]},{"id":"1e0c2960f6208f51","type":"function","z":"4021245d1b6d7d40","name":"humidity","func":"msg.payload=msg.payload.Humidity\nglobal.set('h',msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":220,"y":200,"wires":[["5ceab371e6f62287"]]},{"id":"5ceab371e6f62287","type":"ui_gauge","z":"4021245d1b6d7d40","name":"","group":"d99cefdf8f5b3159","order":1,"width":0,"height":0,"gtype":"gage","title":"Humidity","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","className":"","x":560,"y":200,"wires":[]},{"id":"10f9d1cd0daeee60","type":"ui_button","z":"4021245d1b6d7d40","name":"","group":"d99cefdf8f5b3159","order":2,"width":0,"height":0,"passthru":false,"label":"Ligth on","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"lighton","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":440,"wires":[["39f7014e98fe5e27","9e821b343cfda771"]]},{"id":"39f7014e98fe5e27","type":"debug","z":"4021245d1b6d7d40","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":460,"wires":[]},{"id":"9e821b343cfda771","type":"ibmiot out","z":"4021245d1b6d7d40","authentication":"apiKey","apiKey":"5294e29e93ef0d30","outputType":"cmd","deviceId":"b11m3edeviceid","deviceType":"b11m3edevicetype","eventCommandType":"command","format":"String","data":"1","qos":0,"name":"IBM IoT","service":"registered","x":560,"y":560,"wires":[]},{"id":"ce774594d7acee57","type":"http in","z":"4021245d1b6d7d40","name":"","url":"/sensor","method":"get","upload":false,"swaggerDoc":"","x":130,"y":260,"wires":[["5324d6cc43f52808"]]},{"id":"5324d6cc43f52808","type":"function","z":"4021245d1b6d7d40","name":"hhtpfunctionnode","func":"msg.payload={\"temperature\":global.get(\"t\"),\"humidity\":global.get(\"h\")}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":260,"wires":[["1196ac6baa5b1bd6"]]},{"id":"1196ac6baa5b1bd6","type":"http response","z":"4021245d1b6d7d40","name":"","statusCode":"","headers":{},"x":590,"y":260,"wires":[]},{"id":"12f32dcdb2135fb7","type":"ui_button","z":"4021245d1b6d7d40","name":"","group":"d99cefdf8f5b3159","order":4,"width":0,"height":0,"passthru":false,"label":"Light Off","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"lightoff","payloadType":"str","topic":"topic","topicType":"msg","x":120,"y":540,"wires":[["9e821b343cfda771","39f7014e98fe5e27"]]},{"id":"73bbd596372befec","type":"http in","z":"4021245d1b6d7d40","name":"","url":"/control","method":"get","upload":false,"swaggerDoc":"","x":130,"y":620,"wires":[["3b736a33b2a8996d","a91fc73a9d6eab35"]]},{"id":"3b736a33b2a8996d","type":"http response","z":"4021245d1b6d7d40","name":"","statusCode":"","headers":{},"x":550,"y":620,"wires":[]},{"id":"a91fc73a9d6eab35","type":"function","z":"4021245d1b6d7d40","name":"","func":"msg.payload=msg.payload.command\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":700,"wires":[["1e2d07bd4418b39f","9e821b343cfda771"]]},{"id":"1e2d07bd4418b39f","type":"debug","z":"4021245d1b6d7d40","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":700,"wires":[]},{"id":"9fb44dccc653ec20","type":"ui_spacer","z":"4021245d1b6d7d40","name":"spacer","group":"d99cefdf8f5b3159","order":1,"width":1,"height":1},{"id":"5294e29e93ef0d30","type":"ibmiot","name":"","keepalive":"60","serverName":"","cleansession":true,"appId":"","shared":false},{"id":"d99cefdf8f5b3159","type":"ui_group","name":"weather monitoring","tab":"5869a8033ec3b485","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"5869a8033ec3b485","type":"ui_tab","name":"control","icon":"dashboard","disabled":false,"hidden":false}]
153 changes: 153 additions & 0 deletions sketch.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include <WiFi.h>//library for wifi
#include <PubSubClient.h>//library for MQtt
#include "DHT.h"// Library for dht11
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // define type of sensor DHT 11
#define LED 5
DHT dht (DHTPIN, DHTTYPE);// creating the instance by passing pin and typr of dht connected

void callback(char* subscribetopic, byte* payload, unsigned int payloadLength);

//-------credentials of IBM Accounts------

#define ORG "0jjsl2"//IBM ORGANITION ID
#define DEVICE_TYPE "b11m3edevicetype"//Device type mentioned in ibm watson IOT Platform
#define DEVICE_ID "b11m3edeviceid"//Device ID mentioned in ibm watson IOT Platform
#define TOKEN "_zlY3G?Os5O?M5puUo" //Token
String data3;
float h, t;


//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";// Server Name
char publishTopic[] = "iot-2/evt/Data/fmt/json";// topic name and type of event perform and format in which data to be send
char subscribetopic[] = "iot-2/cmd/test/fmt/String";// cmd REPRESENT command type AND COMMAND IS TEST OF FORMAT STRING
char authMethod[] = "use-token-auth";// authentication method
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;//client id


//-----------------------------------------
WiFiClient wifiClient; // creating the instance for wificlient
PubSubClient client(server, 1883, callback ,wifiClient); //calling the predefined client id by passing parameter like server id,portand wificredential
void setup()// configureing the ESP32
{
Serial.begin(115200);
dht.begin();
pinMode(LED,OUTPUT);
delay(10);
Serial.println();
wificonnect();
mqttconnect();
}

void loop()// Recursive Function
{

h = dht.readHumidity();
t = dht.readTemperature();
Serial.print("temperature:");
Serial.println(t);
Serial.print("Humidity:");
Serial.println(h);

PublishData(t, h);
delay(1000);
if (!client.loop()) {
mqttconnect();
}
}



/*.....................................retrieving to Cloud...............................*/

void PublishData(float temp, float humid) {
mqttconnect();//function call for connecting to ibm
/*
creating the String in in form JSon to update the data to ibm cloud
*/
String payload = "{\"temperature\":";
payload += temp;
payload += "," "\"humidity\":";
payload += humid;
payload += "}";


Serial.print("Sending payload: ");
Serial.println(payload);


if (client.publish(publishTopic, (char*) payload.c_str())) {
Serial.println("Publish ok");// if it sucessfully upload data on the cloud then it will print publish ok in Serial monitor or else it will print publish failed
} else {
Serial.println("Publish failed");
}

}
void mqttconnect() {
if (!client.connected()) {
Serial.print("Reconnecting client to ");
Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}

initManagedDevice();
Serial.println();
}
}
void wificonnect() //function defination for wificonnect
{
Serial.println();
Serial.print("Connecting to ");

WiFi.begin("Wokwi-GUEST", "", 6);//passing the wifi credentials to establish the connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void initManagedDevice() {
if (client.subscribe(subscribetopic)) {
Serial.println((subscribetopic));
Serial.println("subscribe to cmd OK");
} else {
Serial.println("subscribe to cmd FAILED");
}
}

void callback(char* subscribetopic, byte* payload, unsigned int payloadLength)
{

Serial.print("callback invoked for topic: ");
Serial.println(subscribetopic);
for (int i = 0; i < payloadLength; i++) {
//Serial.print((char)payload[i]);
data3 += (char)payload[i];
}

Serial.println("data: "+ data3);
if(data3=="lighton")
{
Serial.println(data3);
digitalWrite(LED,HIGH);

}

else
{
Serial.println(data3);
digitalWrite(LED,LOW);

}
data3="";


}

0 comments on commit 18bb680

Please sign in to comment.