NodeMcu Serial Comm IOT
user_8125766783
plain_text
3 years ago
2.7 kB
2
Indexable
Never
// BLYNK INTERFACE CONNECTIVITY #define BLYNK_TEMPLATE_ID "TMPLmihvENoF" #define BLYNK_DEVICE_NAME "Weight Sensor" #define BLYNK_AUTH_TOKEN "v_f8skPGopj52y7vwTu5aeMBl4wiCkBa" #define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <SoftwareSerial.h> SoftwareSerial NodeMcu(D2,D3); // Your WiFi credentials. // Set password to "" for open networks. char ssid[] = "Virus1"; char pass[] = "Pandemic_2019"; char auth[] = "v_f8skPGopj52y7vwTu5aeMBl4wiCkBa"; BlynkTimer timer; String myString; // complete message from arduino, which consistors of sensors data char rdata; // received characters int firstVal, secondVal; // sensor values // This function sends Arduino's up time every second to Virtual Pin (1). // In the app, Widget's reading frequency should be set to PUSH. This means // that you define how often to send data to Blynk App. void myTimerEvent() { // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V1, millis() / 1000); } void setup() { Serial.begin(115200); NodeMcu.begin(9600); Blynk.begin(auth, ssid, pass); timer.setInterval(1000L,Voltage_sensorvalue); timer.setInterval(1000L,Weight_sensorvalue); } void loop() { if (NodeMcu.available() == 0 ) { Blynk.run(); timer.run(); // Initiates BlynkTimer } if (NodeMcu.available() > 0 ) { Serial.println(rdata); rdata = Serial.read(); myString = myString+ rdata; if( rdata == '\n') { Serial.println(myString); String l = getValue(myString, ',', 0); String m = getValue(myString, ',', 1); firstVal = l.toInt(); secondVal = m.toInt(); myString = ""; } } } void Voltage_sensorvalue() { int sdata = firstVal; // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V2, sdata); } void Weight_sensorvalue() { int sdata = secondVal; // You can send any value at any time. // Please don't send more that 10 values per second. Blynk.virtualWrite(V3, sdata); } String getValue(String data, char separator, int index) { int found = 0; int strIndex[] = { 0, -1 }; int maxIndex = data.length() - 1; for (int i = 0; i <= maxIndex && found <= index; i++) { if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] = strIndex[1] + 1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; }