2017-02-12 1 views
1

Quelqu'un peut-il m'aider à trouver comment assembler ces deux morceaux de code pour obtenir le résultat dont j'ai besoin? Mes yeux se croisent en regardant ça. Je sais que c'est un jeu d'enfant pour tout le monde sauf moi, mais je ne suis pas un programmeur et c'est juste pour un petit projet personnel. Jusqu'ici, après des heures et des heures de lecture et de visionnage de toutes les vidéos que j'ai pu trouver concernant Arduino, Pubnub et les capteurs, j'ai fait publier des capteurs sur Pubnub. J'ai créé un compte Freeboard pour la visualisation et tout cela fonctionne. Le problème est que les données publiées sont fausses. Fondamentalement, je veux lire une tension de la batterie et le publier sur PubNub. Je peux obtenir l'Arduino (Uno R3) pour lire la tension et je peux ajuster les valeurs dans le code pour correspondre à la tension réelle. Le problème que je rencontre est de prendre ce morceau de code qui fonctionne et de le bourrer dans le tableau JSON qui est publié sur PubNub. Si quelqu'un serait prêt à m'aider et peut-être expliquer un peu (ou non - je vais bien si je le fais juste travailler), j'apprécierais tellement le temps, l'aide et l'effort.Comment puis-je obtenir la valeur de la broche A0 de la deuxième esquisse dans le tableau JSON de la première esquisse?

Merci!

// Chaque esquisse fonctionne de manière indépendante. J'ai besoin de les fusionner pour obtenir la bonne lecture publiée.

//VoltagePubNub.ino (C'est celui qui publie, ce qui est ce que je veux. Je veux juste la valeur publiée pour être la valeur du second croquis.)


#include <SPI.h> 
    #include <Ethernet.h> 
    #include <PubNub.h> 
    #include <aJSON.h> 

    // Some Ethernet shields have a MAC address printed on a sticker on the shield; 
    // fill in that address here, or choose your own at random: 
    const static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 

    // Memory saving tip: remove myI and dnsI from your sketch if you 
    // are content to rely on DHCP autoconfiguration. 
    IPAddress myI(192, 168, 2, 114); 
    IPAddress dnsI(8, 8, 8, 8); 


    const static char pubkey[] = "publish_key"; 
    const static char subkey[] = "subscribe_key"; 
    const static char channel[] = "channel_name"; 
    char uuid[] = "UUID"; 

    #define NUM_CHANNELS 1 // How many analog channels do you want to read? 
    const static uint8_t analog_pins[] = {A0}; // which pins are you reading? 

    void setup() 
    { 
     Serial.begin(9600); 
     Serial.println("Serial set up"); 

     Ethernet.begin((byte*) mac, myI, dnsI); 
     Serial.println("Ethernet set up"); 
     delay(1000); 

     Serial.println("Ethernet set up"); 

     PubNub.begin(pubkey, subkey); 
     Serial.println("PubNub set up"); 
      delay(5000); 
    } 

    void loop() 
    { 

     Ethernet.maintain(); 

     EthernetClient *client; 

      // create JSON objects 
      aJsonObject *msg, *analogReadings; 
      msg = aJson.createObject(); 
      aJson.addItemToObject(msg, "analogReadings", analogReadings = aJson.createObject()); 

      // get latest sensor values then add to JSON message 
      for (int i = 0; i < NUM_CHANNELS; i++) { 
       String analogChannel = String(analog_pins[i]); 
       char charBuf[analogChannel.length()+1]; 
       analogChannel.toCharArray(charBuf, analogChannel.length()+1); 
       int analogValues = analogRead(analog_pins[i]); 
       aJson.addNumberToObject(analogReadings, charBuf, analogValues); 
      } 

      // convert JSON object into char array, then delete JSON object 
      char *json_String = aJson.print(msg); 
      aJson.deleteItem(msg); 

      // publish JSON formatted char array to PubNub 
     Serial.print("publishing a message: "); 
     Serial.println(json_String); 
      client = PubNub.publish(channel, json_String); 
     if (!client) { 
     Serial.println("publishing error"); 
     } else 
      free(json_String); 
     client->stop(); 

     delay(5000); 
    } 

//VoltageSensor.ino

(Ceci est celui avec la valeur correcte, mais pas fonction de publication.)


int analogInput = A0; 
    float vout = 0.0; 
    float vin = 0.0; 
    float R1 = 31000.0; // 
    float R2 = 8700.0; // 
    int value = 0; 
    int volt = 0; 
    void setup(){ 
     pinMode(analogInput, INPUT); 
     Serial.begin(9600); 
     Serial.print("DC VOLTMETER"); 
     Serial.println(""); 
    } 
    void loop(){ 
     // read the value at analog input 
     value = analogRead(analogInput); 
     vout = (value * 4.092)/1024.0; 
     vin = vout/(R2/(R1+R2)); 

    Serial.print("INPUT V= "); 
    Serial.println(vin,2); 
    delay(2000); 
    } 

Répondre

2

Ce n'est peut-être pas la façon la plus glamour ou la plus appropriée de le faire, mais j'ai réussi à faire ce dont j'avais besoin. J'édité la première esquisse avec le code suivant:


// create JSON objects 
    aJsonObject *msg, *analogReadings; 
    msg = aJson.createObject(); 
    aJson.addItemToObject(msg, "analogReadings", analogReadings = aJson.createObject()); 


    // get latest sensor values then add to JSON message 
    for (int i = 0; i < NUM_CHANNELS; i++) { 
     float vout = 0.0; 
     float vin = 0.0; 
     float R1 = 33060.0; // 
     float R2 = 7600.0; // 
     int value = 0; 
     int volt = 0; 

     //Serial.print("INPUT V= "); 
     //Serial.println(vin,2); 

     String analogChannel = String(analog_pins[i]); 

     value = analogRead(analog_pins[i]); 
     vout = (value * 4.092)/1024.0; 
     vin = vout/(R2/(R1+R2));   

     char charBuf[analogChannel.length()+1]; 
     analogChannel.toCharArray(charBuf, analogChannel.length()+1); 
     float theVoltage = (vin); 
     int analogValues = analogRead(analog_pins[i]); 
     aJson.addNumberToObject(analogReadings, charBuf, theVoltage); 
    } 

    // convert JSON object into char array, then delete JSON object 
    char *json_String = aJson.print(msg); 
    aJson.deleteItem(msg); 

Maintenant, la valeur est publiée à PubNub et est sur Freeboard.io à ont consigné les this link.