NodeMCU og flere DS18B20 på Thingspeak Hjelp

Holder på å lage meg en gjærings logger basert på NodeMCU og DS18B20 sensorer.
Denne skal laste opp til Thingspeaks.

Har kommet så langt at jeg har fått systemet til å laste opp med en sensor.
Har også klart å finne adressene til alle sensorene jeg vil koble til.

Men så kommer utfordringen. Hvordan kode slik at jeg får flere sensorer. Foreløbig tenkte jeg å prøve med 3 stk.

Her er koden jeg bruker. Hentet fra nett.
Noen som kan hjelpe?

Kode:
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//Def
#define myPeriodic 60 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2  // DS18B20 on arduino pin2 corresponds to D4 on physical board
//#define mySSR 0  // Solid State Relay on pin 0

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="xxxxxx";
const char* MY_SSID = "ASUS";
const char* MY_PWD = "xxxxxxx";
int sent = 0;
void setup() {
  Serial.begin(115200);
  connectWifi();
}

void loop() {
  float temp;
  //char buffer[10];
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
  Serial.print(String(sent)+" Temperature: ");
  Serial.println(temp);
 
  //if (temp != prevTemp)
  //{
  //sendTeperatureTS(temp);
  //prevTemp = temp;
  //}
 
  sendTeperatureTS(temp);
  int count = myPeriodic;
  while(count--)
  delay(1000);
}

void connectWifi()
{
  Serial.print("Connecting to "+*MY_SSID);
  WiFi.begin(MY_SSID, MY_PWD);
  while (WiFi.status() != WL_CONNECTED) {
  delay(1000);
  Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("Connected");
  Serial.println(""); 
}//end connect

void sendTeperatureTS(float temp)
{ 
   WiFiClient client;
 
   if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
   Serial.println("WiFi Client connected ");
   
   String postStr = apiKey;
   postStr += "&field1=";
   postStr += String(temp);
   postStr += "\r\n\r\n";
   
   client.print("POST /update HTTP/1.1\n");
   client.print("Host: api.thingspeak.com\n");
   client.print("Connection: close\n");
   client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
   client.print("Content-Type: application/x-www-form-urlencoded\n");
   client.print("Content-Length: ");
   client.print(postStr.length());
   client.print("\n\n");
   client.print(postStr);
   delay(1000);
   
   }//end if
   sent++;
client.stop();
}//end send
 
Sikkert ikke det mest optimale, men du kan initialisere alt så har med temp 3 ganger, litt tungvindt men det funker.

Gjorde det slik selv nå sist, har kjørt en 1.5 uke med nodemcu, 3 sensorer, koblet mot blynk, så via blynk til thingspeak i tillegg. Blynk kan anbefales, veldig bra og enkelt for å vise dataene.

IMG_3874.PNG IMG_3841.JPG
 
Sikkert ikke det mest optimale, men du kan initialisere alt så har med temp 3 ganger, litt tungvindt men det funker.

Gjorde det slik selv nå sist, har kjørt en 1.5 uke med nodemcu, 3 sensorer, koblet mot blynk, så via blynk til thingspeak i tillegg. Blynk kan anbefales, veldig bra og enkelt for å vise dataene.

Vis vedlegget 30480 Vis vedlegget 30481
Takk for svar. Skal prøve det.
Har aldri brukt Blynk før, så det blir jo en ny utfordring
 
getTempCByIndex(0) leser første sensoren, getTempCByIndex(1) leser sensor nummer 2 og så videre.
Du må lese temperatur fra hver sensor du har koblet til, og sende disse til Thingspeak. Jeg kjenner ikke thingspeak, men kanskje noe slikt:

Kode:
...
  DS18B20.requestTemperatures();
  temp1 = DS18B20.getTempCByIndex(0);
  temp2 = DS18B20.getTempCByIndex(1);
  temp3 =DS18B20.getTempCByIndex(2);
  sendTemp(temp1,temp2,temp3);
...

  void sendtTemp(float temp1, float temp2, float temp){
...
   postStr += "&field1=";
   postStr += String(temp1);
   postStr += "&field2=";
   postStr += String(temp2);
   postStr += "&field3=";
   postStr += String(temp3);
   postStr += "\r\n\r\n";
...
  }
 
Problemet løst.
Fant en kode på nett som gjør det hele utrolig enkelt.
Bare å forandre personlige opplysninger, og laste den opp til min Nodemcu, og alt virker.
Har foreløbig tilkoblet tre sensorer men skal prøve med flere senere.
Legge ved den nye koden om andre er interessert.

Kode:
#include <DallasTemperature.h>
#include <OneWire.h>
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>

// User changeable vaules go here.

#define ONE_WIRE_BUS D4                                  // Digital pin DS18B20 is connected to.
#define TEMPERATURE_PRECISION 11                  // Set sensor precision.  Valid options 8,10,11,12 Lower is faster but less precise

unsigned long myChannelNumber = xxxxx;            // Thingspeak channel ID here
const char * myWriteAPIKey = "xxxxxxxxxxxx";      // Write API key here
const char* ssid     = "ASUS";                                 // SSID of wireless network
const char* password = "xxxxxxxxx";                     // Password for wireless network

int fieldStart = 1;                               // Field number to start populating ThingSpeak channel data, leave at
                                                  // 1 if this is the only device reporting to that channel. 
                                                  // If more than one device this should be the FIRST FREE field.

int updatePeriod = 120;                            //delay in seconds to update to ThingSpeak.  Should be set to not less than 15.


int status = WL_IDLE_STATUS;
WiFiClient  client;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);


int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address

void setup() {
Serial.begin(115200);
  delay(10);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  ThingSpeak.begin(client);
  sensors.begin();
 
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();
 
  // locate devices on the bus
  Serial.print("Locating devices...");
 
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");

  // report parasite power requirements
  Serial.print("Parasite power is: ");
  if (sensors.isParasitePowerMode()) Serial.println("ON");
  else Serial.println("OFF");
 
  // Loop through each device, print out address
  for(int i=0;i<numberOfDevices; i++)
  {
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i))
  {
    Serial.print("Found device ");
    Serial.print(i, DEC);
    Serial.print(" with address: ");
    printAddress(tempDeviceAddress);
    Serial.println();
   
    Serial.print("Setting resolution to ");
    Serial.println(TEMPERATURE_PRECISION, DEC);
   
    // set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
    sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
   
    Serial.print("Resolution actually set to: ");
    Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
    Serial.println();
  }else{
    Serial.print("Found ghost device at ");
    Serial.print(i, DEC);
    Serial.print(" but could not detect address. Check power and cabling");
  }
  }
}

// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

void loop() {
  sensors.requestTemperatures();
  // Itterate through each sensor and send readings to ThinkSpeak. 
  for (int i=0; i<=(numberOfDevices - 1); i++){
    float temp=sensors.getTempCByIndex(i);
    ThingSpeak.setField(i+fieldStart,temp);
    Serial.println("Sensor #:");
    Serial.println(i);
    Serial.println("Temperature:");
    Serial.println(temp);
  }
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);  //Write fields to Thingspeak, comment this line out
                                                              //if you wish to test without uploading data.
    Serial.println("Data sent to ThinkSpeak");
  delay(updatePeriod * 1000);
}
 
Tilbake
Topp