[Help] HMC5883L (Magnetometer) with Uno to ThingSpeak.com
Posted: Sat Mar 05, 2016 6:33 am
Hi, fellow ESP8266 Community.
I need quite a lot enlightenment regarding my current project. I really wish lots of smarter (than me) people in this forum would help bit by bit. Furthermore, I thought since nobody has posted this kind of project in this forum so it could bring new general ideas toward whole community.
FYI, HMC5883L only have 5 pin which 4 of them are connected to Arduino Uno. (VCC->3.3V, GND->GND, SDA->A4, SCL->A5). The last pin is DRDY which no need to be used because the current output has sufficient clock already (Interrupt Pin). HMC5883L produces integer data types representing miliGauss in each axis (X,Y,Z). I have print these results in serial monitor Arduino and it works fine. Scaled = Raw with some formulas. You can look below.
Using ESP8266, I want to send 3 integer data types to ThingSpeak.com. But still I haven't found a way to do it. My UNO code is below. Please ask if there is something you want to know. Thank you very much !
I need quite a lot enlightenment regarding my current project. I really wish lots of smarter (than me) people in this forum would help bit by bit. Furthermore, I thought since nobody has posted this kind of project in this forum so it could bring new general ideas toward whole community.
FYI, HMC5883L only have 5 pin which 4 of them are connected to Arduino Uno. (VCC->3.3V, GND->GND, SDA->A4, SCL->A5). The last pin is DRDY which no need to be used because the current output has sufficient clock already (Interrupt Pin). HMC5883L produces integer data types representing miliGauss in each axis (X,Y,Z). I have print these results in serial monitor Arduino and it works fine. Scaled = Raw with some formulas. You can look below.
Using ESP8266, I want to send 3 integer data types to ThingSpeak.com. But still I haven't found a way to do it. My UNO code is below. Please ask if there is something you want to know. Thank you very much !
Code: Select all
/** Definisi Kamus **/
#include <Wire.h>
#include <SPI.h>
#include <HMC5883L.h>
#include <Ethernet.h>
/** HMC5883L **/
// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;
/** HMC5883L DONE**/
/** ESP8266 **/
// Local Network Settings
byte mac[] = { "My Mac Address, not written here" }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "My API Channel Key, not written here";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
#define IP "184.106.153.149" // thingspeak.com IP address; see their website for more detail
String GET = "GET /update?api_key=My API Channel Key, not written here&field"; // you channel will have unique code
String field1="1=";
String field2="2=";
String field3="3=";
String field4="4=";
// Initialize Arduino Ethernet Client
EthernetClient client;
/** ESP8266 DONE **/
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
/** HMC5883L SETUP **/
Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.
Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.
Serial.println("Setting scale to +/- 1.3 Ga");
error = compass.SetScale(1.3); // Set the scale of the compass.
if(error != 0) // If there is an error, print it out.
Serial.println(compass.GetErrorText(error));
Serial.println("Setting measurement mode to continous.");
error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
if(error != 0) // If there is an error, print it out.
Serial.println(compass.G not written hereetErrorText(error));
/** HMC5883L DONE **/
/** ESP8266 **/
Serial.println("AT+RST");
delay(5000);
Serial.println("AT");
delay(5000);
if(Serial.find("OK"))
{
Serial.println("OK");
Serial.println("Connected");
}
/** ESP8266 DONE **/
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
/** HMC5883L**/
// Retrive the raw values from the compass (not scaled).
MagnetometerRaw raw = compass.ReadRawAxis();
// Retrived the scaled values from the compass (scaled to the configured scale).
MagnetometerScaled scaled = compass.ReadScaledAxis();
// Values are accessed like so:
int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)
int MilliGauss_OnThe_YAxis = scaled.YAxis;// (or YAxis, or ZAxis)
int MilliGauss_OnThe_ZAxis = scaled.ZAxis;// (or YAxis, or ZAxis)
int ada=0;
if ((MilliGauss_OnThe_ZAxis <0) || (MilliGauss_OnThe_ZAxis > 300))
{ada=1;
Serial.println("ADA");}
else
{ada=0;}
/** HMC5883L DONE **/
/** ESP8266 **/
String cmd = "AT+CIPSTART=\"TCP\",\""; //standard code. see https://github.com/espressif/esp8266_at/wiki //
cmd += IP; // += is concatenating the previous cmd string with the content of IP
cmd += "\",80"; // same thing here it just keep adding stuff to the cmd string content
Serial.println(cmd);//type in the string in cmd into the ESP8266
delay(5000);
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
delay(5000);
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("1="+MilliGauss_OnThe_XAxis);
updateThingSpeak("2="+MilliGauss_OnThe_YAxis);
updateThingSpeak("3="+MilliGauss_OnThe_ZAxis);
updateThingSpeak("4="+ada);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 )
{startEthernet();}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void startEthernet()
{
client.stop();
Serial.println("Connecting Arduino to network...");
Serial.println();
delay(1000);
// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}