Jump to content

Arduino help


Jenki

Recommended Posts

Can anyone point me to some useful online guides?

I've done some python programming, and messing around with arduino and an ESP32.

I've got plans for many little projects measuring temperatures etc and hopefully a solar diverter.

Currently cutting and pasting snippets of code to try to get readings from DS18B20. Which I can do, but struggling with the best way to get the hex address in a format where I can send this to an External log. 

I think SScanf is the solution, but just don't seem to be able to get my head around using it 🙈.

 

My first project is to read my sensors and get the hex address (1 at a time) and want to do this remotely. So I've got the ESP to connect to WiFi, and send data to a Google sheets via  http. ( not tested this not connected to my pc- but it is passing temperatures) I'm struggling to the address into a format I can add to the http string.

Which leads me to think I need some more background understanding than just cut, paste, compile try to fix errors, and find it doesn't work. So delete that and copy something else. Where should I look? Or are there any good books recommendations?

TIA

Link to comment
Share on other sites

So initially I had to install some sensors (time constraints) without getting the address. So I've worked these to a central point, I've used two sensors on each wire for the same location(redundancy). So I firstly want to read each wire and get the two addresses. I can do this on a bread board in the static caravan we are living, but need to do it remotely and the ESP32 WiFi is easy to set up. So I don't have a problem getting readings from the sensors, and even get the address (Hex) to print to the serial monitor. But struggling to get this into the Http string.

.....exec?sensor=28C6DE49F6B63C55.

Link to comment
Share on other sites

16 minutes ago, Jenki said:

So I don't have a problem getting readings from the sensors, and even get the address (Hex) to print to the serial monitor. But struggling to get this into the Http string.

.....exec?sensor=28C6DE49F6B63C55.

Post your code and we can take a look. 

Link to comment
Share on other sites

38 minutes ago, MikeSharp01 said:

Post your code and we can take a look. 

Thanks @MikeSharp01  here's the code that is sending info the the google sheet. its just sending the sensor number in the the array at the moment. (i)

 

 

//Include required libraries
//sensors
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged TO GPIO 4
#define ONE_WIRE_BUS 4

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Number of temperature devices found
int numberOfDevices;

// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress; 

// =======================

OneWire ds(4);  //data wire connected to GPIO 4
 

// =============================


//wifi

#include "WiFi.h"
#include <HTTPClient.h>
#include "time.h"
const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 19800;
const int   daylightOffset_sec = 0;
// WiFi credentials
const char* ssid = "****";         // change SSID
const char* password = "****";    // change password
// Google script ID and required credentials
String GOOGLE_SCRIPT_ID = "AKfycbyHfficNQ-jJUTHrwKSyQVmlix5tvJdSX6DPkW2nJr52NBWN9rCqp-m7dQYI9AteDO1";    // change Gscript ID

int count = 0;
char Sensor[16];

void setup() {

    Serial.begin(9600);
   // connect to WiFi
  Serial.println();
  Serial.print("Connecting to wifi: ");
  Serial.println(ssid);
  Serial.flush();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

 if (WiFi.status() == WL_CONNECTED) {
    static bool flag = false;

    // get sensor info

 // Start up the library
  sensors.begin();


  
  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();


// +++++++++++++++++++++

  // locate devices on the bus
  Serial.println("Locating devices...");
  Serial.print("Found ");

  Serial.print(numberOfDevices, DEC);
  Serial.println(" devices.");
  Serial.println("");
  
  Serial.println("Printing addresses...");
  for (int i = 0;  i < numberOfDevices;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    sensors.getAddress(tempDeviceAddress, i);
    // printAddress(tempDeviceAddress);
  }

  
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numberOfDevices, DEC);
   Serial.println(" devices.");

char sensorstr; // hold hexsensor name


// Loop through each device, print out address


// +++++++++++++++++++ I think this should fix, just dont understand the syntax....

//uint8_t i;
//int addrv[8];

//        sscanf(newAddy, "%x,%x,%x,%x,%x,%x,%x,%x",
//                &addrv[0], &addrv[1], &addrv[2], &addrv[3],
//                &addrv[4], &addrv[5], &addrv[6], &addrv[7]);  // parse the 8 ascii hex bytes in 8 ints

  //      for(i = 0; i< 8; i++)
    //    {
      //          device[i] = (__typeof__(device[0])) addrv[i]; //fill in device address bytes using a cast
        //}

// +++++++++++++++++++


  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: ");
// sensorstr = printAddress(tempDeviceAddress);
      Serial.println();
    } else {
      Serial.print("Found ghost device at ");
      Serial.print(i, DEC);
      Serial.print(" but could not detect address. Check power and cabling");
    }
  }
  sensors.requestTemperatures(); // Send the command to get temperatures
  
  // Loop through each device, print out temperature data
  for(int i=0;i<numberOfDevices; i++){
    // Search the wire for address
    if(sensors.getAddress(tempDeviceAddress, i)){
       float tempC = sensors.getTempC(tempDeviceAddress);
      // Output the device ID
            Serial.println(i,DEC);


for (uint8_t i = 0; i < 8; i++)  // this oprints the hex number to the serial for testing....
  {
    //Serial.print("0x");
    if (tempDeviceAddress[i] < 0x10) Serial.print("0");
    Serial.print(tempDeviceAddress[i], HEX);
//    strcat(sensorstr, (tempDeviceAddress[i], HEX));  // 
   Serial.println(">>");
   Serial.print(sensorstr);
  Serial.println("");
  Serial.println("<<");
  }

     
      
    
    String urlFinal = "https://script.google.com/macros/s/"+GOOGLE_SCRIPT_ID+"/exec?"+"logger="+(i)+"&sensor="+tempC;
    Serial.print("POST data to spreadsheet:");
    Serial.println(urlFinal);
    HTTPClient http;
    http.begin(urlFinal.c_str());
    http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
    int httpCode = http.GET(); 
    Serial.print("HTTP Status Code: ");
    Serial.println(httpCode);
    //---------------------------------------------------------------------
    //getting response from google sheet
    String payload;
  //  if (httpCode > 0) {
    //    payload = http.getString();
      //  Serial.println("Payload: "+payload);    
   // }
    //---------------------------------------------------------------------
    http.end();
 
  }
  }
}
 
}
 

