I thought I’d drop this here for someone who needed a copy-paste solution.
I am parsing a serial string output from a YDLIDAR X4 rotating lidar. I have to do this because the library sucks. a lot. I mean I’m not over my heels with the unit anyhow, first off who the hell outputs at 128,000 baud? I’ll post the code when it’s done enough for someone to build off of it.
I was getting an error in number of bytes in the packet as parsed from the serial string so I had to break out the function into really busy but obvious code to see where I was going wrong. The code has me concatenating two bytes and an upper nibble. Ignore the discrepancy in how I added my bytes backwards in my code vs. how the device actually stacks it’s data… because you know: WHO IN THEIR RIGHT MIND SHIFTS LSB ON THE LEFT?! (note the “length bytes as shown in 12 bits and the “response length” example below.)
Anyhow… feel free to show me how to do it better in the comments. Obviously this is built for you to look at it do it’s magic in a serial monitor.
What a groaner.. who needs another datalogger? Well I do, so stuff it. I was inspired to check out these iButtonLink devices that allow you to daisy-chain connect a number of devices in a single circuit of sorts with RJ45 patch cables. I had seen these a couple years ago in some top-notch datacenters that terminated to a concentrator (4 strings) that were then pushed up to some IBM services app server.
A while back I published a Microsoft PIC microcontroller version of this … I decided to give it another whirl with the “easy button” … Arduino. Of course there were a couple libraries that I just had to collectively stuff into the same sketch and easy-peasy it was done in an hour or so.
This time I have my data stored to an SD card, which … I hastily put together.
If I was actually going to finish this I would add three things to this to finish it up.
Use an RTC, figure an interface to enter the correct time. (maybe add an ethernet shield and get NTP?) … or just manual enter.
I’d grab the interval time off the RTC and just have the loop do a non-blocking wait, then poll the RTC time or interrupt (whatever you favorite way to do this is..) that way I was collecting trend information on the “:00″s.
I would add a method of checking for an SD card to be interested and allow it to start logging from card insert. I would also add time/date stamp to the file and finally, still SD card related I would check file size and stop logging when I got close to full.
So that is that. I’ve been creating a lot of fun lately but nothing mind-bending.. just going through every module I’ve ever bought doing the demo code … or going the other way around.. working on every Arduino demo/example in a class .. I’ve also built an FE boat, moved my workshop inside (update video coming kind of soon) and been working on PopBot 0.5 ..
The code:
/*
* Chas Ihler
* @chasxmd
* https://iradan.com
*
* License: Public Domain - it's mostly copy and paste and preferences anyways.
*
* iButtonLink, grabs all sensors and logs them to card if present.
* Recommendations:
* Add an RTC and a method of updating time.
* I'd also grab actual seconds off the RTC for logging and use a non-blocking wait for the polling/logging.
*
* Libraries:
* https://github.com/matmunk/DS18B20
* IDE built in SD card/SPI library
*
* Sensor:
* https://www.ibuttonlink.com/products/t-sense-temperature-sensor
* which has this device within..
* https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
*
* Hookup:
* You'll need a pullup resistor from 5V to pin 2
* ibutton hookup:
* 1 - GND
* 2 - 5VDC
* 3 - NC
* 4 - Arduino Pin 2 (w/ Pullup)
* 5 - GND
* 6 - NC
* 7 - NC
* 8 - NC
*
* SD card attached to SPI bus as follows:
* MOSI - pin 11
* MISO - pin 12
* CLK - pin 13
* CS - pin 4
*
*/
#include <DS18B20.h>
#include <SPI.h>
#include <SD.h>
DS18B20 ds(2); //pin 2
const int chipSelect = 4;
void setup() {
Serial.begin(115200);
delay(100);
Serial.println("----------------------------------------");
Serial.println(" ");
Serial.println("DS18B20 / iButtonLink Demo");
Serial.print("Devices: ");
Serial.println(ds.getNumberOfDevices());
Serial.println(" ");
Serial.println("----------------------------------------");
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
int i_address;
Serial.println("----------------------------------------");
while (ds.selectNext()) {
String str_dataString = "";
switch (ds.getFamilyCode()) {
case MODEL_DS18S20:
Serial.println("Model: DS18S20/DS1820");
break;
case MODEL_DS1822:
Serial.println("Model: DS1822");
break;
case MODEL_DS18B20:
Serial.println("Model: DS18B20");
break;
default:
Serial.println("Unrecognized Device");
break;
}
uint8_t address[8];
ds.getAddress(address);
Serial.print("Address:");
for (uint8_t i = 0; i < 8; i++) {
Serial.print(" ");
Serial.print(address[i]);
str_dataString += String(address[i]);
}
Serial.println();
/*
Serial.print("Resolution: ");
Serial.println(ds.getResolution());
Serial.print("Power Mode: ");
if (ds.getPowerMode()) {
Serial.println("External");
} else {
Serial.println("Parasite");
}
*/
Serial.print("Temperature: ");
Serial.print(ds.getTempC());
Serial.print(" C / ");
Serial.print(ds.getTempF());
Serial.println(" F");
str_dataString += ",";
str_dataString += String(ds.getTempC());
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(str_dataString);
dataFile.close();
Serial.println(str_dataString);
} else {
Serial.println("error opening datalog.txt");
}
Serial.println(" ***** ");
Serial.println();
}
delay(30000);
}