Recently I obtained a cheap (under $8) teeny-tiny SIM800 breakout board. The serial UART interfaced SIM800 makes it very easy for any embedded system to add cellular network access and connect to the cloud. The breakout board has a Mirco SIM seat the the back of the PCB.
After some efforts, I managed to make it work with an Arduino Leonardo. Basically 5 wires are needed to connect the module to Arduino and they connect pins of VCC/GND/Rx/Tx/Reset. This is quite straight-forward. The module’s VCC can’t directly connected to that of Arduino as SIM800 requires a working voltage of 3.7V-4.2V. So a dixode is needed to step down the voltage a bit. The basic programming to make the module work was soon done by referring to the SIM800 AT command-set manual.
My target is continuously sending HTTP requests containing sensor data to a web server (data as URL argument) and retrieve the response containing command. To make it easier for myself and other people who use the same SIM800 based modules, I started to write an Arduino library for this purpose. It contains only what I need at the moment. That includes what is needed to perform HTTP request and retrieving GSM location data. I am expanding it graudually. The library is hosted on GitHub and an exmple sketch is also available. The sketch can be as simple as following.
#include "SIM800.h" #define APN "connect" #define con Serial static const char* url = "http://arduinodev.com/datetime.php"; CGPRS_SIM800 gprs; void setup() { con.begin(9600); while (!con); for (;;) { con.print("Resetting..."); while (!gprs.init()); con.println("OK"); con.print("Setting up network..."); byte ret = gprs.setup(APN); if (ret == 0) break; con.print("Error code:"); con.println(ret); } con.println("OK"); for (;;) { if (gprs.httpInit()) break; con.println(gprs.buffer); gprs.httpUninit(); delay(1000); } } void loop() { gprs.httpConnect(url); while (gprs.httpIsConnected() == 0) { // can do something here while waiting } if (gprs.httpState == HTTP_ERROR) { con.println("Connect error"); return; } con.println(); gprs.httpRead(); int ret; while ((ret = gprs.httpIsRead()) == 0) { // can do something here while waiting } if (gprs.httpState == HTTP_ERROR) { con.println("Read error"); return; } // now we have received payload con.print("[Payload]"); con.println(gprs.buffer); // show position GSM_LOCATION loc; if (gprs.getLocation(&loc)) { con.print("LAT:"); con.print(loc.lat, 6); con.print(" LON:"); con.print(loc.lon, 6); con.print(" TIME:"); con.print(loc.hour); con.print(':'); con.print(loc.minute); con.print(':'); con.println(loc.second); } }
Pingback: Microcontrolers | Pearltrees