从我的PI中,我通过pubnub发送一条消息,其中包含已被刻录的Leds的序列。很奇怪我在Android应用程序中收到了不同的东西。
#! /usr/bin/env python
#
import random
import time
import sys
from time import sleep
import RPi.GPIO as GPIO
from pubnub import Pubnub
pubnub = Pubnub(publish_key='pub-c-da666f8d070cace',
subscribe_key='sub-c-93dab7fe')
CHANNEL_IN = 'in'
CHANNEL_OUT = 'out'
def blink_leds():
leds = [18,17,27,22]
random.shuffle(leds)
volgorde = ', '.join(str(led) for led in leds)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
for led in leds:
GPIO.setup( led, GPIO.OUT)
for led in leds:
for state in [True,False]:
GPIO.output(led, state)
sleep(0.5)
return volgorde
def run():
print("blinking leds")
volgorde = blink_leds()
print("sending code")
print(volgorde)
pubnub.publish(CHANNEL_IN,volgorde)
def stop():
GPIO.cleanup()
pubnub.unsubscribe(CHANNEL_OUT)
def callback(message,channel):
if 'Inorde' in str(message):
print("Inorde")
run()
if 'Fout' in str(message):
print("Fout")
stop()
if 'Start' in str(message):
print("starting")
run()
pubnub.subscribe(channels=CHANNEL_OUT, callback=callback)
pubnub.publish(CHANNEL_IN,"Start")
我做错了什么?
我的活动: 包be.thomasmore.sequincepi;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.*;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import com.pubnub.api.*;
import org.json.*;
import java.util.HashMap;
import java.util.Random;
public class play extends Activity {
private Pubnub mPubNub;
public static final String PUBLISH_KEY = "pub-c-da6362de-5562-43a7-9b0e-66f8d070cace";
public static final String SUBSCRIBE_KEY = "sub-c-935fbdb2-df94-11e5-8905-02ee2ddab7fe";
public static final String CHANNEL_IN = "in";
public static final String CHANNEL_OUT = "out";
Button btn1;
Button btn2;
Button btn3;
Button btn4;
TextView txtHaal;
public String volgorde = "";
public String antwoord = "";
public int score = 0;
TextView txtScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
txtHaal = (TextView) findViewById(R.id.txtHaal);
txtScore = (TextView) findViewById(R.id.txtScore);
initPubNub();
knoppen();
}
private void knoppen() {
btn1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
button.setEnabled(false);
antwoord += "1";
controleer();
}
});
btn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
button.setEnabled(false);
antwoord += "2";
controleer();
}
});
btn3.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
button.setEnabled(false);
antwoord += "3";
controleer();
}
});
btn4.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
button.setEnabled(false);
antwoord += "4";
controleer();
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
btn1.setEnabled(false);
btn2.setEnabled(false);
btn3.setEnabled(false);
btn4.setEnabled(false);
}
});
}
public void controleer() {
if (antwoord.length() == 4) {
if (antwoord.equals(juisteNummers(volgorde))) {
publish("Inorde");
score++;
String.valueOf(score);
}else{
txtHaal.setText("Fout");
publish("Fout");
}
}
}
public void setTextView(final TextView textView, final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(text);
}
});
}
public void initPubNub() {
this.mPubNub = new Pubnub(
PUBLISH_KEY,
SUBSCRIBE_KEY);
this.mPubNub.setUUID("SequencePI");
subscribe();
}
public void subscribe() {
try {
this.mPubNub.subscribe(CHANNEL_IN, new Callback() {
@Override
public void connectCallback(String channel, Object message) {
Log.d("PUBNUB", "SUBSCRIBE : CONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void disconnectCallback(String channel, Object message) {
Log.d("PUBNUB", "SUBSCRIBE : DISCONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
public void reconnectCallback(String channel, Object message) {
Log.d("PUBNUB", "SUBSCRIBE : RECONNECT on channel:" + channel
+ " : " + message.getClass() + " : "
+ message.toString());
}
@Override
public void successCallback(String channel, Object message) {
Log.d("PUBNUB", "SUBSCRIBE : " + channel + " : "
+ message.getClass() + " : " + message.toString());
//juisteNummers(message.toString());
if(message.toString().contains("Start")){
publish("Start");
}else{
volgorde = message.toString();
antwoord ="";
runOnUiThread(new Runnable() {
@Override
public void run() {
btn1.setEnabled(true);
btn2.setEnabled(true);
btn3.setEnabled(true);
btn4.setEnabled(true);
}
});
}
}
@Override
public void errorCallback(String channel, PubnubError error) {
Log.d("PUBNUB", "SUBSCRIBE : ERROR on channel " + channel
+ " : " + error.toString());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public void publish(String done) {
JSONObject js = new JSONObject();
try {
js.put(CHANNEL_OUT, done);
} catch (JSONException e) {
e.printStackTrace();
}
Callback callback = new Callback() {
public void successCallback(String channel, Object response) {
Log.d("PUBNUB", response.toString());
}
public void errorCallback(String channel, PubnubError error) {
Log.d("PUBNUB", error.toString());
}
};
this.mPubNub.publish(CHANNEL_OUT, js, callback);
}
public String juisteNummers(String volgorde) {
String vol = "";
HashMap<String,String> omzetting = new HashMap<>();
omzetting.put("17","1");
omzetting.put("18","2");
omzetting.put("22","3");
omzetting.put("27","4");
for (int i = 0;i<=12;i+=4) {
vol += omzetting.get(volgorde.substring(i, i + 2));
}
return vol;
}
}
答案 0 :(得分:0)
您的Python代码中的pub / sub键与Android代码中的pub / sub键不匹配。而且你不应该公开发布你的全部密钥。