Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sender,Sensor code. Similar to Master, Receiver but not identical.,
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantoSensor_2.ino\SantoSensor_2.ino.ino
- //C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor3\SantosSensor3.ino, Wed Aug 10 13:32:09 NZST 2022.
- //Compiled above and ran but had not plugged in Master Wemos with slightly different code.
- /* SantosSensor4.ino Wed Aug 10 16:38:10 NZST 2022. Making strucs better. Representing Master/Slave idea
- SantosSensor4.ino
- Tue Aug 16 12:05:02 NZST 2022 Include new struc variables to make it compatible with SantosMaster3.ino C:\Users\Dell\Documents\Arduino\WorkingAugust22\SantosSensor7\SantosSensor7.ino
- Next version uses HTU sensor in subroutine,Thu Aug 18 13:00:29 NZST 2022
- C:\Users\Dell\Documents\Arduino\WorkingAugust22\HTU_0SantosSensor7\HTU_0SantosSensor7.ino compiles, but not tested yet. Thu Aug 18 14:10:16 NZST 2022. Now just going to call subroutine
- that tests HTU21D
- */
- #include <ESP8266WiFi.h>
- //09
- #include <espnow.h> // #include <Adafruit_Sensor.h> Need this?
- #include <Wire.h>
- #include "SparkFunHTU21D.h"
- //Create an instance of the object
- HTU21D myHumidity;
- uint8_t broadcastAddress[] = {0x98,0xF4,0xAB,0xBF,0xEC,0xCC}; //Mac address of Wemos2
- //{0x18,0xFE,0x34,0xF9,0x2E,0x4A} ; //Mac address of Wemos1-
- // Define variables to store Sensor readings to be sent to Master
- float temperature;
- float humidity;
- unsigned long milliSecsA;
- uint8_t orders;
- uint8_t stack[10];
- uint8_t stackPtr;
- uint8_t bufferPtr;
- uint8_t bigBuffer[200];
- // Define variables to store incoming readings from Master
- float incomingTemp;
- float incomingHum;
- unsigned long incomingMillisA;
- uint8_t incomingOrders;
- uint8_t incomingStac[10];
- uint8_t incomingStackPtr;
- uint8_t incomingBufferPtr;
- uint8_t incomingBigBuffer[200];
- // Updates Sensor readings every 10 seconds, 5 secs
- const long interval = 5000;
- unsigned long previousMillis = 0; // will store last time Sensor was updated
- int t=0; //for light blinking
- // Variable to store if sending data was successful
- String success;
- //Structure example to send data
- //Must match the receiver structure
- typedef struct struct_message {
- float temp;
- float hum;
- unsigned long millisA; //for timing
- uint8_t ord;
- uint8_t stac[10];
- uint8_t stacPtr;
- uint8_t bufPtr;
- uint8_t bigBuf[200];
- } struct_message;
- // Create a struct_message called SensorReadings to hold sensor readings; to be sent to master
- struct_message SensorReadings;
- // Create a struct_message to hold incoming master readings
- struct_message incomingReadings;
- // Callback when data is sent
- void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
- //delay(100);
- Serial.print("\nLast Packet Send Status (From Sensor I: ");
- //delay(110); //above delays make sure something gets serial-written
- if (sendStatus == 0){
- Serial.println("Delivery success");
- }
- else{
- Serial.println("Delivery fail");
- }
- }
- // Callback when data is received
- void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
- memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
- //led on
- //digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- //delay(100) ;
- Serial.print("OnDataRecv() running. Bytes received: ");
- Serial.println(len);
- incomingTemp = incomingReadings.temp;
- incomingHum = incomingReadings.hum;
- incomingMillisA= incomingReadings.millisA;
- incomingOrders=incomingReadings.ord;
- for(int i =0;i<10;i++) incomingStac[i]=incomingReadings.stac[i];
- incomingStackPtr=incomingReadings.stacPtr;
- incomingBufferPtr=incomingReadings.bufPtr;
- for(int i =0;i<10;i++) incomingBigBuffer[i]=incomingReadings.bigBuf[i];
- Serial.println("OnDataRecv() finished");
- //led off
- //digitalWrite(LED_BUILTIN, LOW); delay(100);
- //Now all the struct fields from have been put into slave sensor variables ...
- //Like incomingxxx=incomingReadings.xxx, where xxx's are similar but not identical.
- }
- void getReadings(){
- // Read the sender's sensor data. Read Temperature. Fake,right now.
- Serial.println("\n now doing getReadings()))))))))))))))))");
- temperature = 1112.3 ; //dht.readTemperature();
- // Read temperature as Fahrenheit (isFahrenheit = true)
- //float t = dht.readTemperature(true);
- if (isnan(temperature)){
- Serial.println("Failed to read from Sensor");
- temperature = 0.0;
- }
- humidity = 1145.6; //dht.readHumidity();
- if (isnan(humidity)){
- Serial.println("Failed to read from Sensor");
- humidity = 0.0;
- }
- }
- void printIncomingReadings(){
- // Display Readings in Serial Monitor. Nothing so far
- Serial.println("\nINCOMING READINGS from Master ----------------------");
- Serial.print("Temperature: ");
- Serial.print(incomingTemp);
- Serial.println(" ºC");
- Serial.print("Humidity: ");
- Serial.print(incomingHum);
- Serial.println(" %");
- Serial.print(incomingMillisA);
- Serial.println(" ..m..");
- Serial.print(incomingOrders);
- Serial.println(" incomingOrders..");
- Serial.print(incomingStackPtr);
- Serial.println("incomingStackPtr");
- for(int i =0;i<10;i++) Serial.print( incomingStac[i]);
- Serial.println(" <--incomingStac[]");
- for(int i =0;i<200;i++) Serial.print( incomingBigBuffer[i]);
- Serial.println(" <--incomingBigBuffer[]");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- myHumidity.begin();
- pinMode(LED_BUILTIN, OUTPUT);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- // Init ESP-NOW
- if (esp_now_init() != 0) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // Set ESP-NOW Role
- esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
- // Once ESPNow is successfully Init, we will register for Send CB to
- // get the status of Trasnmitted packet
- esp_now_register_send_cb(OnDataSent);
- // Register peer
- esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
- // Register for a callback function that will be called when data is received
- esp_now_register_recv_cb(OnDataRecv);
- }
- void loop() {
- //loopLED();
- //getHDU();
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- // save the last time you updated the DHT values
- previousMillis = currentMillis; //NB programmable delay.
- doLED();
- //Get Sensor 1 readings
- //getReadings();
- getReadings2(); //read the Sensor
- //Set values to send
- SensorReadings.temp = temperature;
- SensorReadings.hum = humidity;
- SensorReadings.millisA=millis();
- // Send message via ESP-NOW
- esp_now_send(broadcastAddress, (uint8_t *) &SensorReadings, sizeof(SensorReadings));
- //delay(100);
- // Print incoming readings
- printIncomingReadings();
- Serial.print(" ================Orders0= ");
- Serial.println( incomingReadings.ord);
- }
- }
- void getHDU() { //read via HDU21 the temp and humidity
- while(1)
- {
- Serial.println("Nothing HDUish here yet.");
- float humd = myHumidity.readHumidity();
- float temp = myHumidity.readTemperature();
- Serial.print("Time:");
- Serial.print(millis());
- Serial.print(" Temperature:");
- Serial.print(temp, 1);
- Serial.print("C");
- Serial.print(" Humidity*:");
- Serial.print(humd, 1);
- Serial.print("%");
- Serial.println();
- delay(1000);
- }
- }
- void getReadings2(){
- //Replaces fake readings with real ones from the breadboad of Wemos Mini 1
- // float humd = myHumidity.readHumidity();
- //float temp = myHumidity.readTemperature(); //readTemperature();
- temperature = myHumidity.readTemperature(); //readHumidity(); //both temperature and humidity are global floats
- humidity= myHumidity.readHumidity(); //readTemperature();
- Serial.print("Time:");
- Serial.print(millis());
- Serial.print(" Temperature:");
- Serial.print(temperature, 1);
- Serial.print("C");
- Serial.print(" humidity:");
- Serial.print(humidity, 1);
- Serial.print("%");
- Serial.println();
- }
- void doLED() {
- t++; if(t==2) t=0;
- // Serial.print("--------t=");
- Serial.println(t);
- //Serial.print(LED_BUILTIN);
- if (t==1) digitalWrite(LED_BUILTIN, HIGH);
- if (t==0) digitalWrite(LED_BUILTIN, LOW);
- }
- /*
- void setup() {
- // initialize digital pin LED_BUILTIN as an output.
- pinMode(LED_BUILTIN, OUTPUT);
- }
- // the loop function runs over and over again forever
- void loopLED() {
- while(1)
- {
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment