element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Needing some help with BBB and mysql temperature data
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 6 replies
  • Answers 1 answer
  • Subscribers 48 subscribers
  • Views 926 views
  • Users 0 members are here
Related

Needing some help with BBB and mysql temperature data

ridemywideglide
ridemywideglide over 10 years ago

I have almost zero experience with coding, and I'm needing some help..

 

I managed to piece together code from thanks to google to do most of what I want, but I'm missing steps..

 

I have this code from Brad's blog, that works just fine, but I need to add to it the ability to put the data in a mysql database, not print it to the screen..

 

#include <stdio.h>

#include <dirent.h>

#include <string.h>

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

   

// struct to hold ds18b20 data for linked list

struct ds18b20 {

  char devPath[128];

  char devID[16];

  char tempData[6];

  struct ds18b20 *next;

};


// Find connected 1-wire devices. 1-wire driver creates entries for each device

// in /sys/bus/w1/devices on Beaglebone Black.  Create linked list.

int8_t findDevices(struct ds18b20 *d) {

  DIR *dir;

        struct dirent *dirent; 

  struct ds18b20 *newDev;

        char path[] = "/sys/bus/w1/devices";

        int8_t i = 0;

        dir = opendir(path);

        if (dir != NULL)

        {

                while ((dirent = readdir(dir))) {

                        // 1-wire devices are links beginning with 28-

                        if (dirent->d_type == DT_LNK &&

                                        strstr(dirent->d_name, "28-") != NULL) {

  newDev = malloc( sizeof(struct ds18b20) );

                                strcpy(newDev->devID, dirent->d_name);

                                // Assemble path to OneWire device

                                sprintf(newDev->devPath, "%s/%s/w1_slave", path, newDev->devID);

                                i++;

  newDev->next = 0;

  d->next = newDev;

  d = d->next;

                        }

  }

  (void) closedir(dir);

        }

        else

  {

                perror ("Couldn't open the w1 devices directory");

                return 1;

        }

  return i;

}


int8_t readTemp(struct ds18b20 *d) {

  while(d->next != NULL){

  d = d->next;

  int fd = open(d->devPath, O_RDONLY);

  if(fd == -1)

        {

        perror ("Couldn't open the w1 device.");

                return 1;

        }

  char buf[256];

  ssize_t numRead;

        while((numRead = read(fd, buf, 256)) > 0) {

                strncpy(d->tempData, strstr(buf, "t=") + 2, 5);

                float tempC = strtof(d->tempData, NULL);

                printf("Device: %s  - ", d->devID);

                printf("Temp: %.3f C  ", tempC / 1000);

                printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32);

        }

        close(fd);

  }

  return 0;

}


int main (void) {

  struct ds18b20 *rootNode;

  struct ds18b20 *devNode;

  // Load pin configuration. Ignore error if already loaded

  system("echo BB-W1 > /sys/devices/bone_capemgr.9/slots 2>/dev/null");

  while(1) {

  rootNode = malloc( sizeof(struct ds18b20) );

  devNode = rootNode;

  int8_t devCnt = findDevices(devNode);

  printf("\nFound %d devices\n\n", devCnt);

  readTemp(rootNode);

  // Free linked list memory

  while(rootNode) {

  // Start with current value of root node

  devNode = rootNode;

  // Save address of next devNode to rootNode before deleting current devNode

  rootNode = devNode->next;

  // Free current devNode.

  free(devNode);

  }

  // Now free rootNode

  free(rootNode);

  }

  return 0;

}


I also have code that fakes temp readings and adds them to the database, but I'm lost on how to modify that code to take real readings as the author was using an older temp sensor.

  • Sign in to reply
  • Cancel
  • clem57
    0 clem57 over 10 years ago

    Can you give reference(URL) to Brad's Post? Any other sources(URL) of the code? It is difficult to see code without any comments and understand the flow. What SQL database do you have loaded?

    Thanks,

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ridemywideglide
    0 ridemywideglide over 10 years ago in reply to clem57

    Here's the link to Brad's code and a description of it.

     

    http://bradsmc.blogspot.com/2014/06/c-program-to-read-multiple-ds18b20-1.html?showComment=1424817923261#c4194925361961791536

     

    The code that I've started with, is linked below. The only problem is that the temp sensor is different, and I'm not of the ability to modify that for my ds18b20 sensors... Seemed easier to add database ability to Brad's code.

     

    BeagleBone Web Server - Temperature Sensor

     

     

    I've got mysql 5.5.41

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to ridemywideglide

                     printf("Device: %s  - ", d->devID);                      // Device ID value

                     printf("Temp: %.3f C  ", tempC / 1000);              // Temp in C

                     printf("%.3f F\n\n", (tempC / 1000) * 9 / 5 + 32); // Temp in F

    // Initialize the temperature sensor

    MCP9701E sensor("/sys/devices/platform/omap/tsc/ain2");

              

    // Take a temperature measurement and insert it into the database

    for (int i = 0; i < 10; i++)

    {

    temp = sensor.GetTemperature();

    // Move the above values into correct variables before running the next statement

    mysql_stmt_execute(stmt);

    sleep(60);   // Sleep for a minute

    }

              

    // Close the temperature sensor

    sensor.Close();

    I have added comments showing the information to move from the one piece to where they get written to the SQL code.

     

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • ridemywideglide
    0 ridemywideglide over 10 years ago in reply to clem57

    I'm not using any of the code that mentions the MCP9701E sensor. I only referenced it as an example of what I need. I used the rest of that tutorial to get the server and mysql working, but the temp sensor part is useless for me as I have a 1 wire sensor.

     

    The first link (Brads code) is where I'm needing the mysql data entry addition, and it doesn't need to print anything on the screen, just update the database every second..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to ridemywideglide

    Looking for a 1 wire sensor, I find a good tutorial on wiring, powering and programming at Arduino Playground - OneWire. I beleive it is simple enough yet fairly complete.

    Hope this works,

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ridemywideglide
    0 ridemywideglide over 10 years ago in reply to clem57

    Yea I don't know where the miss-communication keeps happening...  I don't need  a tutorial for 1 wire sensors, I have them working...  I need to add the ability to put the temperature data in the mysql database, to the code linked first, in the first post, Brad's code..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube