Sending data from node MCU using Arduino IDE with the help of variables - mysql

#include "DHT.h"
#define DHTTYPE DHT11
#define dht_dpin 0
const int analog_ip = A0;
int inputVal = 0;
DHT dht(dht_dpin, DHTTYPE);
void setup() {
dht.begin();
Serial.begin(9600);
Serial.println("Humidity sensor \n\n");
delay(1000);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("current humidity = ");
Serial.print(h);
Serial.println("% ");
Serial.print("\ncurrent temperature = ");
Serial.print(t);
Serial.println("C ");
inputVal = analogRead (analog_ip);
Serial.print("\ncurrent heartbeat = ");
Serial.println (60000/inputVal);
delay(1000);
}
This is my working code. I want to send data from these sensors to the database. How can I do this using variables of my code? I am using Node MCU as I/O processor.

visit this githup page
Github MySQL Connector with Samples
and download the lib.
There are many samples how to use MySQL / MariaDB.

Related

How can I fix my serial communication from Arduio Uno to ESP8266?

I'm testing the Serial Communication with my Arduino and NodeMCU, and how I can send one data to another. Here is the Code.
For the Arduino Uno
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); //RX, TX
float i = 10;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop() {
Serial.print("Number ");
Serial.print(i);
Serial.println("");
mySerial.write(i);
delay(1000);
}
And here is for my ESP8266 NodeMCU
#include <SoftwareSerial.h>
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
The result should be that in the Serial Monitor it should say "The Number is 10". But instead it says -1.00 enter image description here
Am I doing something wrong?
Ah, I finally figured it out. Apparently I need to initialize the NodeMCU Pin's as so:
#include <SoftwareSerial.h>
static const uint8_t D1 = 5;
static const uint8_t D2 = 4;
SoftwareSerial myNode(D1, D2); //RX, TX
float git;
void setup() {
Serial.begin(115200);
myNode.begin(9600);
}
void loop() {
git = Serial.read();
Serial.print("The Number is ");
Serial.print(git);
Serial.println("");
delay(1000);
}
Try
git = myNode.read();
instead of
git = Serial.read();
And check myNode.available() before reading

How to send and parse JSON data through NRF24L01 sensor?

TRANSMITTER.ino (COM8 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
radio.begin();
radio.openWritingPipe(addresses);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
DynamicJsonBuffer jBuffer;
JsonObject& root = jBuffer.createObject();
root["North"] = "true";
root["South"] = "false";
root["East"] = "true";
root["West"] = "true";
radio.write(&root, sizeof(root));
delay(1000);
}
RECEIVER.ino (COM9 port)
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include<ArduinoJson.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses[6] = {"00001"};
void setup() {
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, addresses);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if ( radio.available()) {
unsigned char data[1024];
radio.read(&data, sizeof(data));
StaticJsonBuffer<1024> jsonBuffer;
JsonObject& toor = jsonBuffer.parseObject(data);
String n = toor["North"];
String s = toor["South"];
String e = toor["East"];
String w = toor["West"];
Serial.println(n);
Serial.println(s);
Serial.println(e);
Serial.println(w);
delay(1000);
}
}
I am trying to transmit a JSON data over NRF24L01, and printing it to the serial monitor, but i dont see any output in my serial monitor(COM9 serial monitor). what is the mistake am i doing here?
I am using arduino JSON 5.13.5 version

Why does the serial monitor just show an endless loop of the "dot" and not the other serial prints?

The code should transfer an JSON object and the print "hello" in the void loop, but it just repeat the "dot" from the void setup. What did i miss?
I tried different serial prints in the main loop but none of them is actually transferred. Maybe there is an infinite loop in the void setup?
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <vector>
const char* apSsid = "ap-ssid";
const char* apPassword = "ap-password";
const char* clientSsid = "client-ssid";
const char* clientPassword = "client-password";
WiFiEventHandler probeRequestPrintHandler;
String macToString(const unsigned char* mac) {
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return String(buf);
}
std::vector<WiFiEventSoftAPModeProbeRequestReceived> myList;
void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {
myList.push_back(evt);
}
void setup() {
Serial.begin(115200);
Serial.print("Hello!");
// Don't save WiFi configuration in flash - optional
WiFi.persistent(false);
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(apSsid, apPassword);
WiFi.begin(clientSsid, clientPassword);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.print("");
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);
}
void loop() {
delay(3000);
String json = "";
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonArray& probes = root.createNestedArray("probes");
for(WiFiEventSoftAPModeProbeRequestReceived w : myList){
JsonObject& probe = probes.createNestedObject();
probe["address"] = macToString(w.mac);
probe["rssi"] = w.rssi;
}
myList.clear();
root.printTo(json);
Serial.print(json);
Serial.print("hallo");
}
It should transfer the dot, a json object and the word "hallo"
This is because the ESP8266 can't find a WiFi access point with SSID client-ssid and password client-password.
Unless it gets connected to the said access point it will print . on the screen.
You can change the access point parameters to one that visible in the range of ESP8266 alternatively you can change the access point SSID and password to one in the code.
You can use the WiFi manager to avoid hard-code SSID names and password.

Mongoose C++: How to parse HTTP GET or POST request using mongoose?

I am trying to create a simple c++ web-based GUI. I am not interested in using Qt or Visual Studio based GUI. I am rather interested in web based as my requirements are very minimal and basic.
So I came across "Mongoose" the C-based web server. After going through the examples I cooked up some code but it's not working as I have almost zero knowledge about internet programming. I was wondering if any of you have a simple example where I can retrieve the user data from the HTML form either using POST or GET request.
Here is what I have managed so far:
//////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mongoose.h"
static const char *s_http_port = "8000";
volatile bool kill_server = FALSE;
struct mg_mgr mgr;
struct mg_connection *nc;
bool control1_triggered = FALSE;
bool control2_triggered = FALSE;
struct file_writer_data {
FILE *fp;
size_t bytes_written;
};
static void handle_upload(struct mg_connection *nc, int ev, void *p) {
printf("Signal received! %d\n", ev);
control1_triggered = TRUE;
struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p;
printf(mp->data.p);
switch (ev) {
case MG_EV_HTTP_PART_DATA:
break;
}
}
static void handle_upload2(struct mg_connection *nc, int ev, void *p) {
printf("Signal received#2! %d\n", ev);
control2_triggered = TRUE;
}
void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
(void)ev_data;
switch (ev) {
case MG_EV_HTTP_REQUEST:
// Invoked when the full HTTP request is in the buffer (including body).
mg_printf(nc, "%s",
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"<html><body>Controls"
"<form method=\"GET\" action=\"/upload\" "
" enctype=\"multipart/form-data\">"
"<input type = \"text\" name = \"fname\" value = \"John\">"
"<input type=\"submit\" value=\"Fix Position\" />"
"</form>"
"<form method=\"POST\" action=\"/Kill\" "
" enctype=\"multipart/form-data\">"
"<input type=\"submit\" value=\"Kill Server\" />"
"</form>"
"input.search{width: 20em; height: 2em;}"
"</body></html>");
nc->flags |= MG_F_SEND_AND_CLOSE;
break;
}
}
int main() {
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, s_http_port, ev_handler);
mg_register_http_endpoint(nc, "/upload", handle_upload);
mg_register_http_endpoint(nc, "/Kill", handle_upload2);
// Set up HTTP server parameters
mg_set_protocol_http_websocket(nc);
while (1);
return 0;
}
Please note I have been googling around 3 days now, have seen most of the links and questions. But not a lot of support with Mongoose
Could you please help me with an example on how to parse GET or POST HTML request using Mongoose ?
Thank you so much.
Cheers,
Avi
You access post data from nc->content, to get a certain value you use mg_get_var(nc, size of nc, "fname", buffer, size of buffer).
Example:
int size = 1024, ret;
char *buffer = new char[size];
mg_get_var(nc, sizeof(nc), "fname", buffer, size);
Side note Gregwar made a C++ wrapper for Mongoose, its an older version (by about four years) but it might help, link.
Edit:
Method should be mg_get_http_var not mg_get_var
int size = 1024, ret;
char *buffer = new char[size];
mg_get_http_var(nc, "fname", buffer, size);
Link

how to send data serially and simultaneously read serial data using json library

i just started using the ArduinoJson library for my project. my aim is to send three sensor values using BT to a RasPi from my arduino nano using json format. Also, control a relay by receiving '0' or '1' from BT.
I am successfully able to send data. But, when i include the following piece of code: (to read serial data)
if (Serial.available > 0)
{
z = Serial.read();
...............
}
and i send a '1'; my data transmission is disturbed and the json string is affected. when i send '0', it goes back to normal.
i need help in solving this issue as im unable to find out where i am going wrong!! kindly do the needful!!
my code:
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTPIN 12 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 sensor
DHT dht(DHTPIN, DHTTYPE); //initilize the DHT sensor
int ldr = A0; // LDR connected to A0
int relay = 13;
float h,t;
int rx = 11; // softwareserial rx of arduino is pin 11
int tx = 10; // softwareserial tx of arduino is pin 12
SoftwareSerial mySerial(rx,tx);
int l; // variable to store the value coming from the sensor
int bt = 0; // variable to store incoming BT data
void setup()
{
pinMode(relay, OUTPUT);
pinMode(ldr, INPUT);
Serial.begin(9600);
mySerial.begin(9600);
mySerial.println(" Project FLIP");
mySerial.println("sensor testing");
mySerial.println(" DHT11 test!");
dht.begin();
delay(2000);
}
void loop()
{
humidity_read();//read humidity value
temperature_read();//read temperature
light_read(); //read LDR value
jason_print();
relay_control();//control relay by reading serial data 0=off, 1=on
jason_print();
}
void jason_print()
{
StaticJsonBuffer<200> temp;
JsonObject& root = temp.createObject();
root["humidity"] = h;
root["temp"] = t;
root["light"] = l;
root.printTo(Serial);
Serial.println();
delay(1000);
}
void humidity_read()
{
h = dht.readHumidity(); //
}
void temperature_read()
{
t = dht.readTemperature(); // Read temperature as Celsius (the default)
}
void relay_control()
{
if (Serial.available() > 0)
{
bt = Serial.read();
if (bt == '1')
{
digitalWrite(relay, HIGH);
}
else if (bt == '0')
{
digitalWrite(relay, LOW);
}
}
}
void light_read()
{
l = analogRead(ldr); //Read LDR value
}
i tried using software serial concept and the same code seems to be working perfectly!! i connected a HC-05 BT module to D10 & 11 pins of arduino and am successfully able to control the relay using my mobile. also, no interference with the json data that is also being transmitted..
but, i still dont know why there is a problem if im using the serial monitor of arduino IDE to do the same operation..