Create account in ThingsSpeak . After sign up complete you see the following profile dashboard after click on MyProfile.

Now Download POSTMAN and install in the system. Using Postman you can create channel through which you can feed data of channel field. In thingspeak you can create 8 field , where data will be stored. You can feed data from postman using http get, post and you can show the realtime field data in android application.7
After downloading and installing the Postman , open Postman you will see the following dashboard.
At first let create channel with three field like field1, field2, field3.
Copy link like this from your profile
https://api.thingspeak.com/channels.json?api_key=ILA15VPFYBECOKAS&name=MyChannel
and paste in postman and select post and send. MyChannel will be created.
You have to replace this api_key with your api_key
https://api.thingspeak.com/update?api_key=HU377FC9DOAH7P3O&field1=6
After paste in postman this link and send , the field1 value updated using 6. You can see from MyChannel.
Now we have to develop android code to show the realtime field data.
https://github.com/eurosa/MQTT.git
Download code and run in Android Studio.
In MainActvity replace Client Id with your Thingspeak Client Id and also username and password
public class MainActivity extends AppCompatActivity { Button b ; Button c; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connect(); b = (Button)findViewById(R.id.button); c = (Button)findViewById(R.id.button2); b.setOnClickListener(v -> { Intent ii = new Intent(MainActivity.this,graphActivity.class); MainActivity.this.startActivity(ii); }); c.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent ii = new Intent(MainActivity.this,Main3Activity.class); MainActivity.this.startActivity(ii); } }); } public void connect(){ String clientId ="KBUjEzIRCBsvCgcNCSwfHQ4"; MqttClient.generateClientId(); final MqttAndroidClient client = new MqttAndroidClient(this.getApplicationContext(), "tcp://mqtt3.thingspeak.com:1883",//mqtt:// not working > tcp://working clientId); MqttConnectOptions options = new MqttConnectOptions(); options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1); options.setCleanSession(false); options.setUserName("KBUjEzIRCBsvCgcNCSwfHQ4"); options.setPassword("syDJo4Ad/oVb8rjbSAuf8nQE".toCharArray()); try { IMqttToken token = client.connect(options); //IMqttToken token = client.connect(); token.setActionCallback(new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { // We are connected Log.d("file", "onSuccess"); //publish(client,"payloadd"); subscribe(client,"channels/2071049/subscribe/fields/field1"); // subscribe(client,"channels/2071049/fields/field1"); client.setCallback(new MqttCallback() { TextView tt = (TextView) findViewById(R.id.tt); TextView th = (TextView) findViewById(R.id.th); @Override public void connectionLost(Throwable cause) { Log.d("file", "connectionLost"); } @Override public void messageArrived(String topic, MqttMessage message) throws Exception { Log.d("file 1", message.toString()); if (topic.equals("channels/2071049/subscribe/fields/field1")){ tt.setText(message.toString()); } if (topic.equals("channels/2071049/fields/field1")){ th.setText(message.toString()); } } @Override public void deliveryComplete(IMqttDeliveryToken token) { } }); } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { // Something went wrong e.g. connection timeout or firewall problems Log.d("file", "onFailure"); } }); } catch (MqttException e) { e.printStackTrace(); } } public void publish(MqttAndroidClient client, String payload){ String topic = "foo/bar"; byte[] encodedPayload = new byte[0]; try { encodedPayload = payload.getBytes("UTF-8"); MqttMessage message = new MqttMessage(encodedPayload); client.publish(topic, message); } catch (UnsupportedEncodingException | MqttException e) { e.printStackTrace(); } } public void subscribe(MqttAndroidClient client , String topic){ int qos = 1; try { IMqttToken subToken = client.subscribe(topic, qos); subToken.setActionCallback(new IMqttActionListener() { @Override public void onSuccess(IMqttToken asyncActionToken) { // The message was published } @Override public void onFailure(IMqttToken asyncActionToken, Throwable exception) { // The subscription could not be performed, maybe the user was not // authorized to subscribe on the specified topic e.g. using wildcards } }); } catch (MqttException e) { e.printStackTrace(); } } // public void printMessege(MqttAndroidClient client){ // client.setCallback(new MqttCallback() { // @Override // public void connectionLost(Throwable cause) { // // } // // @Override // public void messageArrived(String topic, MqttMessage message) throws Exception { // Log.d("file", message.toString()); // // } // // @Override // public void deliveryComplete(IMqttDeliveryToken token) { // // } // }); // } }
Comments