First of all, I am very new to arduino..
How should I Modify this Sketch? It should send the data from A0 PH-Sensor to ESP8266 NodeMCU for Online Viewing of the Value
#Arduino Code
[i]#include <Wire.h>[/i]
[i]#include <Adafruit_GFX.h>[/i]
[i]#include <Adafruit_SSD1306.h>[/i]
[i]#include <ArduinoJson.h>[/i]
[i]
[/i]
[i] [/i]
[i]#define SensorPin 0 // the pH meter Analog output is connected with the Arduino’s Analog[/i]
[i]unsigned long int avgValue; //Store the average value of the sensor feedback[/i]
[i]float b;[/i]
[i]int buf[10],temp;[/i]
[i] [/i]
[i]#define SCREEN_WIDTH 128 // OLED display width, in pixels[/i]
[i]#define SCREEN_HEIGHT 64 // OLED display height, in pixels[/i]
[i]#define OLED_RESET -1 // Reset pin # (or -1 if sharing reset pin)[/i]
[i]Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);[/i]
[i]
[/i]
[i]
[/i]
[i]
[/i]
[i]void setup()[/i]
[i]{[/i]
[i] pinMode(13,OUTPUT); [/i]
[i] Serial.begin(9600); [/i]
[i] [/i]
[i]
[/i]
[i] [/i]
[i] [/i]
[i]if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) [/i]
[i]{ [/i]
[i] Serial.println(F("SSD1306 allocation failed"));[/i]
[i] for(;;); // Don't proceed, loop forever[/i]
[i]}[/i]
[i] display.display();[/i]
[i] delay(2);[/i]
[i] display.clearDisplay();[/i]
[i] [/i]
[i]display.clearDisplay();[/i]
[i]display.setTextColor(WHITE);[/i]
[i]display.setTextSize(2);[/i]
[i]display.setCursor(0,5);[/i]
[i]display.print("PH Sensor");[/i]
[i]display.display();[/i]
[i]delay(3000);[/i]
[i] [/i]
[i]}[/i]
[i]
[/i]
[i][b]StaticJsonBuffer<400> jsonBuffer;[/b][/i]
[i][b]JsonObject& root = jsonBuffer.createObject(); // When I am typing this in it is not working anymore... [/b][/i]
[i]void loop()[/i]
[i]{[/i]
[i] for(int i=0;i<10;i++) //Get 10 sample value from the sensor for smooth the value[/i]
[i] { [/i]
[i] buf[i]=analogRead(SensorPin);[/i]
[i] delay(10);[/i]
[i] }[/i]
[i] for(int i=0;i<9;i++) //sort the analog from small to large[/i]
[i] {[/i]
[i] for(int j=i+1;j<10;j++)[/i]
[i] {[/i]
[i] if(buf[i]>buf[j])[/i]
[i] {[/i]
[i] temp=buf[i];[/i]
[i] buf[i]=buf[j];[/i]
[i] buf[j]=temp;[/i]
[i] }[/i]
[i] }[/i]
[i] }[/i]
[i] avgValue=0;[/i]
[i] for(int i=2;i<8;i++) //take the average value of 6 center sample[/i]
[i] avgValue+=buf[i];[/i]
[i] float phValue=(float)avgValue*5.0/1024/6; //convert the analog into millivolt[/i]
[i] phValue=3.5*phValue; //convert the millivolt into pH value[/i]
[i][b] root["a1"] = phValue;[/b][/i]
[i] [/i]
[i] Serial.print(" pH:"); [/i]
[i] Serial.print(phValue,2);[/i]
[i] Serial.println("");[/i]
[i] [/i]
[i] display.clearDisplay();[/i]
[i] display.setTextSize(2);[/i]
[i] display.setCursor(20,0);[/i]
[i] display.println("Ph Value");[/i]
[i] [/i]
[i] display.setTextSize(3);[/i]
[i] display.setCursor(30,30);[/i]
[i] display.print(phValue);[/i]
[i] [/i]
[i] display.display(); [/i]
[i] [/i]
[i] digitalWrite(13, HIGH); [/i]
[i] delay(800);[/i]
[i] digitalWrite(13, LOW); [/i]
[i] [/i]
[i] [/i]
[i]}[/i]
[i]
#NODEMCU CODE
#include<ESP8266WiFi.h>
#include<WiFiClient.h>
#include<ESP8266WebServer.h>
#include <ArduinoJson.h>
const char* ssid = "speedy";//Replace with your network SSID
const char* password = "50188533294263852955";//Replace with your network password
ESP8266WebServer server(80);
String page = "";
int data1;
void setup()
{
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(WiFi.localIP());
server.on("/", []()
{
page = "<html><head><title>IoT Design</title></head><style type=\"text/css\">";
page += "table{border-collapse: collapse;}th {background-color: green ;color: white;}table,td {border: 4px solid black;font-size: x-large;";
page += "text-align:center;border-style: groove;border-color: rgb(255,0,0);}</style><body><center>";
page += "<h1>Smart Aquaculture Monitoring using IoT</h1>
<table style=\"width: 1200px;height: 450px;\"><tr>";
page += "<th>Parameters</th><th>Value</th><th>Units</th></tr><tr><td>PH Value</td><td>"+String(data1)+"</td><td>N/A</td></tr>";
page += "<tr><td>Temperature</td><td>"+String(data2)+"</td><td>Centigrade</td></tr><tr><td>Moisture</td><td>"+String(data3)+"</td><td>%</td>";
page += "<meta http-equiv=\"refresh\" content=\"3\">";
server.send(200, "text/html", page);
});
server.begin();
}
void loop()
{
StaticJsonBuffer<1000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(Serial);
if (root == JsonObject::invalid())
{
return;
Serial.println("invalid");
}
data1 = root["a1"];
Serial.println(data1);
server.handleClient();
}
[/i]
Hi speedy,
welcome to the forum. Very good to post code as a code-section in the very first posting.
This forum-software has some flaws. Something went wrong while inserting the code
best way to do it is
There is an automatic function for doing this inthe Arduino-IDE
just three steps
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy to forum"
paste clipboard into write-window of a posting
Your question is very demanding: "Here is the code: can somebody please adapt the code for me"
You seem to know nothing about programming. At least you haven't wrote anything about your programming-knowledge. Even if somebody would make the changes in the code for you. Without knowing anything about programming you would be forced to come back and ask again and again and again for each and every single detail you want to change.
So you should make a first attempt to understand where the code must be changed and ask a more specific question.
In 70% of all cases as soon as the thread-opener (TO) writes an overview about his project a much simples and better working solution can be suggested. So it is a good idea to give an overview. Maybe something like blynk or thingSpeak is better suited.
best regards Stefan
I combined those with a simpler webserver code. it something like you would like to do?
(compiles, NOT tested!)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
// Replace with your network credentials
const char* ssid = "<Your-WiFi-SSID>";
const char* password = "<Your-WiFi-Password>";
ESP8266WebServer server(80); //instantiate server at port 80 (http port)
String page = "";
String text = "";
double data;
#define SensorPin A0 // the pH meter Analog output is connected with the Arduino's Analog
unsigned long int avgValue; //Store the average value of the sensor feedback
float b;
int buf[10], temp;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
pinMode(13, OUTPUT);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
delay(2);
display.clearDisplay();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 5);
display.print("PH Sensor");
display.display();
WiFi.begin(ssid, password); //begin WiFi connection
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/data.txt", []() {
text = (String)data;
server.send(200, "text/html", text);
});
server.on("/", []() {
page = "<h1>Sensor to Node MCU Web Server</h1><h1>Ph Value:</h1> <h1 id=\"data\">""</h1>\r\n";
page += "<script>\r\n";
page += "var x = setInterval(function() {loadData(\"data.txt\",updateData)}, 1000);\r\n";
page += "function loadData(url, callback){\r\n";
page += "var xhttp = new XMLHttpRequest();\r\n";
page += "xhttp.onreadystatechange = function(){\r\n";
page += " if(this.readyState == 4 && this.status == 200){\r\n";
page += " callback.apply(xhttp);\r\n";
page += " }\r\n";
page += "};\r\n";
page += "xhttp.open(\"GET\", url, true);\r\n";
page += "xhttp.send();\r\n";
page += "}\r\n";
page += "function updateData(){\r\n";
page += " document.getElementById(\"data\").innerHTML = this.responseText;\r\n";
page += "}\r\n";
page += "</script>\r\n";
server.send(200, "text/html", page);
});
server.begin();
Serial.println("Web server started!");
}
void loop()
{
for (int i = 0; i < 10; i++) //Get 10 sample value from the sensor for smooth the value
{
buf[i] = analogRead(SensorPin);
delay(10);
}
for (int i = 0; i < 9; i++) //sort the analog from small to large
{
for (int j = i + 1; j < 10; j++)
{
if (buf[i] > buf[j])
{
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
avgValue = 0;
for (int i = 2; i < 8; i++) //take the average value of 6 center sample
avgValue += buf[i];
float phValue = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
phValue = 3.5 * phValue; //convert the millivolt into pH value
data = phValue;
Serial.print(" pH:");
Serial.print(phValue, 2);
Serial.println("");
display.clearDisplay();
display.setTextSize(2);
display.setCursor(20, 0);
display.println("Ph Value");
display.setTextSize(3);
display.setCursor(30, 30);
display.print(phValue);
display.display();
digitalWrite(13, HIGH);
delay(800);
digitalWrite(13, LOW);
server.handleClient();
}
I based the webserver code on the example here:
hope that helps....
system
Closed
May 7, 2021, 7:54am
4
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.