Skip to content

Instantly share code, notes, and snippets.

@machinechat
Last active July 23, 2020 02:57
Show Gist options
  • Save machinechat/e9645dd20a4547211eae39ca666ff5fb to your computer and use it in GitHub Desktop.
Save machinechat/e9645dd20a4547211eae39ca666ff5fb to your computer and use it in GitHub Desktop.
Example Arduino code for ESP8266 NodeMCU to sample hall-effect entry switch and send results to Machinechat JEDI One via HTTP
// Arduino code for NodeMCU boards with Espressif 8266 WiFi controller
// for sampling a hall effect sensor on pin D5 and sending the state
// to Machinechat JEDI One via an HTTP Post every 5 seconds
// V1.0
#include <ESP8266WiFi.h> // Include the Wi-Fi library
#include <ESP8266HTTPClient.h>
#define DELAYTIME 1000
// Wi-Fi settings - replace with your Wi-Fi SSID and password, in quotes
const char* ssid = "Your_SSID";
const char* password = "Your_WiFi_Password";
// Change to IP address of Windows/Mac/Ubuntu PC or Raspberry Pi running Machinechat JEDI software
// If you changed the JEDI port number, replace 8100 with the new port
const char* host = "192.168.1.7:8100";
// Declare the variable inPin for the Hall effect sensor
uint8_t inPin = D5;
// This is the setup section and will only run one time
void setup() {
// Configure status LED on NodeMCU board
// Later, it will be programmed to blink every HTTP POST
pinMode(LED_BUILTIN, OUTPUT);
//Let the NodeMCU know inPin is an INPUT
pinMode(inPin, INPUT);
// Configure serial port for debug when NodeMCU board is connected
// to your computer or laptop using USB port
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
// Initiate Wi-Fi connection setup
WiFi.begin(ssid, password);
// Show status on serial monitor
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
// Wait for Wi-Fi connection and show progress on serial monitor
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
// This is the main section and will loop continuously
void loop() {
String postData;
// This is the minimum amount of time to wait before
// reading the sensor
delay(DELAYTIME);
// Store Hall effect sensor state
// State is "true" if door is open
String door_state;
if (digitalRead(inPin) == LOW)
{
door_state = "false";
}
else
{
door_state = "true";
}
Serial.println(door_state);
// Build a string with data to send to JEDI. The format is
// {
// "context": {
// "target_id" : "Sensor1"
// },
// "data": {
// "metric1" : metric_value,
// "metric2" : metric_value
// }
// }
//
// Replace metric1 with what ever data metrics that you are
// sending to JEDI. Replace metric_value with the value of
// the metric. If you have more than one sensor, set the
// target_id with the name of the sensor.
//Modify the post data to include door_state
postData = "{\"context\":{\"target_id\":\"Entry_Sensor\"}, \"data\":{\"door_open\":" + String(door_state) +
" }}";
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
// Send the data to JEDI using HTTP.
String address = String("http://") + String(host) + String("/v1/data/mc");
http.begin(client, address);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(postData);
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.print("received payload: ");
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end(); //Close connection
// Blink the status LED
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
} else {
Serial.println("Error in WiFi connection");
}
// Wait for 5 seconds before repeating the loop
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment