Engineering and technology notes

Cleaning Up Pasted Text

The ease of copying and pasting text from Web sites and email greatly simplifies many tasks in Word, but problems often arise in making the pasted text conform to the style of the document into which it is pasted. One of the most common chores is getting rid of excess line breaks, which cause the text to wrap short of the right margin. There are several ways to work around this problem.

Source: Cleaning Up Pasted Text

Arduino – Compare Board specs

This table shows a quick comparison between the characteristics of all the Arduino and Genuino boards.

Arduino Retired boards specs

Source: Arduino – Compare

Sending events from an Arduino to Riemann

Sending events from an Arduino to Riemann

Introduction

Monitoring can be many things. At uSwitch we monitor not only the general health of the servers we have in production, but also the internal state of the services, such as the number of reports stuck in a process, needing human intervention. For much of this monitoring, we use Riemann. Riemann allows us to quickly set up basic server monitoring, but is open enough for us to easily add new business specific metrics.

Riemann events don’t need to originate from a server in the cloud. In this post I’m going to demonstrate how to send events from an Arduino to Riemann, using a HTTP proxy. I am going to describe a simple circuit with a thermistor, an Ethernet interface and an Arduino Uno. The circuit is going to send the current temperature in the room to a Riemann server every five seconds.

The objective of the post is not to go into detail with every step of the construction of the circuit, but rather to describe the overall design.

Architecture

Sending events to Riemann requires access to one of the existing Riemann client libraries, or an implementation of the Riemann protocol. The Arduino platform is fairly limited in the size of the programs that can be stored on the board, and the range of libraries available. There isn’t an Arduino Riemann library out there, and even if there were, it might not be able to fit on the board, given other libraries and custom code.

Therefore, this setup only uses a TCP library on the Arduino, and a Riemann HTTP proxy written in Clojure to forward events to Riemann.

Architectual overview

Setting up a Riemann HTTP proxy

This setup uses the Riemann HTTP Proxy project. If you go that project and clone the repo onto the box where you are running Riemann, you can start the proxy simply by changing to the directory where it was cloned and executing lein run.

This assumes that you have leiningen installed. If you do not, installation instructions are avaiable here.

Per default, the proxy is going to listen for HTTP traffic on port 8123, interpret anything it gets as an edn-formatted Riemann event and try to forward it to Riemann running on localhost. You can change the listening post with the --port argument, and the server address of Riemann with the --server argument.

You’ll probably want to have the proxy running using an upstart script or something similar.

Getting the Arduino online

To get your Arduino online, you either need an Ethernet shield or a stand-alone Ethernet interface. The Ethernet shield is typically around £30, whereas the Ethernet interface can be bought on Ebay for about £5.

I went for the cheap option and got a ENC28J60 Ethernet interface online. If you do the same, you’ll need to connect the pins to your board manually, either by soldering wires unto the board, or by using female-male jumper wires. You should connect the board up as follows:

  • SO to Arduino pin 12
  • SI to Arduino pin 11
  • SCK to Arduino pin 13
  • CS to Ardunio pin 8
  • VCC to Arduino 3V3 pin
  • GND to Arduino GND pin

This Instructables was a great help for me when setting it up.

The chip requires a driver. In this post we are going to use the EtherCard driver for Arduino. You need to download and install the latest version in the Arduino editor. There are instructions as to how to do it on the EtherCard wiki.

Add a thermistor to the Arduino

A thermistor is a small component that varies its resitance according to its temperature. If we connect a thermistor to the analog A0 pin on the Arduino through a 10K Ohm resistor to ground, we can use analogRead to get accurate readings we can convert to degrees celsius using the Steinhart-Hart equation.

This guide is very helpful for playing around with the thermistor. The guide contains a wiring diagram if you need the details for hooking up the thermistor to A0.

A setup similar to the one described will look something like this.

Arduino setup with Ethernet addapter and thermistor

Arduino source

The program we are going to upload to the Arduino is quite simple. Every five seconds, we read the temperature and send the result as an edn-formatted event to the HTTP proxy. To keep the program simple, there is no error handling.

This example assumes your Riemann HTTP proxy can be looked up on DNS. Just change the proxyHost variable to point to your proxy, and you should be good to go.

#include <EtherCard.h>
#include <Stash.h>

static byte mac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500];
Stash stash;

char proxyHost[] PROGMEM = "my.domain.com";

static uint32_t timer;

void setup () {
  Serial.begin(57600);

  if (ether.begin(sizeof Ethernet::buffer, mac) == 0)
    Serial.println( "Failed to access Ethernet controller");

  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);

  if (!ether.dnsLookup(proxyHost))
    Serial.println("DNS failed");
  ether.hisport  =  8123;

  ether.printIp("SRV: ", ether.hisip);
}

double thermister(int adc) {
  double t;
  t = log(((10240000/adc) - 10000));
  t = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * t * t )) * t );
  t = t - 273.15;
  return t;
}

static void postTemperature(double temperature) {
  char temperatureString[10];
  dtostrf(temperature,1,2,temperatureString);

  byte sd = stash.create();
  stash.print("{:state \"ok\", :service \"temperature\", :metric ");
  stash.print(temperatureString);
  stash.print(", :host \"lounge\"}");
  stash.save();
  Stash::prepare(PSTR("POST http://$F:8123/ HTTP/1.1" "\r\n"
                      "Host: $F:8123" "\r\n"
                       "Content-Length: $D" "\r\n"
                       "\r\n"
                       "$H"),
                 proxyHost, proxyHost, stash.size(), sd);
  ether.tcpSend();
}

void loop () {
  int val = analogRead(0);
  double temperature = thermister(val);

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  if (millis() > timer) {
    postTemperature(temperature);
    timer = millis() + 5000;
  }
}

Hopefully, if you go to your Riemann dashboard, you’ll be able to read the current temperature in the room you’ve plaved the Arduino as the metric of the temperature events that are ticking in.

A Riemann dashboard with the temperature in my lounge - toasty!

Conclusion

In this post I’ve demonstrated how to send events from a very simple platform, the Arduino, to Riemann without relying on a Riemann library on the platform from which the events originate.

The question now is what to do with the events once they’re in Riemann. If you want to store them for further analysis, you can forward them to Graphite, or you could send out an email if the current temperature gets above a set threshold.

Using a HTTP proxy to send Riemann events can also be an advantage in projects where you wish to introduce Riemann monitoring on a component basis, but where you are restricted to a certain range of libraries. Since HTTP will (almost) always be available, you can resort to sending HTTP messages to the proxy, rather than import another library to your codebase.

Source: Sending events from an Arduino to Riemann

SMTP protocol problems · Issue #65 · jcw/ethercard · GitHub

Hi,

I’ve been looking at the possibility of sending SMTP (email) using Ethercard, however the way the current packetloop works doesn’t seem to be very compatible with SMTP.

I can get as far as the connection message being returned e.g. “220 SMTP server ready”,
by having a custom fill callback function, that doesn’t fill with anything.
and by setting persist_tcp_connection=true to stop the connection being immediately closed

However looking at the ACT sent in response to the connection message, the current packetloop sends [PSH,ACK] where normally SMTP just sends ACK

Even if I ignore this, the next step in the protocol is to send the “HELO” message using [PSH,ACK] however by this point the packetloop code seems very confused and setting tcp_client_state=2 and waiting for the next call to the buffer fill callback, doesn’t happen because the packetloop returns before it gets to the state==2 code 🙁

Can someone tell me whether the current packetloop is likely to be able to support a new protocol at all, or with minor modifications, or whether I would need to write my own packetloop (based heavily on the existing code) ?

PS. I’m an experienced programming, but have had no dealings with TCPIP before.

FYI.
Looking at a working SMTP transfer the overall packet structure looks like this

SYN
SYN,ACK
ACK

TCP duplicate ACK (not sure why this happens)

PSH,ACK server sends connection message
ACK client ack to server connection message
PSH,ACK client sends HELO message
ACK server sends ACK for client ACK

etc etc

Source: SMTP protocol problems · Issue #65 · jcw/ethercard · GitHub

Send email on event with EtherCard | JeeLabs.net Forum

Send email on event with EtherCard

Thank you for your work on EtherCard. I have not used it yet, but it looks very promising. I have been using the etherShield libraries that I found via the geetech forums with good results.

My problem is that I want to send an email on a detected event (button press, temperature change, etc). Does the EtherCard library have this capability? I have not been successful doing this with the etherShield libraries. I have done this with the Arduino ethernetShield device (W5100), and the Arduino libraries. However, I would much rather use the ENC28J60 module to do this. I can handle the event detection portion. What I need help with is just the send an email part. Any advice would be greatly appreciated!

Thanks, MFM

Source: Send email on event with EtherCard | JeeLabs.net Forum

strcpy(), strncpy()

Copy a string

Prototypes

#include <string.h>
char *strcpy(char *dest, char *src);
char *strncpy(char *dest, char *src, size_t n);

Description

These functions copy a string from one address to another, stopping at the NUL terminator on the srcstring.

strncpy() is just like strcpy(), except only the first n characters are actually copied. Beware that if you hit the limit, n before you get a NUL terminator on the src string, your dest string won’t be NUL-terminated. Beware! BEWARE!

(If the src string has fewer than n characters, it works just like strcpy().)

You can terminate the string yourself by sticking the '\0' in there yourself:

char s[10];
char foo = "My hovercraft is full of eels."; // more than 10 chars

strncpy(s, foo, 9); // only copy 9 chars into positions 0-8
s[9] = '\0';        // position 9 gets the terminator

Return Value

Both functions return dest for your convenience, at no extra charge.

Example

char *src = "hockey hockey hockey hockey hockey hockey hockey hockey";
char dest[20];

int len;

strcpy(dest, "I like "); // dest is now "I like "

len = strlen(dest);

// tricky, but let's use some pointer arithmetic and math to append
// as much of src as possible onto the end of dest, -1 on the length to
// leave room for the terminator:
strncpy(dest+len, src, sizeof(dest)-len-1);

// remember that sizeof() returns the size of the array in bytes
// and a char is a byte:
dest[sizeof(dest)-1] = '\0'; // terminate

// dest is now:       v null terminator
// I like hockey hocke 
// 01234567890123456789012345

See Also

Source: strcpy(), strncpy()

sprint or strcpy in arduino ?

I have a function that gets a pointer , and it should then change that pointer to a new one, so void function (char *pointer) {    char data[40];   // some calculations    //send back respond     strcpy(pointer,data);   } I have read here that strcpy is a bad practice and that there are replacements(in Arduino? ) : http://stackoverflow.com/questions/12275381/strncpy-vs-sprintf What is the right command to change that pointer argument safely ?

Source: sprint or strcpy in arduino ?