Robofun 機器人論壇

標題: 程式合併 [打印本頁]

作者: James    時間: 2019-5-21 14:23
標題: 程式合併
想要請問一下各位大大

我想要透過透過lora的接收端上傳感測器的數據到ThingSpeak,但不知道要怎麼合併

再麻煩各位大大了


以下是ThingSpeak的程式碼

#include "ACS712.h"
// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial =  SoftwareSerial(2,3);      // arduino RX pin=2  arduino TX pin=3    connect the arduino RX pin to esp8266 module TX pin   -  connect the arduino TX pin to esp8266 module RX pin


ACS712 sensor(ACS712_30A, A1);
#include <DHT.h>        //Attention: For new  DHT11 version  library you will need the Adafruit_Sensor library
#define DHTPIN 12                // Connect the signal pin of DHT11 sensor to digital pin 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const int mq3 = 0;
const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=8;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino

int limit;
int value;



String apiKey = "DVXLJUGQVUUKQXZN";     // replace with your channel's thingspeak WRITE API key

String ssid="Green-7730";    // Wifi network SSID
String password ="135791113";  // Wifi network password

boolean DEBUG=true;

//======================================================================== showResponce
void showResponse(int waitTime){
    long t=millis();
    char c;
    while (t+waitTime>millis()){
      if (espSerial.available()){
        c=espSerial.read();
        if (DEBUG) Serial.print(c);
      }
    }
                  
}
//========================================================================
boolean thingSpeakWrite(float value1, float value2, float value3, float value4, float value5){
  String cmd = "AT+CIPSTART=\"TCP\",\"";                  // TCP connection
  cmd += "184.106.153.149";                               // api.thingspeak.com
  cmd += "\",80";
  espSerial.println(cmd);
  if (DEBUG) Serial.println(cmd);
  if(espSerial.find((char*)"Error")){
    if (DEBUG) Serial.println("AT+CIPSTART error");
    return false;
  }
  
  
  String getStr = "GET /update?api_key=";   // prepare GET string
  getStr += apiKey;
  
  getStr +="&field1=";
  getStr += String(value1);
  getStr +="&field2=";
  getStr += String(value2);
  getStr +="&field3=";
  getStr += String(value3);
  getStr +="&field4=";
  getStr += String(value4);
  getStr +="&field5=";
  getStr += String(value5);
  
  // getStr +="&field3=";
  // getStr += String(value3);
  // ...
  getStr += "\r\n\r\n";

  // send data length
  cmd = "AT+CIPSEND=";
  cmd += String(getStr.length());
  espSerial.println(cmd);
  if (DEBUG)  Serial.println(cmd);
  
  delay(1000);
  if(espSerial.find((char*)">")){
    espSerial.print(getStr);
    if (DEBUG)  Serial.print(getStr);
  }
  else{
    espSerial.println("AT+CIPCLOSE");
    // alert user
    if (DEBUG)   Serial.println("AT+CIPCLOSE");
    return false;
  }
  return true;
}
//================================================================================ setup
void setup() {               
  DEBUG=true;           // enable debug serial
  Serial.begin(9600);
    pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPin, OUTPUT);
sensor.calibrate();
  dht.begin();
  espSerial.begin(9600);  // enable software serial
                          // Your esp8266 module's speed is probably at 115200.
                          // For this reason the first time set the speed to 115200 or to your esp8266 configured speed
                          // and upload. Then change to 9600 and upload again
  
  //espSerial.println("AT+RST");         // Enable this line to reset the module;
  //showResponse(1000);

  //espSerial.println("AT+UART_CUR=9600,8,1,0,0");    // Enable this line to set esp8266 serial speed to 9600 bps
  //showResponse(1000);
  
  

  espSerial.println("AT+CWMODE=1");   // set esp8266 as client
  showResponse(1000);

  espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\"");  // set your home router SSID and password
  showResponse(5000);

   if (DEBUG)  Serial.println("Setup completed");
}


