NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

/**
IBM IoT Foundation managed Device

Author: Ant Elder
License: Apache License v2
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h> // https://github.com/knolleary/pubsubclient/releases/tag/v2.3
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson/releases/tag/v5.0.7
#define led D1
#define led2 D2
#define led3 D3
#define led4
int v;
#define led5 D5
//-------- Customise these values -----------
const char* ssid = "OPPO A37f";
const char* password = "123456789";
#define led D1
#define led2 D2
#define led3 D3
#define led4 D4
#define led5 D5
#define ORG "q91plx"
#define DEVICE_TYPE "device"
#define DEVICE_ID "mydevice123"
#define TOKEN "1C*z4L0e-tNyZNtYRi"
//-------- Customise the above values --------

char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
//String value;
const char publishTopic[] = "iot-2/evt/status /fmt/json";
const char responseTopic[] = "iotdm-1/response";
const char manageTopic[] = "iotdevice-1/mgmt/manage";
const char updateTopic[] = "iotdm-1/device/update";
const char rebootTopic[] = "iotdm-1/mgmt/initiate/device/reboot";
const char subTopic[] = "iot-2/cmd/nishanth/fmt/json";
void wifiConnect();
void mqttConnect();
void initManagedDevice();
void publishData();
void handleUpdate(byte* payload) ;
void callback(char* topic, byte* payload, unsigned int payloadLength);
WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);
int publishInterval = 5000; // 30 seconds
long lastPublishMillis;
int sensorpin = A0;
int sensorvalue;
void setup() {
Serial.begin(115200);
Serial.println();
pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
pinMode(D5,OUTPUT);
wifiConnect();
mqttConnect();
initManagedDevice();
}

void loop() {


if (millis() - lastPublishMillis > publishInterval) {
publishData();
lastPublishMillis = millis();
}

if (!client.loop()) {
mqttConnect();
initManagedDevice();
}
}

void wifiConnect() {
Serial.print("Connecting to "); Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("nWiFi connected, IP address: "); Serial.println(WiFi.localIP());
}

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

void initManagedDevice() {
if (client.subscribe("iotdm-1/response")) {
Serial.println("subscribe to responses OK");
} else {
Serial.println("subscribe to responses FAILED");
}

if (client.subscribe(rebootTopic)) {
Serial.println("subscribe to reboot OK");
} else {
Serial.println("subscribe to reboot FAILED");
}

if (client.subscribe("iotdm-1/device/update")) {
Serial.println("subscribe to update OK");
} else {
Serial.println("subscribe to update FAILED");
}
if (client.subscribe(subTopic)) {
Serial.println("subscribe to subtopic OK");
} else {
Serial.println("subscribe to update FAILED");
}


StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& d = root.createNestedObject("d");
JsonObject& metadata = d.createNestedObject("metadata");
metadata["publishInterval"] = publishInterval;
JsonObject& supports = d.createNestedObject("supports");
supports["deviceActions"] = true;

char buff[300];
root.printTo(buff, sizeof(buff));
Serial.println("publishing device metadata:"); Serial.println(buff);
if (client.publish(manageTopic, buff)) {
Serial.println("device Publish ok");
} else {
Serial.print("device Publish failed:");
}
}

void publishData() {

String payload = "{"d":{"Illumination%":";
payload += sensorvalue;
payload += "}}";

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

if (client.publish(publishTopic, (char*) payload.c_str())) {
Serial.println("Publish OK");
} else {
Serial.println("Publish FAILED");
}
}

void callback(char* topic, byte* payload, unsigned int payloadLength) {
Serial.print("callback invoked for topic: "); Serial.println(topic);

if (strcmp (responseTopic, topic) == 0) {
return; // just print of response for now
}

if (strcmp (rebootTopic, topic) == 0) {
Serial.println("Rebooting...");
ESP.restart();
}

if (strcmp (updateTopic, topic) == 0) {
handleUpdate(payload);
}
if (strcmp (subTopic, topic) == 0) {
Serial.print("Subscribed");
Serial.println((char*)payload);
handleUpdate(payload);
}
}

void handleUpdate(byte* payload) {
StaticJsonBuffer<300> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject((char*)payload);
if (!root.success()) {
Serial.println("handleUpdate: payload parse FAILED");
return;
}
String value=root["command"];
int v1=root["value1"];
int v2=root["value2"];
int v3=root["value3"];
int v4=root["value4"];
int v5=root["value5"];
analogWrite(D1,v1);
analogWrite(D2,v2);
analogWrite(D3,v3);
analogWrite(D4,v4);
analogWrite(D5,v5);



Serial.print("data:");
Serial.println(value);
if(value=="LIGHTON")
{
digitalWrite(D1,HIGH);

}
if(value=="LIGHTOFF")
{
digitalWrite(D1,LOW);

}
if(value=="LIGHT2ON")
{
digitalWrite(D2,HIGH);
}
if(value=="LIGHT2OFF")
{
digitalWrite(D2,LOW);
}
if(value=="LIGHT3ON")
{
digitalWrite(D3,HIGH);
}
if(value=="LIGHT3OFF")
{
digitalWrite(D3,LOW);
}
if(value=="LIGHT4ON")
{
digitalWrite(D4,HIGH);
}
if(value=="LIGHT4OFF")
{
digitalWrite(D4,LOW);
}
if(value=="LIGHT5ON")
{
digitalWrite(D5,HIGH);
}
if(value=="LIGHT5OFF")
{
digitalWrite(D5,LOW);
}

value="";
Serial.println("handleUpdate payload:"); root.prettyPrintTo(Serial); Serial.println();

JsonObject& d = root["d"];
JsonArray& fields = d["fields"];
for (JsonArray::iterator it = fields.begin(); it != fields.end(); ++it) {
JsonObject& field = *it;
const char* fieldName = field["field"];
if (strcmp (fieldName, "metadata") == 0) {
JsonObject& fieldValue = field["value"];
if (fieldValue.containsKey("publishInterval")) {
publishInterval = fieldValue["publishInterval"];
Serial.print("publishInterval:"); Serial.println(publishInterval);
}
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.