Link to comment
Share on other sites

14 hours ago, MikeSharp01 said:

Sprintf does it - here is a little experiment I just did!

image.thumb.png.9c9ef691b3a96522565039f8d2886a5c.png

Thanks @MikeSharp01. This is my problem. I'm sure it does, I just don't understand getting it to work for me, this is why I need to do some more learning rather than cut and paste. Any suggestions on books/ tutorials? Thanks again.

Link to comment
Share on other sites

OK - I understand. So below is some example code I built to give you an idea to play with and links to some tutorial stuff. I connected a DS18B20 to an ESP32 board and ran the code below. It explains, very simply, why sscanf can't be used out of the box, you will need to pre process the int8_t buffer before you could use it and if you pre processed it you wouldn't need it - if you get my drift. The tutorials point link in the code is quite good at explaining the syntax for sscanf. In some ways sprintf also needs a bit of work but would be a much more elegant solution in my view. I have tried to explain it all in the code as we go along.

 

My code, with output, full code is below. The bottom line is printed from a string built up by iterating across the int8_t buffer, the line above it by just printing our each byte value as HEX and the line above that is the raw code output from the tutorial mentioned. Hope it makes sense. 

image.thumb.png.3b831ce3a13dc276dbdb511c88c345c9.png

 

My setup looks like this, just one DS18B20

image.thumb.png.ca7cc84ea02f8eb67ea331908bdd4585.png

 

Full code:

 

#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 15 on the ESP32
#define ONE_WIRE_BUS 15
 
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
 
// variable to hold device addresses
DeviceAddress Thermometer;
 
int deviceCount = 0;
char str[20];         // I added this bit - we will use this to create the bytes as hex.
String addressStr;    // and this - our final string will appear here
 
void setup(void)
{
  // start serial port
  Serial.begin(115200);
 
  // Start up the library
  sensors.begin();
 
  // locate devices on the bus
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
 
  Serial.println("Printing addresses...");
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    sensors.getAddress(Thermometer, i);
    printAddress(Thermometer);
  }
}
 
void loop(void)
{ }
 
void printAddress(DeviceAddress deviceAddress)
{
  // This the original code I am playing with. It pulls each int out of the
  // int8_t buffer and really just uses the clever bits of the Serial.print
  // function to pop out the hex values. You can do very little from here to
  // create a string. Howevere the basic process can be used and that is what
  // I have done below.
  for (uint8_t i = 0; i < 8; i++)
  {
    Serial.print("0x");
    if (deviceAddress[i] < 0x10) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) Serial.print(", ");
  }
  Serial.println("");
 
  //  This is the additional bit:
  /*  Our unit8_t[8] is an eight byte array / buffer each byte of which holds
      a number that represents the HEX value of the byte address in that
      position so we will need to handle them one after the other and build
      up our final string as we go.
      So we need to iteratively pull all the HEX values out one by one
      and then build up our string of all the chars. Sprintf with the
      %x format does this neatly. It will put every hex pair into our
      str buffer a pair at a time.
      We will then need to take that pair and pop it into the bigger
      address buffer so we can print / add it to our data line later.  
  */
 
  //First set up a couple of things...
  Serial.println("");     // Give us a blank line
  addressStr = "";        // Clear our output buffer
 
  /*  Now we can iterate down the uint8_t buffer pulling each byte out, use
      sprintf to create an ASCII pair from the value, print the pair out, as debug check,
      and add it to the addressStr buffer. sscanf can't be used directly as it needs
      char input and you have ints. You can read more at this tutorial
  */
  for (uint8_t i = 0; i < 8; i++)
  {
    sprintf(str, "%x", deviceAddress[i]);
    Serial.print(str);
    addressStr = addressStr + str;
    //if (i < 7) Serial.print(", ");
  }
  Serial.println("");           // Give us a blank line
  Serial.println(addressStr);   // Now print out our string - it should look
                                // just like the line above it which was created
                                // as we iterated but now we have it as a string
                                // we can pass to other things EG your google sheet.
}
 

 

 

 

  • Thanks 1
Link to comment
Share on other sites

No worries I have a bunch of the same sensors in the house so although I plan different way to do things. I thought it would be fun to pull out the address as a string. I convert the number to a meaningfull name in the code local code, MQTT the reading and store readings into a mysql db using node red.

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...