// ====================================================================== loop
void loop() {
  value = analogRead(AOUTpin); //reads the analaog value from the alcohol sensor's AOUT pin
  limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin
// Read sensor values
float I = sensor.getCurrentDC();
   float t = dht.readTemperature();
   float h = dht.readHumidity();
        if (isnan(t) || isnan(h)) {
        if (DEBUG) Serial.println("Failed to read from DHT");
      }
      else {
          if (DEBUG)  Serial.println("Temp="+String(t)+" *C");
          if (DEBUG) Serial.println("Humidity="+String(h)+" %");
          if (DEBUG) Serial.println(String("I = ") + I + " A");
                                     // Write values to thingspeak
      }
       if (isnan(value) || isnan(limit)) {
        if (DEBUG) Serial.println("Failed to read from Mq3");
      }
      else {
          if (DEBUG)  Serial.println("alcohol="+String(value));
          if (DEBUG) Serial.println("lim="+String(limit));
           thingSpeakWrite(value,limit,t,h,I);                                      // Write values to thingspeak
      }

delay(20000);  
}










以下是Lora接收端的程式碼


#include <SPI.h>
#include <RH_RF95.h>

//When use LG01, uncomment this line, so print the result in Console.
//When use LoRa Mini Dev, Comment this link
//#define  LG01_GATEWAY  

#ifdef LG01_GATEWAY
#include <Console.h>
#include <Process.h>
#define BAUDRATE 115200
#define SerialPrint Console
#else
#define SerialPrint Serial
#endif

float frequency = 868.0;// Change the frequency here.

RH_RF95 rf95;
void setup()
{
  #ifdef LG01_GATEWAY
    Bridge.begin(BAUDRATE);
    SerialPrint.begin();
  #else
    SerialPrint.begin(9600);
  #endif
  
  if (!rf95.init())
    {
      SerialPrint.println("LoRa module init failed, Please cehck hardware connection");
      
    }
   
  // Setup ISM frequency
  rf95.setFrequency(frequency);
  // Setup Power,dBm
  rf95.setTxPower(13);
  // Defaults BW Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
  SerialPrint.print("Listening on frequency: ");
  SerialPrint.println(frequency);
}

void loop()
{
  if (rf95.available())
  {
    // Should be a message for us now   
    char buf[RH_RF95_MAX_MESSAGE_LEN];
    char len = sizeof(buf);
    if (rf95.recv(buf, &len))
    {
     if(buf[0] == 1 & buf[1] == 1 & buf[2] ==1) //Get sensor data from node id 111
      {
        int newData[6] = {0, 0, 0, 0, 0, 0};
        for (int i = 0; i < 4; i++)
        {
          newData[i] = buf[i +3];
        }
        int a = newData[0];
        int b = newData[1];
        int c = newData[2];
        float d = newData[3];
        SerialPrint.println("ID = 111 :");
        SerialPrint.print("Current humdity = ");
        SerialPrint.print(a);
        SerialPrint.print("%  ");
        SerialPrint.print("temperature = ");
        SerialPrint.print(b);
        SerialPrint.println("C  ");
        SerialPrint.print("酒精值:");
        SerialPrint.println(c);
        SerialPrint.print("I=");
        SerialPrint.print(d);
        SerialPrint.println("A");
        
        
      }
     if(buf[0] == 1 & buf[1] == 1 & buf[2] == 2) //Get sensor data from node id 112
      {
        int newData[4] = {0, 0, 0, 0};
        for (int i = 0; i < 2; i++)
        {
          newData[i] = buf[i + 3];
        }
        int a = newData[0];
        int b = newData[1];
        SerialPrint.print("ID = 112 :");
        SerialPrint.print("Current humdity = ");
        SerialPrint.print(a);
        SerialPrint.print("%  ");
        SerialPrint.print("temperature = ");
        SerialPrint.print(b);
        SerialPrint.println("C  ");
      }
    }
    else
    {
        SerialPrint.println("recv failed");
    }
  }
}




歡迎光臨 Robofun 機器人論壇 (https://robofun.net/forum/) Powered by Discuz! X3.2